14 Commits

Author SHA1 Message Date
8d189ae541 fix(TM): List is now working again
It was referencing the new memsource API url (v2), but list still uses
v1.
2022-12-30 10:50:14 -03:00
a22f23cc10 test: adding phpUnit 2022-12-23 00:27:51 -03:00
adfea614ad feat: added new Async and TM modules on main file 2022-12-23 00:26:18 -03:00
1788befec6 refactor: moving saveIntoFile to Helper function 2022-12-23 00:25:50 -03:00
228b3a06e1 feat: adding TM module 2022-12-23 00:25:12 -03:00
aab66427a8 refactor: creating Helper class and moving saveIntoFile function 2022-12-23 00:24:41 -03:00
1a3c690138 feat: added async module 2022-12-23 00:24:24 -03:00
f14492d744 Include edit Project Status 2021-02-12 19:03:12 -03:00
b739763448 Include job download source and target files 2021-02-12 19:02:25 -03:00
abda9e4ee8 Fix the pre-push hook error with different phpunit versions
Now the pre-push hook check first for a composer phpunit, it not found,
try to use a system wide one. If none of them are available, it will
rise an error
2021-02-11 18:29:32 -03:00
c88478f7d8 Includes file extension on bilingual download
It is important to choose the file extension, so including it on the
filename would be the easier way for us, as we already have the
extension when using it.
2021-02-11 18:25:32 -03:00
37ebdabc20 Improving Makefile and PHPUNIT dependency on composer.json 2019-06-28 02:21:55 -03:00
92409b9571 Avoiding composer.lock to be commited 2019-06-28 00:58:33 -03:00
8e14eb86b3 Added Makefile to install the hook for automatic test 2019-06-28 00:54:06 -03:00
13 changed files with 255 additions and 18 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ tags
vendor/
.php_cs.cache
*.mxliff
composer.lock

13
Makefile Normal file
View File

@@ -0,0 +1,13 @@
install:
command -v composer && composer install || echo "Please, install Composer to use tests"
command -v phpunit || command -v composer && composer require --dev phpunit/phpunit ^8
chmod +x ./pre-push.sh
mkdir -p .git/hooks
ln -fs ../../pre-push.sh .git/hooks/pre-push
uninstall:
rm -f ./.git/hooks/pre-push
if [ -d "./vendor/phpunit" ] ; then composer remove --dev phpunit/phpunit; fi
check:
gaze '{src,tests}/**/*.php' -c "./vendor/phpunit/phpunit/phpunit --bootstrap vendor/autoload.php tests --testdox --color"

View File

@@ -2,6 +2,7 @@
"name": "brunofontes/memsource-api",
"description": "A personal memsource api to better understand how it works",
"license": "GPL-3.0-or-later",
"version": "1.2",
"authors": [
{
"name": "Bruno F. Fontes",
@@ -20,5 +21,8 @@
"type": "vcs",
"url": "https://github.com/brunofontes/Memsource-API"
}
]
],
"require-dev": {
"phpunit/phpunit": "^8"
}
}

12
pre-push.sh Normal file → Executable file
View File

@@ -1 +1,11 @@
phpunit --bootstrap vendor/autoload.php tests --testdox --color
params="--bootstrap vendor/autoload.php tests --testdox --color"
if [ -d ./vendor/phpunit ]; then
./vendor/phpunit/phpunit/phpunit $params
else
if [ $(command -v phpunit) ]; then
phpunit $params
else
echo "Please, run 'make install' or install phpunit globally to run the tests"
fi
fi

32
src/Async.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class Async extends \BrunoFontes\Memsource\BaseApi
{
private $_url = '/api2/v1/async';
/**
* Get asynchronous request
*
* @param string $queryParams An array with the Query parameters to filter projects
*
* @return string The JSON answer from Memsource
*/
public function getAsyncRequest(string $asyncRequestId, array $queryParams = []): string
{
$response = $this->fetchApi->fetch('get', "{$this->_url}/{$asyncRequestId}", $queryParams);
if ($this->hasError($response)) {
throw new \Exception("Error listing AsyncRequest: " . $this->getError($response), 1);
}
return $response;
}
}

View File

@@ -30,19 +30,22 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
*/
public function download(string $projectUid, array $jobUids, string $filename): array
{
$url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile";
$fileExtension = pathinfo($filename)['extension'];
$fileNoExtension = basename($filename,'.'.$fileExtension);
;
$url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile?format=" . strtoupper($fileExtension);
$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;
$filenames[$i] = count($groupedJobUids) > 1 ? "{$fileNoExtension}_{$i}.{$fileExtension}" : $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);
Helper::saveIntoFile($filenames[$i], $filecontent);
}
return $filenames;
}
@@ -63,17 +66,6 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
return ['jobs' => $convertedArray];
}
private function _saveIntoFile(string $filename, string $filecontent): void
{
try {
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
} catch (\Exception $e) {
throw new \Exception("File could not be saved: {$e->error}", 1);
}
}
/**
* Upload a bilingual file to Memsource
*

19
src/Helper.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace BrunoFontes\Memsource;
class Helper
{
public static function saveIntoFile(string $filename, string $filecontent): void
{
try {
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
} catch (\Exception $e) {
throw new \Exception("File could not be saved: {$e->error}", 1);
}
}
}

View File

@@ -29,4 +29,39 @@ class Jobs extends \BrunoFontes\Memsource\BaseApi
}
return $response;
}
/**
* Download a target file
*
* @param string $projectUid The project uid which contain the jobs
* @param string $jobUid Job uid to be download
* @param string $filename Filename of the saved file
* @param string $format File format: ORIGINAL or PDF
*/
public function downloadTargetFile(string $projectUid, string $jobUid, string $filename, string $format = "ORIGINAL")
{
$url = "/api2/v1/projects/{$projectUid}/jobs/{$jobUid}/targetFile";
$filecontent = $this->fetchApi->fetch('get', $url);
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
}
/**
* Download a source file
*
* @param string $projectUid The project uid which contain the jobs
* @param string $jobUid Job uid to be download
*/
public function downloadOriginalFile(string $projectUid, string $jobUid, string $filename)
{
$url = "/api2/v1/projects/{$projectUid}/jobs/{$jobUid}/original";
$filecontent = $this->fetchApi->fetch('get', $url);
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
}
}

View File

@@ -13,6 +13,8 @@ use \BrunoFontes\Memsource\FetchApi;
use \BrunoFontes\Memsource\Jobs;
use \BrunoFontes\Memsource\Oauth;
use \BrunoFontes\Memsource\Project;
use \BrunoFontes\Memsource\TM;
use \BrunoFontes\Memsource\Async;
/**
* Memsource API class
@@ -26,6 +28,8 @@ class Memsource
private $_bilingualFile;
private $_jobs;
private $_project;
private $_tm;
private $_async;
private $_fetchApi;
public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web')
@@ -72,4 +76,24 @@ class Memsource
{
return $this->_project ?? $this->_project = new Project($this->_fetchApi);
}
/**
* Memsource API Async related functions
*
* @return Async
*/
public function async(): Async
{
return $this->_async ?? $this->_async = new Async($this->_fetchApi);
}
/**
* Memsource API TM related functions
*
* @return TM
*/
public function tm(): TM
{
return $this->_tm ?? $this->_tm = new TM($this->_fetchApi);
}
}

View File

@@ -42,4 +42,19 @@ class Project extends \BrunoFontes\Memsource\BaseApi
throw new \Exception("Error getting project {$projectUid}: " . $this->getError($response), 1);
}
}
/**
* Edit the project status
*
* @param string $projectUid The project UID
* @param string $status The new status
*/
public function editStatus(string $projectUid, string $status): void
{
$queryParam = ['status' => $status];
$response = $this->fetchApi->fetch('jsonPost', "{$this->_url}/{$projectUid}/setStatus", $queryParam);
if ($this->hasError($response)) {
throw new \Exception("Error editing project status on project: {$projectUid}: " . $this->getError($response), 1);
}
}
}

74
src/TM.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
enum ExportFormat
{
case TMX;
case XLSX;
}
class TM extends \BrunoFontes\Memsource\BaseApi
{
private $_url = '/api2/v2/transMemories';
/**
* List projects
*
* @param string $queryParams An array with the Query parameters to filter projects
*
* @return string The JSON answer from Memsource
*/
public function list(array $queryParams = []): string
{
$response = $this->fetchApi->fetch('get', '/api2/v1/transMemories/', $queryParams);
if ($this->hasError($response)) {
throw new \Exception("Error listing TMs: " . $this->getError($response), 1);
}
return $response;
}
/**
* Export a TM in an async way
*
* @param string $tmUid The TM UID
* @param array[string] $targetLangs The language pairs to export
*
* @return string A json string with all translation memories info
*/
public function export(string $tmUid, array $targetLangs): string
{
$queryParam['exportTargetlangs'] = $targetLangs;
$response = $this->fetchApi->fetch('jsonPost', "{$this->_url}/{$tmUid}/export", $queryParam);
if ($this->hasError($response)) {
throw new \Exception("Error getting tm {$tmUid}: " . $this->getError($response), 1);
}
return $response;
}
/**
* Download a TM. You need to export the TM first in order to obtain the
* asyncExport id
*
* @param string $asyncId The asyncId obtainable by export function
* @param ExportFormat $format The file format that will be exported
* @param string $filename The filename that will be created to store the
* downloaded TM
*/
public function download(string $asyncId, string $filename, ExportFormat $fileFormat = ExportFormat::TMX)
{
$queryParam['format'] = $fileFormat->name;
$filecontent = $this->fetchApi->fetch('get', "/api2/v1/transMemories/downloadExport/{$asyncId}/", $queryParam);
if ($this->hasError($filecontent)) {
throw new \Exception("Error downloading TM asyncID {$asyncId}: " . $this->getError($filecontent), 1);
}
Helper::saveIntoFile($filename, $filecontent);
}
}

View File

@@ -11,14 +11,14 @@ final class BilingualFileTest extends TestCase
$api = new Memsource('fakeToken');
$this->assertEquals(
[],
$api->bilingualFile()->download('uid', [], 'filename')
$api->bilingualFile()->download('uid', [], 'filename.xliff')
);
}
public function testInvalidDownloadUidsShouldThrowError()
{
$api = new Memsource('fakeToken');
$this->expectException(\Exception::class);
$api->bilingualFile()->download('uid', ['a'], 'filename');
$api->bilingualFile()->download('uid', ['a'], 'filename.xliff');
}
public function testUploadInexistentFileShouldThrowError()

View File

@@ -12,4 +12,22 @@ final class JobsTest extends TestCase
$this->expectException(\Exception::class);
$api->jobs()->list('invalidProjectUid', []);
}
public function testDownloadTargetFileReturnsNull()
{
$api = new Memsource('fakeToken');
$this->assertEquals(
null,
$api->jobs()->downloadTargetFile('projUid', 'jobUid', 'filename.xliff')
);
}
public function testDownloadOriginalFileReturnsNull()
{
$api = new Memsource('fakeToken');
$this->assertEquals(
null,
$api->jobs()->downloadOriginalFile('projUid', 'jobUid', 'filename.xliff')
);
}
}