mirror of
https://github.com/brunofontes/Memsource-API.git
synced 2025-12-11 11:50:48 -03:00
Including Tests and verifications for errors
This commit is contained in:
@@ -15,4 +15,15 @@ class BaseApi
|
||||
{
|
||||
$this->fetchApi = $fetchApi;
|
||||
}
|
||||
|
||||
protected function hasError(string $jsonResponse): bool
|
||||
{
|
||||
return isset(json_decode($jsonResponse, true)['errorCode']);
|
||||
}
|
||||
|
||||
protected function getError(string $jsonResponse): string
|
||||
{
|
||||
return json_decode($jsonResponse, true)['errorDescription'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,12 +31,17 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
||||
public function download(string $projectUid, array $jobUids, string $filename): array
|
||||
{
|
||||
$url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile";
|
||||
$filenames = [];
|
||||
|
||||
$groupedJobUids = array_chunk($jobUids, 100);
|
||||
for ($i = 0; $i < count($groupedJobUids); $i++) {
|
||||
$apiReadyArray = $this->_convertUidArrayToApiRequest($groupedJobUids[$i]);
|
||||
$filenames[$i] = count($groupedJobUids) > 1 ? "{$i}_{$filename}" : $filename;
|
||||
$filecontent = $this->fetchApi->fetch('jsonPost', $url, $apiReadyArray);
|
||||
if ($this->hasError($filecontent)) {
|
||||
$errorMsg = $this->getError($filecontent);
|
||||
throw new \Exception("Error downloading file: {$errorMsg}", 1);
|
||||
}
|
||||
$this->_saveIntoFile($filenames[$i], $filecontent);
|
||||
}
|
||||
return $filenames;
|
||||
@@ -80,10 +85,17 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
||||
public function upload(string $filename, array $params): string
|
||||
{
|
||||
$urlParams = http_build_query($params);
|
||||
$fileContent = file_get_contents($filename);
|
||||
if ($fileContent === false) {
|
||||
throw new \Exception('File for upload inexistent or invalid', 1);
|
||||
try {
|
||||
$fileContent = file_get_contents($filename);
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception('File for upload inexistent or invalid: ' . $filename, 1);
|
||||
}
|
||||
return $this->fetchApi->fetch('put', $this->_url . "?{$urlParams}", [$fileContent]);
|
||||
|
||||
$response = $this->fetchApi->fetch('put', $this->_url . "?{$urlParams}", [$fileContent]);
|
||||
|
||||
if ($this->hasError($response)) {
|
||||
throw new \Exception("Error uploading file {$filename}: " . $this->getError($response), 1);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,30 +39,44 @@ class FetchApi
|
||||
$setopt = [];
|
||||
switch ($method) {
|
||||
case 'get':
|
||||
$this->checkAccessToken();
|
||||
$parameters = http_build_query($parameters);
|
||||
$url = $url . ($parameters ? '?'.$parameters : '');
|
||||
break;
|
||||
case 'put':
|
||||
$this->checkAccessToken();
|
||||
$setopt = $this->getPutParam()+$this->getPostParam(implode("", $parameters));
|
||||
break;
|
||||
case 'post':
|
||||
$this->checkAccessToken();
|
||||
$parameters = http_build_query($parameters);
|
||||
$setopt = $setopt + $this->getPostParam($parameters);
|
||||
break;
|
||||
case 'jsonPost':
|
||||
$this->checkAccessToken();
|
||||
$setopt = $this->getJsonPostParam($parameters);
|
||||
break;
|
||||
case 'download':
|
||||
$this->checkAccessToken();
|
||||
if (empty($filename)) {
|
||||
throw new Exception('You need to specify a filename to download a file.', 1);
|
||||
}
|
||||
$setopt = $this->getDownloadFileParam($filename)
|
||||
+ $this->getJsonPostParam($parameters);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception("Method {$method} is invalid on Fetch", 1);
|
||||
}
|
||||
return $this->curl($url, $setopt);
|
||||
}
|
||||
|
||||
private function checkAccessToken()
|
||||
{
|
||||
if (empty($this->token)) {
|
||||
throw new \Exception("Missing Access Token", 1);
|
||||
}
|
||||
}
|
||||
|
||||
private function getDownloadFileParam(string $filename)
|
||||
{
|
||||
return [
|
||||
@@ -98,10 +112,10 @@ class FetchApi
|
||||
protected function curl(string $url, array $curl_extra_setopt = [])
|
||||
{
|
||||
if (empty($url)) {
|
||||
throw new Exception('URL not defined', 1);
|
||||
throw new \Exception('URL not defined', 1);
|
||||
}
|
||||
|
||||
$header = $this->token ? ["Authorization: Bearer {$this->token}"] : [];
|
||||
$header = ($this->token ? ["Authorization: Bearer {$this->token}"] : []);
|
||||
$header = array_merge($header, $curl_extra_setopt[CURLOPT_HTTPHEADER]??[]);
|
||||
$curl_setopt = [
|
||||
CURLOPT_URL => $this->base_url . $url,
|
||||
|
||||
@@ -23,6 +23,10 @@ class Jobs extends \BrunoFontes\Memsource\BaseApi
|
||||
public function list(string $projectUid, array $parameters = []): string
|
||||
{
|
||||
$url = "/api2/v2/projects/{$projectUid}/jobs";
|
||||
return $this->fetchApi->fetch('get', $url, $parameters);
|
||||
$response = $this->fetchApi->fetch('get', $url, $parameters);
|
||||
if ($this->hasError($response)) {
|
||||
throw new \Exception("Error listing projects: " . $this->getError($response), 1);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,11 @@ class Project extends \BrunoFontes\Memsource\BaseApi
|
||||
*/
|
||||
public function list(array $queryParams = []): string
|
||||
{
|
||||
return $this->fetchApi->fetch('get', $this->_url, $queryParams);
|
||||
$response = $this->fetchApi->fetch('get', $this->_url, $queryParams);
|
||||
if ($this->hasError($response)) {
|
||||
throw new \Exception("Error listing projects: " . $this->getError($response), 1);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,6 +37,9 @@ class Project extends \BrunoFontes\Memsource\BaseApi
|
||||
*/
|
||||
public function get(string $projectUid): string
|
||||
{
|
||||
return $this->fetchApi->fetch('get', "{$this->_url}/{$projectUid}");
|
||||
$response = $this->fetchApi->fetch('get', "{$this->_url}/{$projectUid}");
|
||||
if ($this->hasError($response)) {
|
||||
throw new \Exception("Error getting project {$projectUid}: " . $this->getError($response), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user