mirror of
https://github.com/brunofontes/Memsource-API.git
synced 2025-04-19 18:37:12 +00:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
8d189ae541 | |||
a22f23cc10 | |||
adfea614ad | |||
1788befec6 | |||
228b3a06e1 | |||
aab66427a8 | |||
1a3c690138 | |||
f14492d744 | |||
b739763448 | |||
abda9e4ee8 | |||
c88478f7d8 | |||
37ebdabc20 | |||
92409b9571 | |||
8e14eb86b3 | |||
dc437d7044 | |||
51980ebf14 | |||
283e85b834 | |||
aaabb91039 | |||
e4f2bd04be | |||
97e0a56b65 | |||
13fadfe9ae | |||
6f19522c52 | |||
3c6d2f0442 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ tags
|
||||
vendor/
|
||||
.php_cs.cache
|
||||
*.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"
|
115
README.md
115
README.md
@ -4,53 +4,128 @@ I am creating this Memsource API as a way to learn how to deal with one and to u
|
||||
|
||||
There are other Memsource API repositories on GibHub that appears to be fully functional if you need it.
|
||||
|
||||
## Getting an Access Token
|
||||
## Installing
|
||||
|
||||
To be able to use the Memsource API, you need an **access token**, but to get it, you need to:
|
||||
Install it with [Composer](https://getcomposer.org/):
|
||||
|
||||
### Register as a developer on Memsource website
|
||||
1. Create a `composer.json` file with the following content:
|
||||
|
||||
So you will receive your:
|
||||
- *client id*
|
||||
- *client secret*
|
||||
```json
|
||||
{
|
||||
"repositories": [
|
||||
{
|
||||
"type": "vcs",
|
||||
"url": "https://github.com/brunofontes/Memsource-API"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"brunofontes/memsource-api": "*"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Get an Authorization Code
|
||||
2. Run `php composer.phar install`
|
||||
3. Add the following line on your .php file:
|
||||
|
||||
```php
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
```
|
||||
|
||||
## Using
|
||||
|
||||
This repository returns a JSON string for almost any command.
|
||||
If you are not sure how to use it, just convert it to an object or an array as follows:
|
||||
|
||||
```php
|
||||
$myObject = json_decode($response);
|
||||
$myArray = json_decode($response, true);
|
||||
```
|
||||
|
||||
### Create an instance
|
||||
|
||||
```php
|
||||
$memsource = new \BrunoFontes\Memsource();
|
||||
$url = $memsource->oauth()->getAuthorizationCodeUrl($cliend_id, $callback_uri);
|
||||
```
|
||||
|
||||
- If you have already an access token, just include it:
|
||||
|
||||
```php
|
||||
$memsource = new \BrunoFontes\Memsource($token);
|
||||
```
|
||||
|
||||
### Getting an Access Token
|
||||
|
||||
To be able to use the Memsource API, you need an **access token**. In order to get it, just follow the instructions below.
|
||||
|
||||
#### Register as a developer on Memsource website
|
||||
|
||||
So you will receive your:
|
||||
|
||||
- *client id*
|
||||
- *client secret*
|
||||
|
||||
#### Get an Authorization Code
|
||||
|
||||
```php
|
||||
$memsourceUrl = $memsource->oauth()->getAuthorizationCodeUrl($cliend_id, $callback_uri);
|
||||
```
|
||||
|
||||
Redirect your browser to this returned `$url` so the user can login via *oauth*.
|
||||
|
||||
The `$callback_uri` will be called by **Memsource** with a `$_GET['code']` that contains your Authorization Code, which you can use to...
|
||||
The `$callback_uri` will be called by *Memsource* with a `$_GET['code']` that contains your Authorization Code, which you can use to...
|
||||
|
||||
### Get an Access Token
|
||||
#### Get an Access Token
|
||||
|
||||
```php
|
||||
$authCode = $_GET['code'];
|
||||
$memsource = new \BrunoFontes\Memsource();
|
||||
$token = $memsource->oauth()->getAccessToken($authCode, $client_id, $client_secret, $callback_uri);
|
||||
```
|
||||
|
||||
Safely store this `$token` with the related user data and use it on any
|
||||
Safely store this `$token` with the related user data and use it to instantiate the class whenever it were necessary.
|
||||
|
||||
## Project
|
||||
### Project
|
||||
|
||||
### Project list
|
||||
#### Project list
|
||||
|
||||
To list all projects...
|
||||
To list all projects:
|
||||
|
||||
```php
|
||||
$memsource = new \BrunoFontes\Memsource();
|
||||
$projectList = $memsource->project()->listProjects;
|
||||
$projectList = $memsource->project()->list;
|
||||
```
|
||||
|
||||
To use filters, add the API filter as parâmeter:
|
||||
To use filters, add the API filter as parameter:
|
||||
|
||||
```php
|
||||
$projectList = $memsource->project()->listProjects(['name' => 'Project X']);
|
||||
$projectList = $memsource->project()->list(['name' => 'Project X']);
|
||||
```
|
||||
|
||||
## Bilingual Files
|
||||
#### Get Project
|
||||
|
||||
```php
|
||||
$projectList = $memsource->project()->get($projectUid);
|
||||
```
|
||||
|
||||
### Jobs
|
||||
|
||||
#### List Jobs
|
||||
|
||||
Only projectUid is essencial:
|
||||
|
||||
```php
|
||||
$jobs = $memsource->jobs()->list($projectUid, ['count' => true, 'filename' => 'my_file.html']);
|
||||
```
|
||||
|
||||
### Bilingual Files
|
||||
|
||||
#### Download Bilingual File
|
||||
|
||||
```php
|
||||
$memsource->BilingualFile()->download($projectUid, ['JobUid1', 'jobUid2'], 'download.mxliff');
|
||||
```
|
||||
|
||||
#### Upload Bilingual File
|
||||
|
||||
```php
|
||||
$parameters = ['format' => 'MXLF', 'saveToTransMemory' => 'None', 'setCompleted' => 'false'];
|
||||
$result = $api->bilingualFile()->upload('upload.mxliff', $parameters);
|
||||
```
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
11
pre-push.sh
Executable file
11
pre-push.sh
Executable file
@ -0,0 +1,11 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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'];
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
* @author Bruno Fontes <developer@brunofontes.net>
|
||||
* @link https://github.com/brunofontes
|
||||
*/
|
||||
|
||||
namespace BrunoFontes\Memsource;
|
||||
|
||||
class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
||||
@ -27,16 +28,24 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
||||
*
|
||||
* @return array A list of the downloaded files
|
||||
*/
|
||||
public function downloadBilingualFile(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 = [];
|
||||
|
||||
$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);
|
||||
$this->_saveIntoFile($filenames[$i], $filecontent);
|
||||
if ($this->hasError($filecontent)) {
|
||||
$errorMsg = $this->getError($filecontent);
|
||||
throw new \Exception("Error downloading file: {$errorMsg}", 1);
|
||||
}
|
||||
Helper::saveIntoFile($filenames[$i], $filecontent);
|
||||
}
|
||||
return $filenames;
|
||||
}
|
||||
@ -57,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
|
||||
*
|
||||
@ -76,13 +74,20 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
||||
*
|
||||
* @return string The http request responde from API in json format
|
||||
*/
|
||||
public function uploadBilingualFile(string $filename, array $params): string
|
||||
public function upload(string $filename, array $params): string
|
||||
{
|
||||
$urlParams = http_build_query($params);
|
||||
try {
|
||||
$fileContent = file_get_contents($filename);
|
||||
if ($fileContent === false) {
|
||||
throw new \Exception('File for upload inexistent or invalid', 1);
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
@ -12,13 +12,18 @@ class FetchApi
|
||||
protected $base_url;
|
||||
protected $token;
|
||||
|
||||
public function getBase_url()
|
||||
{
|
||||
return $this->base_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* BaseAPI needs at least the Memsource Token to use it's API
|
||||
*
|
||||
* @param string $token
|
||||
* @param string $memsourceBaseUrl [Optional] A non-standard Memsource URL base for the API to work
|
||||
*/
|
||||
public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web')
|
||||
public function __construct(string $token = null, string $memsourceBaseUrl)
|
||||
{
|
||||
$this->base_url = $memsourceBaseUrl;
|
||||
$this->token = $token;
|
||||
@ -27,7 +32,7 @@ class FetchApi
|
||||
/**
|
||||
* Fetch API data using curl
|
||||
*
|
||||
* @param string $method Should be 'get', 'post', 'jsonPost' or 'download'
|
||||
* @param string $method Should be 'get', 'put', 'post', 'jsonPost', 'download' or 'raw'
|
||||
* @param string $url The api url
|
||||
* @param array $parameters Array ['key' => 'value'] of get or post fields or structured array for json requests
|
||||
* @param string $filename [optional] Specified file in which the download request will be saved
|
||||
@ -39,30 +44,47 @@ 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;
|
||||
case 'raw':
|
||||
$setopt = $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 +120,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,
|
||||
|
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
44
src/Jobs.php
44
src/Jobs.php
@ -10,7 +10,6 @@ namespace BrunoFontes\Memsource;
|
||||
|
||||
class Jobs extends \BrunoFontes\Memsource\BaseApi
|
||||
{
|
||||
|
||||
/**
|
||||
* List jobs of a project
|
||||
* The API request returns a MAX of 50 Jobs.
|
||||
@ -21,9 +20,48 @@ class Jobs extends \BrunoFontes\Memsource\BaseApi
|
||||
*
|
||||
* @return string The JSON answer from Memsource
|
||||
*/
|
||||
public function listJobs(string $projectUid, array $parameters = []): string
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,14 @@
|
||||
|
||||
namespace BrunoFontes;
|
||||
|
||||
use \BrunoFontes\Memsource\BilingualFile;
|
||||
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
|
||||
*
|
||||
@ -20,51 +28,72 @@ 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')
|
||||
{
|
||||
$this->_fetchApi = new \BrunoFontes\Memsource\FetchApi($token, $memsourceBaseUrl);
|
||||
$this->_fetchApi = new FetchApi($token, $memsourceBaseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Memsource Oauth functions
|
||||
*
|
||||
* @return \BrunoFontes\Memsource\Oauth
|
||||
* @return Oauth
|
||||
*/
|
||||
public function oauth(): \BrunoFontes\Memsource\Oauth
|
||||
public function oauth(): Oauth
|
||||
{
|
||||
return $this->_oauth ?? $this->_oauth = new \BrunoFontes\Memsource\oauth();
|
||||
return $this->_oauth ?? $this->_oauth = new oauth($this->_fetchApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Memsource API BilingualFile related functions
|
||||
*
|
||||
* @return \BrunoFontes\Memsource\BilingualFile
|
||||
* @return BilingualFile
|
||||
*/
|
||||
public function bilingualFile(): \BrunoFontes\Memsource\BilingualFile
|
||||
public function bilingualFile(): BilingualFile
|
||||
{
|
||||
return $this->_bilingualFile ?? $this->_bilingualFile = new \BrunoFontes\Memsource\BilingualFile($this->_fetchApi);
|
||||
return $this->_bilingualFile ?? $this->_bilingualFile = new BilingualFile($this->_fetchApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Memsource API Jobs related functions
|
||||
*
|
||||
* @return \BrunoFontes\Memsource\Jobs
|
||||
* @return Jobs
|
||||
*/
|
||||
public function jobs(): \BrunoFontes\Memsource\Jobs
|
||||
public function jobs(): Jobs
|
||||
{
|
||||
return $this->_jobs ?? $this->_jobs = new \BrunoFontes\Memsource\Jobs($this->_fetchApi);
|
||||
return $this->_jobs ?? $this->_jobs = new Jobs($this->_fetchApi);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Memsource API Project related functions
|
||||
*
|
||||
* @return \BrunoFontes\Memsource\Project
|
||||
* @return Project
|
||||
*/
|
||||
public function project(): \BrunoFontes\Memsource\Project
|
||||
public function project(): Project
|
||||
{
|
||||
return $this->_project ?? $this->_project = new \BrunoFontes\Memsource\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);
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,10 @@
|
||||
* @author Bruno Fontes <developer@brunofontes.net>
|
||||
* @link https://github.com/brunofontes
|
||||
*/
|
||||
|
||||
namespace BrunoFontes\Memsource;
|
||||
|
||||
class Oauth extends \BrunoFontes\Memsource
|
||||
class Oauth extends \BrunoFontes\Memsource\BaseApi
|
||||
{
|
||||
private $_url = '/oauth';
|
||||
|
||||
@ -22,8 +23,16 @@ class Oauth extends \BrunoFontes\Memsource
|
||||
*/
|
||||
public function getAuthorizationCodeUrl(string $client_id, string $callback_uri)
|
||||
{
|
||||
$authorize_url = $this->base_url . $this->_url . '/authorize';
|
||||
return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
|
||||
$authorize_url = $this->fetchApi->getBase_url() . $this->_url . '/authorize';
|
||||
$parambeters = http_build_query(
|
||||
[
|
||||
"response_type" => 'code',
|
||||
"client_id" => $client_id,
|
||||
"redirect_uri" => $callback_uri,
|
||||
"scope" => 'openid'
|
||||
]
|
||||
);
|
||||
return "{$authorize_url}?{$parambeters}";
|
||||
}
|
||||
|
||||
public function getAccessToken(string $authorization_code, string $client_id, string $client_secret, string $callback_uri)
|
||||
@ -37,10 +46,12 @@ class Oauth extends \BrunoFontes\Memsource
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => $content
|
||||
];
|
||||
$response = $this->curl($token_url, $params);
|
||||
if ($respose['error']) {
|
||||
throw new Exception("Error getting access token", 1);
|
||||
$response = json_decode($this->fetchApi->fetch('raw', $token_url, $params), true);
|
||||
|
||||
if (isset($response['error'])) {
|
||||
throw new \Exception("Error getting TOKEN: " . $response['error_description'], 1);
|
||||
}
|
||||
|
||||
return $response['access_token'];
|
||||
}
|
||||
}
|
@ -19,9 +19,13 @@ class Project extends \BrunoFontes\Memsource\BaseApi
|
||||
*
|
||||
* @return string The JSON answer from Memsource
|
||||
*/
|
||||
public function listProjects(array $queryParams = []): string
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,8 +35,26 @@ class Project extends \BrunoFontes\Memsource\BaseApi
|
||||
*
|
||||
* @return string A json string with all project info
|
||||
*/
|
||||
public function getProject(string $projectUid): string
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
37
tests/bilingualFileTest.php
Normal file
37
tests/bilingualFileTest.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use BrunoFontes\Memsource;
|
||||
|
||||
final class BilingualFileTest extends TestCase
|
||||
{
|
||||
public function testEmptyDownloadJobUidListShouldReturnEmptyFilenames()
|
||||
{
|
||||
$api = new Memsource('fakeToken');
|
||||
$this->assertEquals(
|
||||
[],
|
||||
$api->bilingualFile()->download('uid', [], 'filename.xliff')
|
||||
);
|
||||
}
|
||||
public function testInvalidDownloadUidsShouldThrowError()
|
||||
{
|
||||
$api = new Memsource('fakeToken');
|
||||
$this->expectException(\Exception::class);
|
||||
$api->bilingualFile()->download('uid', ['a'], 'filename.xliff');
|
||||
}
|
||||
|
||||
public function testUploadInexistentFileShouldThrowError()
|
||||
{
|
||||
$api = new Memsource('fakeToken');
|
||||
$this->expectException(\Exception::class);
|
||||
$api->bilingualFile()->upload('myInvalidFile', []);
|
||||
}
|
||||
|
||||
public function testUploadWithNoTokenShouldThrowError()
|
||||
{
|
||||
$api = new Memsource('fakeToken');
|
||||
$this->expectException(\Exception::class);
|
||||
$api->bilingualFile()->upload('tests/bilingualFileTest.php', []);
|
||||
}
|
||||
}
|
57
tests/fetchApiTest.php
Normal file
57
tests/fetchApiTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use BrunoFontes\Memsource\FetchApi;
|
||||
|
||||
final class FetchApiTest extends TestCase
|
||||
{
|
||||
public function testEmptyFetchUrlShouldThrowError()
|
||||
{
|
||||
$fetch = new FetchApi('fakeToken', 'https://google.com');
|
||||
$this->expectExceptionMessage('URL not defined');
|
||||
$this->assertNotEmpty($fetch->fetch('get', ''));
|
||||
}
|
||||
|
||||
public function testNotEmptyTokenOnFetchShouldNotThrowError()
|
||||
{
|
||||
$fetch = new FetchApi('fakeToken', 'https://google.com');
|
||||
$this->assertNotEmpty($fetch->fetch('get', '/'));
|
||||
}
|
||||
public function testEmptyTokenOnFetchRawShouldNotThrowError()
|
||||
{
|
||||
$fetch = new FetchApi(null, 'http://google.com');
|
||||
$this->assertNotEmpty($fetch->fetch('raw', '/'));
|
||||
}
|
||||
|
||||
public function testEmptyTokenOnFetchGetShouldThrowError()
|
||||
{
|
||||
$fetch = new FetchApi(null, 'http://testUrl.com');
|
||||
$this->expectExceptionMessage('Missing Access Token');
|
||||
$fetch->fetch('get', 'url');
|
||||
}
|
||||
public function testEmptyTokenOnFetchPutShouldThrowError()
|
||||
{
|
||||
$fetch = new FetchApi(null, 'http://testUrl.com');
|
||||
$this->expectExceptionMessage('Missing Access Token');
|
||||
$fetch->fetch('put', 'url');
|
||||
}
|
||||
public function testEmptyTokenOnFetchPostShouldThrowError()
|
||||
{
|
||||
$fetch = new FetchApi(null, 'http://testUrl.com');
|
||||
$this->expectExceptionMessage('Missing Access Token');
|
||||
$fetch->fetch('post', 'url');
|
||||
}
|
||||
public function testEmptyTokenOnFetchJsonPostShouldThrowError()
|
||||
{
|
||||
$fetch = new FetchApi(null, 'http://testUrl.com');
|
||||
$this->expectExceptionMessage('Missing Access Token');
|
||||
$fetch->fetch('jsonPost', 'url');
|
||||
}
|
||||
public function testEmptyTokenOnFetchDownloadShouldThrowError()
|
||||
{
|
||||
$fetch = new FetchApi(null, 'http://testUrl.com');
|
||||
$this->expectExceptionMessage('Missing Access Token');
|
||||
$fetch->fetch('download', 'url');
|
||||
}
|
||||
}
|
33
tests/jobsTest.php
Normal file
33
tests/jobsTest.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use BrunoFontes\Memsource;
|
||||
|
||||
final class JobsTest extends TestCase
|
||||
{
|
||||
public function testInvalidProjectUidShouldThrowError()
|
||||
{
|
||||
$api = new Memsource('fakeToken');
|
||||
$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')
|
||||
);
|
||||
}
|
||||
}
|
34
tests/oauthTest.php
Normal file
34
tests/oauthTest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use BrunoFontes\Memsource;
|
||||
|
||||
final class oauthTest extends TestCase
|
||||
{
|
||||
public function testOauthGetAuthorizationCodeUrl()
|
||||
{
|
||||
$customServerURL = 'http://myPersonalMemsource.com';
|
||||
$api = new Memsource(null, $customServerURL);
|
||||
$expected = $customServerURL . '/oauth/authorize?response_type=code&client_id=id&redirect_uri=http%3A%2F%2Furi&scope=openid';
|
||||
$memsourceUrl = $api->oauth()->getAuthorizationCodeUrl('id', 'http://uri');
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$memsourceUrl
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetAccessTokenExceptionOnFakeCode()
|
||||
{
|
||||
$api = new Memsource();
|
||||
$this->expectException(\Exception::class);
|
||||
$token = $api->oauth()->getAccessToken('fakeCode', 'fakeId', 'fakePass', 'http://any');
|
||||
}
|
||||
|
||||
public function testGetAccessTokenExceptionOnEmptyCode()
|
||||
{
|
||||
$api = new Memsource();
|
||||
$this->expectException(\Exception::class);
|
||||
$token = $api->oauth()->getAccessToken('', '', '', '');
|
||||
}
|
||||
}
|
22
tests/projectTest.php
Normal file
22
tests/projectTest.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use BrunoFontes\Memsource;
|
||||
|
||||
final class ProjectTest extends TestCase
|
||||
{
|
||||
public function testNoTokenShouldThrowError()
|
||||
{
|
||||
$api = new Memsource();
|
||||
$this->expectExceptionMessage('Missing Access Token');
|
||||
$api->project()->list();
|
||||
}
|
||||
|
||||
public function testInvalidProjectUidShouldThrowError()
|
||||
{
|
||||
$api = new Memsource('fakeToken');
|
||||
$this->expectException(\Exception::class);
|
||||
$api->project()->get('invalidProjectUid');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user