You are here

protected function SmartlingApi::sendRequest in TMGMT Translator Smartling 8

Sends request to Smartling Service via Guzzle Client.

Parameters

string $uri: Resource uri.

array $requestData: Parameters to be send as query or multipart form elements.

string $method: Http method uppercased.

Return value

array Decoded JSON answer.

Throws

\Drupal\tmgmt_smartling\Smartling\SmartlingApiException

13 calls to SmartlingApi::sendRequest()
SmartlingApi::deleteFile in src/Smartling/SmartlingApi.php
Removes the file from Smartling.
SmartlingApi::downloadFile in src/Smartling/SmartlingApi.php
Downloads the requested file from Smartling.
SmartlingApi::getAuthorizedLocales in src/Smartling/SmartlingApi.php
Get list of authorized locales for given file.
SmartlingApi::getContextStats in src/Smartling/SmartlingApi.php
Get statistics for context upload
SmartlingApi::getList in src/Smartling/SmartlingApi.php
Lists recently uploaded files. Returns a maximum of 500 files.

... See full list

File

src/Smartling/SmartlingApi.php, line 75

Class

SmartlingApi

Namespace

Drupal\tmgmt_smartling\Smartling

Code

protected function sendRequest($uri, $requestData, $method, $filenames = []) {

  // Set api key and product id as required arguments.
  $requestData['apiKey'] = $this->apiKey;
  $requestData['projectId'] = $this->projectId;

  // Ask for JSON and disable Guzzle exceptions.
  $options = [
    'headers' => [
      'Accept' => 'application/json',
    ],
    'http_errors' => FALSE,
  ];

  // For GET and DELETE http methods use just query parameter builder.
  if (in_array($method, [
    'GET',
    'DELETE',
  ])) {
    $options['query'] = $requestData;
  }
  else {
    $options['multipart'] = [];

    // Remove file from params array and add it as a stream.
    if (!empty($requestData['file'])) {
      $options['multipart'][] = [
        'name' => 'file',
        'contents' => $this
          ->readFile($requestData['file']),
      ];
      unset($requestData['file']);
    }
    foreach ($requestData as $key => $value) {

      // Hack to cast FALSE to '0' instead of empty string.
      if (is_bool($value)) {
        $value = (int) $value;
      }
      $val = [
        'name' => $key,
        // Typecast everything to string to avoid curl notices.
        'contents' => (string) $value,
      ];
      if (isset($filenames[$key])) {
        $val['filename'] = $filenames[$key]['name'];
        $val['Content-Type'] = $filenames[$key]['content_type'];

        //mime_content_type($val['filename']);
      }
      $options['multipart'][] = $val;
    }
  }

  // Avoid double slashes in final URL.
  $uri = ltrim($uri, "/");
  $guzzle_response = $this->httpClient
    ->request($method, $this->baseUrl . '/' . $uri, $options);

  //$guzzle_response = $this->httpClient->request($method, 'http://requestb.in/zmzrfuzn', $options);
  $response_body = (string) $guzzle_response
    ->getBody();

  // Catch all errors from Smartling and throw appropriate exception.
  if ($guzzle_response
    ->getStatusCode() >= 400) {
    $error_response = json_decode($response_body, TRUE);
    $error_messages = [];
    if (!empty($error_response['response']['errors'])) {
      $error_messages = $error_response['response']['errors'];
    }
    elseif (!empty($error_response['response']['messages'])) {
      $error_messages = $error_response['response']['messages'];
    }
    if (!$error_response || empty($error_messages)) {
      throw new SmartlingApiException('Bad response format from Smartling');
    }
    throw new SmartlingApiException(print_r($error_messages, TRUE), $guzzle_response
      ->getStatusCode());
  }

  // "Download file" method return translated file directly.
  if ('file/get' == $uri) {
    return $response_body;
  }
  $response = json_decode($response_body, TRUE);

  // Throw exception if json is not valid.
  if (!$response || !isset($response['response']['data'])) {
    throw new SmartlingApiException('Bad response format from Smartling');
  }
  return $response['response']['data'];
}