class LingotekApi in Lingotek Translation 3.4.x
Same name and namespace in other branches
- 8 src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 8.2 src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 4.0.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.0.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.1.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.2.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.3.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.5.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.6.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.7.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
- 3.8.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi
A simple connector to the Lingotek Translation API.
Hierarchy
- class \Drupal\lingotek\Remote\LingotekApi implements LingotekApiInterface
Expanded class hierarchy of LingotekApi
1 file declares its use of LingotekApi
- LingotekApiUnitTest.php in tests/
src/ Unit/ Remote/ LingotekApiUnitTest.php
1 string reference to 'LingotekApi'
1 service uses LingotekApi
File
- src/
Remote/ LingotekApi.php, line 13
Namespace
Drupal\lingotek\RemoteView source
class LingotekApi implements LingotekApiInterface {
/**
* The HTTP client to interact with the Lingotek service.
*
* @var \Drupal\lingotek\Remote\LingotekHttpInterface
*/
protected $lingotekClient;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs a LingotekApi object.
*
* @param \Drupal\lingotek\Remote\LingotekHttpInterface $client
* A http client.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
*/
public function __construct(LingotekHttpInterface $client, LoggerInterface $logger) {
$this->lingotekClient = $client;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('lingotek.http_client'), $container
->get('logger.channel.lingotek'));
}
/**
* {@inheritdoc}
*/
public function getLocales() {
$this->logger
->debug('Starting Locales request: /api/locale with args [limit => 1000]');
/** @var \Psr\Http\Message\ResponseInterface $response */
try {
$response = $this->lingotekClient
->get('/api/locale', [
'limit' => 1000,
]);
if ($response
->getStatusCode() == Response::HTTP_OK) {
$data = json_decode($response
->getBody(), TRUE);
$this->logger
->debug('getLocales response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $data;
}
} catch (\Exception $e) {
$this->logger
->error('Error requesting locales: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Error requesting locales: ' . $e
->getMessage());
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getAccountInfo() {
try {
$access_token = $this->lingotekClient
->getCurrentToken();
$trimmed_token = $access_token ? substr($access_token, 0, 8) . '…' : '';
$this->logger
->debug('Starting account info request: /auth/oauth2/access_token_info Token: %token', [
'%token' => $trimmed_token,
]);
$response = $this->lingotekClient
->get('/auth/oauth2/access_token_info');
} catch (\Exception $e) {
$this->logger
->error('Error requesting account info: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get account info: ' . $e
->getMessage());
}
$this->logger
->debug('getAccountInfo response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function addDocument($args) {
try {
$this->logger
->debug('Lingotek::addDocument (POST /api/document) called with ' . var_export($args, TRUE));
$response = $this->lingotekClient
->post('/api/document', $args, TRUE);
} catch (\Exception $e) {
$this->logger
->error('Error adding document: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Error adding document: ' . $e
->getMessage());
}
if ($response
->getStatusCode() == Response::HTTP_ACCEPTED) {
$data = json_decode($response
->getBody(), TRUE);
$this->logger
->debug('addDocument response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
if (!empty($data['properties']['id'])) {
return $response;
}
}
// TODO: log warning
return $response;
}
/**
* {@inheritdoc}
*/
public function patchDocument($id, $args) {
try {
$this->logger
->debug('Lingotek::patchDocument (PATCH /api/document) called with id %id and args %args', [
'%id' => $id,
'%args' => var_export($args, TRUE),
]);
$response = $this->lingotekClient
->patch('/api/document/' . $id, $args, TRUE);
} catch (\Exception $e) {
$this->logger
->error('Error updating document: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to patch (update) document: ' . $e
->getMessage());
}
$this->logger
->debug('patchDocument response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function cancelDocument($id) {
try {
$this->logger
->debug('Lingotek::cancelDocument called with id ' . $id);
$args = [
'id' => $id,
'cancelled_reason' => 'CANCELLED_BY_AUTHOR',
];
$response = $this->lingotekClient
->post('/api/document/' . $id . '/cancel', $args);
} catch (\Exception $e) {
$http_status_code = $e
->getCode();
if ($http_status_code === Response::HTTP_NOT_FOUND) {
$this->logger
->error('Error cancelling document: %message.', [
'%message' => $e
->getMessage(),
]);
return new Response($e
->getMessage(), Response::HTTP_NOT_FOUND);
}
$this->logger
->error('Error cancelling document: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to cancel document: ' . $e
->getMessage(), $http_status_code, $e);
}
$this->logger
->debug('cancelDocument response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function cancelDocumentTarget($document_id, $locale) {
try {
$this->logger
->debug('Lingotek::cancelDocumentTarget called with id ' . $document_id . ' and locale ' . $locale);
$args = [
'id' => $document_id,
'locale' => $locale,
'cancelled_reason' => 'CANCELLED_BY_AUTHOR',
'mark_invoiceable' => 'true',
];
$response = $this->lingotekClient
->post('/api/document/' . $document_id . '/translation/' . $locale . '/cancel', $args);
} catch (\Exception $e) {
$http_status_code = $e
->getCode();
if ($http_status_code === Response::HTTP_NOT_FOUND) {
$this->logger
->error('Error cancelling document target: %message.', [
'%message' => $e
->getMessage(),
]);
return new Response($e
->getMessage(), Response::HTTP_NOT_FOUND);
}
$this->logger
->error('Error cancelling document target: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to cancel document target: ' . $e
->getMessage(), $http_status_code, $e);
}
$this->logger
->debug('cancelDocumentTarget response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getDocumentContent($doc_id) {
try {
$this->logger
->debug('Lingotek::getDocumentContent called with id ' . $doc_id);
$response = $this->lingotekClient
->get('/api/document/' . $doc_id . '/content');
} catch (\Exception $e) {
throw new LingotekApiException('Failed to get document: ' . $e
->getMessage());
}
return $response
->getBody()
->getContents();
}
/**
* {@inheritdoc}
*/
public function getDocumentInfo($id) {
try {
$this->logger
->debug('Lingotek::getDocumentInfo called with id ' . $id);
$response = $this->lingotekClient
->get('/api/document/' . $id);
} catch (\Exception $e) {
$this->logger
->error('Error getting document info: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get document: ' . $e
->getMessage());
}
$this->logger
->debug('getDocumentInfo response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getDocumentStatus($id) {
try {
$this->logger
->debug('Lingotek::getDocumentStatus called with id ' . $id);
$response = $this->lingotekClient
->get('/api/document/' . $id . '/status');
} catch (\Exception $e) {
$this->logger
->error('Error getting document status: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get document status: ' . $e
->getMessage());
}
$this->logger
->debug('getDocumentStatus response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getDocumentTranslationStatuses($id) {
try {
$this->logger
->debug('Lingotek::getDocumentTranslationStatuses called with %id', [
'%id' => $id,
]);
$response = $this->lingotekClient
->get('/api/document/' . $id . '/translation');
} catch (\Exception $e) {
$this->logger
->error('Error getting document translation status (%id): %message.', [
'%id' => $id,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get document translation status: ' . $e
->getMessage());
}
$this->logger
->debug('getDocumentTranslationStatuses response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getDocumentTranslationStatus($id, $locale) {
try {
$this->logger
->debug('Lingotek::getDocumentTranslationStatus called with %id and %locale', [
'%id' => $id,
'%locale' => $locale,
]);
$response = $this->lingotekClient
->get('/api/document/' . $id . '/translation');
} catch (\Exception $e) {
$this->logger
->error('Error getting document translation status (%id, %locale): %message.', [
'%id' => $id,
'%locale' => $locale,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get document translation status: ' . $e
->getMessage());
}
$this->logger
->debug('getDocumentTranslationStatus response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
public function addTranslation($id, $locale, $workflow_id = NULL, $vault_id = NULL) {
try {
$this->logger
->debug('Lingotek::addTranslation called with id ' . $id . ' and locale ' . $locale);
$args = [
'locale_code' => $locale,
];
if ($workflow_id) {
$args['workflow_id'] = $workflow_id;
}
if ($vault_id) {
$args['vault_id'] = $vault_id;
}
$response = $this->lingotekClient
->post('/api/document/' . $id . '/translation', $args);
} catch (\Exception $e) {
// If the problem is that the translation already exist, don't fail.
if ($e
->getCode() === Response::HTTP_BAD_REQUEST) {
$responseBody = json_decode($e
->getResponse()
->getBody(), TRUE);
if ($responseBody['messages'][0] === 'Translation (' . $locale . ') already exists.') {
$this->logger
->info('Added an existing target for %id with %args.', [
'%id' => $id,
'%args' => var_export($args, TRUE),
]);
}
return new Response('Was already requested. All is good.', Response::HTTP_CREATED);
}
$this->logger
->error('Error requesting translation (%id, %args): %message.', [
'%id' => $id,
'%args' => var_export($args, TRUE),
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to add translation: ' . $e
->getMessage());
}
$this->logger
->debug('addTranslation response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getTranslation($id, $locale, $useSource = FALSE) {
try {
$this->logger
->debug('Lingotek::getTranslation called with id ' . $id . ' and locale ' . $locale);
$response = $this->lingotekClient
->get('/api/document/' . $id . '/content', [
'locale_code' => $locale,
'use_source' => $useSource ? 'true' : 'false',
]);
} catch (\Exception $e) {
$this->logger
->error('Error getting translation (%id, %locale): %message.', [
'%id' => $id,
'%locale' => $locale,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to add translation: ' . $e
->getMessage());
}
$this->logger
->debug('getTranslation response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function deleteTranslation($id, $locale) {
try {
$this->logger
->debug('Lingotek::deleteTranslation called with id ' . $id . ' and locale ' . $locale);
$response = $this->lingotekClient
->delete('/api/document/' . $id . '/translation', [
'locale_code' => $locale,
]);
} catch (\Exception $e) {
$this->logger
->error('Error getting translation (%id, %locale): %message.', [
'%id' => $id,
'%locale' => $locale,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to add translation: ' . $e
->getMessage());
}
$this->logger
->debug('deleteTranslation response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getCommunities() {
try {
$this->logger
->debug('Lingotek::getCommunities called.');
$response = $this->lingotekClient
->get('/api/community', [
'limit' => 100,
]);
} catch (\Exception $e) {
$this->logger
->error('Error getting communities: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get communities: ' . $e
->getMessage());
}
$this->logger
->debug('deleteTranslation response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $this
->formatResponse($response);
}
/**
* {@inheritdoc}
*/
public function getProject($project_id) {
try {
$this->logger
->debug('Lingotek::getProject called with id ' . $project_id);
$response = $this->lingotekClient
->get('/api/project/' . $project_id);
} catch (\Exception $e) {
$this->logger
->error('Error getting project %project: %message.', [
'%project' => $project_id,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get project: ' . $e
->getMessage());
}
$this->logger
->debug('getProject response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response
->json();
}
/**
* {@inheritdoc}
*/
public function getProjects($community_id) {
try {
$this->logger
->debug('Lingotek::getProjects called with id ' . $community_id);
$response = $this->lingotekClient
->get('/api/project', [
'community_id' => $community_id,
'limit' => 1000,
]);
} catch (\Exception $e) {
$this->logger
->error('Error getting projects for community %community: %message.', [
'%community' => $community_id,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get projects: ' . $e
->getMessage());
}
$this->logger
->debug('getProjects response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $this
->formatResponse($response);
}
public function setProjectCallBackUrl($project_id, $args) {
try {
$this->logger
->debug('Lingotek::setProjectCallBackUrl called with id ' . $project_id . ' and ' . var_export($args, TRUE));
$response = $this->lingotekClient
->patch('/api/project/' . $project_id, $args);
} catch (\Exception $e) {
$this->logger
->error('Error patching project %project_id with %args: %message.', [
'%project_id' => $project_id,
'%args' => var_export($args, TRUE),
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to patch project: ' . $e
->getMessage());
}
$this->logger
->debug('setProjectCallBackUrl response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function getVaults($community_id) {
try {
$this->logger
->debug('Lingotek::getVaults called with id ' . $community_id);
// We ignore $community_id, as it is not needed for getting the TM vaults.
$response = $this->lingotekClient
->get('/api/vault', [
'limit' => 100,
'is_owned' => 'TRUE',
]);
} catch (\Exception $e) {
$this->logger
->error('Error getting vaults for community %community: %message.', [
'%community' => $community_id,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get vaults: ' . $e
->getMessage());
}
$this->logger
->debug('getVaults response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $this
->formatResponse($response);
}
/**
* {@inheritdoc}
*/
public function getWorkflows($community_id) {
try {
$this->logger
->debug('Lingotek::getWorkflows called with id ' . $community_id);
$response = $this->lingotekClient
->get('/api/workflow', [
'community_id' => $community_id,
'limit' => 1000,
]);
} catch (\Exception $e) {
$this->logger
->error('Error getting workflows for community %community: %message.', [
'%community' => $community_id,
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to get workflows: ' . $e
->getMessage());
}
$this->logger
->debug('getWorkflows response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $this
->formatResponse($response);
}
/**
* {@inheritdoc}
*/
public function getFilters() {
try {
$this->logger
->debug('Lingotek::getFilters called.');
$response = $this->lingotekClient
->get('/api/filter', [
'limit' => 1000,
]);
} catch (\Exception $e) {
throw new LingotekApiException('Failed to get filters: ' . $e
->getMessage());
}
return $this
->formatResponse($response);
}
/**
* Formats the response data as id => title based on the JSON returned
* properties.
*
* @param \Psr\Http\Message\ResponseInterface $response
* A response.
*
* @return array
* Array of titles keyed by id from the response entities.
*/
protected function formatResponse($response) {
$formatted_response = [];
$json_response = json_decode($response
->getBody(), TRUE);
if (!empty($json_response['entities'])) {
foreach ($json_response['entities'] as $entity) {
if (!empty($entity['properties']['id']) && !empty($entity['properties']['title'])) {
$formatted_response[$entity['properties']['id']] = $entity['properties']['title'];
}
}
}
return $formatted_response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LingotekApi:: |
protected | property | The HTTP client to interact with the Lingotek service. | |
LingotekApi:: |
protected | property | A logger instance. | |
LingotekApi:: |
public | function |
Adds a document to Lingotek. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Adds a document target translation for a given locale from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Cancels a document on Lingotek. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Cancels the document target with this document id and locale from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
LingotekApi:: |
public | function |
Deletes a document target translation for a given locale from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
protected | function | Formats the response data as id => title based on the JSON returned properties. | |
LingotekApi:: |
public | function |
Get the account information. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the communities associated with the current account. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the document content from Lingotek. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the document information from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the document status from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the document target translation status for a given locale from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the document target translation statuses from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Get the available filters on Lingotek. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Get the available locales on Lingotek. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the project with the given ID. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the community related projects. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets a document target translation for a given locale from the Lingotek service. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the community related vaults. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Gets the community related workflows. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function |
Updates a document in Lingotek. Overrides LingotekApiInterface:: |
|
LingotekApi:: |
public | function | ||
LingotekApi:: |
public | function | Constructs a LingotekApi object. |