You are here

protected function BaseApiAbstract::sendRequest in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.2 api-sdk-php/src/BaseApiAbstract.php \Smartling\BaseApiAbstract::sendRequest()
  2. 8.2 vendor/smartling/api-sdk-php/src/BaseApiAbstract.php \Smartling\BaseApiAbstract::sendRequest()
  3. 8.3 vendor/smartling/api-sdk-php/src/BaseApiAbstract.php \Smartling\BaseApiAbstract::sendRequest()

Parameters

$uri:

array $options:

$method:

bool $returnRawResponseBody:

Return value

mixed. true on SUCCESS and empty data string on $returnRawResponseBody = true array otherwise

Throws

\Smartling\Exceptions\SmartlingApiException

50 calls to BaseApiAbstract::sendRequest()
AuditLogApi::createAccountLevelLogRecord in vendor/smartling/api-sdk-php/src/AuditLog/AuditLogApi.php
AuditLogApi::createProjectLevelLogRecord in vendor/smartling/api-sdk-php/src/AuditLog/AuditLogApi.php
AuditLogApi::searchAccountLevelLogRecord in vendor/smartling/api-sdk-php/src/AuditLog/AuditLogApi.php
AuditLogApi::searchProjectLevelLogRecord in vendor/smartling/api-sdk-php/src/AuditLog/AuditLogApi.php
AuthTokenProvider::authenticate in vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php
Sends /authenticate request

... See full list

File

vendor/smartling/api-sdk-php/src/BaseApiAbstract.php, line 436

Class

BaseApiAbstract
Class BaseApiAbstract

Namespace

Smartling

Code

protected function sendRequest($uri, array $options, $method, $returnRawResponseBody = false) {
  $endpoint = $this
    ->normalizeUri($uri);

  // Dump full request data to log except sensitive data.
  $logRequestData = $options;
  if (isset($logRequestData['headers']['Authorization'])) {
    $logRequestData['headers']['Authorization'] = substr($logRequestData['headers']['Authorization'], 0, 12) . '*****';
  }
  if (isset($logRequestData['json']) && is_array($logRequestData['json']) && isset($logRequestData['json']['userIdentifier'])) {
    $logRequestData['json']['userIdentifier'] = substr($logRequestData['json']['userIdentifier'], 0, 5) . '*****';
    $logRequestData['json']['userSecret'] = substr($logRequestData['json']['userSecret'], 0, 5) . '*****';
  }
  $toLog = [
    'request' => [
      'endpoint' => $endpoint,
      'method' => $method,
      'requestData' => $logRequestData,
    ],
  ];
  $serialized = var_export($toLog, true);
  $this
    ->getLogger()
    ->debug($serialized);
  try {
    $options = $this
      ->processBodyOptions($options);
    $response = $this
      ->getHttpClient()
      ->request($method, $endpoint, $options);
    if (401 === (int) $response
      ->getStatusCode() && !$this instanceof AuthApiInterface) {
      $this
        ->getLogger()
        ->notice('Got unexpected 401 response code, trying to reauth carefully...');
      $this
        ->getAuth()
        ->resetToken();
      $response = $this
        ->getHttpClient()
        ->request($method, $endpoint, $options);
    }
  } catch (RequestException $e) {
    $message = vsprintf('Guzzle:RequestException: %s', [
      $e
        ->getMessage(),
    ]);
    $this
      ->getLogger()
      ->error($message);
    throw new SmartlingApiException($message, 0, $e);
  } catch (\LogicException $e) {
    $message = vsprintf('Guzzle:LogicException: %s', [
      $e
        ->getMessage(),
    ]);
    $this
      ->getLogger()
      ->error($message);
    throw new SmartlingApiException($message, 0, $e);
  } catch (\Exception $e) {
    $message = vsprintf('Guzzle:Exception: %s', [
      $e
        ->getMessage(),
    ]);
    $this
      ->getLogger()
      ->error($message);
    throw new SmartlingApiException($message, 0, $e);
  }

  // Dump full response to log except sensitive data.
  $logResponseData = (string) $response
    ->getBody();
  $logResponseData = preg_replace('/(accessToken":".{5})([^"]+)/', '${1}*****', $logResponseData);
  $logResponseData = preg_replace('/(refreshToken":".{5})([^"]+)/', '${1}*****', $logResponseData);
  $this
    ->getLogger()
    ->debug(json_encode([
    'response' => [
      'statusCode' => $response
        ->getStatusCode(),
      'headers' => $response
        ->getHeaders(),
      'body' => $logResponseData,
    ],
  ], JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT));
  if (400 <= (int) $response
    ->getStatusCode()) {
    $this
      ->checkAuthenticationError($response);
    $this
      ->processErrors($response);
  }
  if ($returnRawResponseBody) {
    return $response
      ->getBody();
  }
  else {
    try {
      $json = json_decode($response
        ->getBody(), true);
      if (!array_key_exists('response', $json) || !is_array($json['response']) || empty($json['response']['code']) || !in_array($json['response']['code'], [
        'SUCCESS',
        'ACCEPTED',
      ])) {
        $this
          ->processError($response);
      }
      return !empty($json['response']['data']) ? $json['response']['data'] : true;
    } catch (RuntimeException $e) {
      $this
        ->processError($response);
    }
  }
}