class LingotekApi in Lingotek Translation 8
Same name and namespace in other branches
- 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.4.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
Hierarchy
- class \Drupal\lingotek\Remote\LingotekApi implements LingotekApiInterface
Expanded class hierarchy of LingotekApi
2 files declare their use of LingotekApi
- LingotekApiUnitTest.php in tests/
src/ Unit/ Remote/ LingotekApiUnitTest.php - LingotekSettingsController.php in src/
Controller/ LingotekSettingsController.php
1 string reference to 'LingotekApi'
1 service uses LingotekApi
File
- src/
Remote/ LingotekApi.php, line 20 - Contains \Drupal\lingotek\Remote\LingotekApi.
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;
/**
* 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;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('lingotek.http_client'), $container
->get('logger.channel.lingotek'));
}
public function getLocales() {
$this->logger
->debug('Starting Locales request: /api/locale with args [limit => 1000]');
/** @var 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;
}
public function getAccountInfo() {
try {
$access_token = $this->lingotekClient
->getCurrentToken();
$this->logger
->debug('Starting account info request: /auth/oauth2/access_token_info?access_token=%token', [
'%token' => $access_token,
]);
$response = $this->lingotekClient
->get('/auth/oauth2/access_token_info?access_token=' . $access_token);
} 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;
}
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 $data['properties']['id'];
}
}
// TODO: log warning
return FALSE;
}
public function patchDocument($id, $args) {
try {
$this->logger
->debug('Lingotek::pathDocument (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);
} 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;
}
public function deleteDocument($id) {
try {
$this->logger
->debug('Lingotek::deleteDocument called with id ' . $id);
$response = $this->lingotekClient
->delete('/api/document' . '/' . $id);
} catch (\Exception $e) {
$http_status_code = $e
->getCode();
if ($http_status_code === Response::HTTP_NOT_FOUND) {
$this->logger
->error('Error deleting document: %message.', [
'%message' => $e
->getMessage(),
]);
return new Response($e
->getMessage(), Response::HTTP_NOT_FOUND);
}
$this->logger
->error('Error deleting document: %message.', [
'%message' => $e
->getMessage(),
]);
throw new LingotekApiException('Failed to delete document: ' . $e
->getMessage(), $http_status_code, $e);
}
$this->logger
->debug('deleteDocument response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
/**
* @param $id
* @return mixed
* @throws \Drupal\lingotek\Exception\LingotekApiException
*
* @deprecated in 8.x-1.4. Use ::getDocumentStatus() instead.
*/
public function getDocument($id) {
return $this
->getDocumentStatus($id);
}
public function documentExists($id) {
// TODO
throw new Exception('Not implemented');
}
public function getDocuments($args = array()) {
try {
$this->logger
->debug('Lingotek::getDocuments called');
$response = $this->lingotekClient
->get('/api/document/', $args);
} catch (\Exception $e) {
throw new LingotekApiException('Failed to get documents: ' . $e
->getMessage());
}
return $response
->getBody();
}
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();
}
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;
}
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;
}
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;
}
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) {
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;
}
$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 \GuzzleHttp\Psr7\Response(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;
}
public function getTranslation($id, $locale) {
try {
$this->logger
->debug('Lingotek::getTranslation called with id ' . $id . ' and locale ' . $locale);
$response = $this->lingotekClient
->get('/api/document/' . $id . '/content', array(
'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('getTranslation response received, code %code and body %body', [
'%code' => $response
->getStatusCode(),
'%body' => (string) $response
->getBody(TRUE),
]);
return $response;
}
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', array(
'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;
}
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);
}
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();
}
public function getProjects($community_id) {
try {
$this->logger
->debug('Lingotek::getProjects called with id ' . $community_id);
$response = $this->lingotekClient
->get('/api/project', array(
'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;
}
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', array(
'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);
}
public function getWorkflows($community_id) {
try {
$this->logger
->debug('Lingotek::getWorkflows called with id ' . $community_id);
$response = $this->lingotekClient
->get('/api/workflow', array(
'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);
}
protected function formatResponse($response) {
$formatted_response = array();
$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;
}
}