mirror of
https://github.com/brunofontes/Memsource-API.git
synced 2025-12-15 13:32:09 -03:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
8d189ae541
|
|||
|
a22f23cc10
|
|||
|
adfea614ad
|
|||
|
1788befec6
|
|||
|
228b3a06e1
|
|||
|
aab66427a8
|
|||
|
1a3c690138
|
|||
|
f14492d744
|
|||
|
b739763448
|
|||
|
abda9e4ee8
|
|||
|
c88478f7d8
|
|||
| 37ebdabc20 | |||
| 92409b9571 | |||
| 8e14eb86b3 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ tags
|
|||||||
vendor/
|
vendor/
|
||||||
.php_cs.cache
|
.php_cs.cache
|
||||||
*.mxliff
|
*.mxliff
|
||||||
|
composer.lock
|
||||||
|
|||||||
13
Makefile
Normal file
13
Makefile
Normal 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"
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
"name": "brunofontes/memsource-api",
|
"name": "brunofontes/memsource-api",
|
||||||
"description": "A personal memsource api to better understand how it works",
|
"description": "A personal memsource api to better understand how it works",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
|
"version": "1.2",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Bruno F. Fontes",
|
"name": "Bruno F. Fontes",
|
||||||
@@ -20,5 +21,8 @@
|
|||||||
"type": "vcs",
|
"type": "vcs",
|
||||||
"url": "https://github.com/brunofontes/Memsource-API"
|
"url": "https://github.com/brunofontes/Memsource-API"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^8"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
pre-push.sh
Normal file → Executable file
12
pre-push.sh
Normal file → Executable 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
32
src/Async.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -30,19 +30,22 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
|||||||
*/
|
*/
|
||||||
public function download(string $projectUid, array $jobUids, string $filename): array
|
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 = [];
|
$filenames = [];
|
||||||
|
|
||||||
$groupedJobUids = array_chunk($jobUids, 100);
|
$groupedJobUids = array_chunk($jobUids, 100);
|
||||||
for ($i = 0; $i < count($groupedJobUids); $i++) {
|
for ($i = 0; $i < count($groupedJobUids); $i++) {
|
||||||
$apiReadyArray = $this->_convertUidArrayToApiRequest($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);
|
$filecontent = $this->fetchApi->fetch('jsonPost', $url, $apiReadyArray);
|
||||||
if ($this->hasError($filecontent)) {
|
if ($this->hasError($filecontent)) {
|
||||||
$errorMsg = $this->getError($filecontent);
|
$errorMsg = $this->getError($filecontent);
|
||||||
throw new \Exception("Error downloading file: {$errorMsg}", 1);
|
throw new \Exception("Error downloading file: {$errorMsg}", 1);
|
||||||
}
|
}
|
||||||
$this->_saveIntoFile($filenames[$i], $filecontent);
|
Helper::saveIntoFile($filenames[$i], $filecontent);
|
||||||
}
|
}
|
||||||
return $filenames;
|
return $filenames;
|
||||||
}
|
}
|
||||||
@@ -63,17 +66,6 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
|||||||
return ['jobs' => $convertedArray];
|
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
|
* Upload a bilingual file to Memsource
|
||||||
*
|
*
|
||||||
|
|||||||
19
src/Helper.php
Normal file
19
src/Helper.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
src/Jobs.php
35
src/Jobs.php
@@ -29,4 +29,39 @@ class Jobs extends \BrunoFontes\Memsource\BaseApi
|
|||||||
}
|
}
|
||||||
return $response;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ use \BrunoFontes\Memsource\FetchApi;
|
|||||||
use \BrunoFontes\Memsource\Jobs;
|
use \BrunoFontes\Memsource\Jobs;
|
||||||
use \BrunoFontes\Memsource\Oauth;
|
use \BrunoFontes\Memsource\Oauth;
|
||||||
use \BrunoFontes\Memsource\Project;
|
use \BrunoFontes\Memsource\Project;
|
||||||
|
use \BrunoFontes\Memsource\TM;
|
||||||
|
use \BrunoFontes\Memsource\Async;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Memsource API class
|
* Memsource API class
|
||||||
@@ -26,6 +28,8 @@ class Memsource
|
|||||||
private $_bilingualFile;
|
private $_bilingualFile;
|
||||||
private $_jobs;
|
private $_jobs;
|
||||||
private $_project;
|
private $_project;
|
||||||
|
private $_tm;
|
||||||
|
private $_async;
|
||||||
private $_fetchApi;
|
private $_fetchApi;
|
||||||
|
|
||||||
public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web')
|
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);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,4 +42,19 @@ class Project extends \BrunoFontes\Memsource\BaseApi
|
|||||||
throw new \Exception("Error getting project {$projectUid}: " . $this->getError($response), 1);
|
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
74
src/TM.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,14 +11,14 @@ final class BilingualFileTest extends TestCase
|
|||||||
$api = new Memsource('fakeToken');
|
$api = new Memsource('fakeToken');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
[],
|
[],
|
||||||
$api->bilingualFile()->download('uid', [], 'filename')
|
$api->bilingualFile()->download('uid', [], 'filename.xliff')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
public function testInvalidDownloadUidsShouldThrowError()
|
public function testInvalidDownloadUidsShouldThrowError()
|
||||||
{
|
{
|
||||||
$api = new Memsource('fakeToken');
|
$api = new Memsource('fakeToken');
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
$api->bilingualFile()->download('uid', ['a'], 'filename');
|
$api->bilingualFile()->download('uid', ['a'], 'filename.xliff');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUploadInexistentFileShouldThrowError()
|
public function testUploadInexistentFileShouldThrowError()
|
||||||
|
|||||||
@@ -12,4 +12,22 @@ final class JobsTest extends TestCase
|
|||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
$api->jobs()->list('invalidProjectUid', []);
|
$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')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user