You are here

public function LingotekApi::request in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::request()
  2. 7.3 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::request()
  3. 7.4 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::request()
  4. 7.5 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::request()
  5. 7.6 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::request()

Calls a Lingotek API method.

Return value

mixed On success, a stdClass object of the returned response data, FALSE on error.

26 calls to LingotekApi::request()
LingotekApi::addContentDocument in lib/Drupal/lingotek/LingotekApi.php
Add a document to the Lingotek platform.
LingotekApi::addRoleAssignment in lib/Drupal/lingotek/LingotekApi.php
Assigns a role to a user. (Must be done by an community admin) Returns TRUE or FALSE
LingotekApi::addTranslationTarget in lib/Drupal/lingotek/LingotekApi.php
Adds a target language to an existing Lingotek Document or Project.
LingotekApi::assignProjectManager in lib/Drupal/lingotek/LingotekApi.php
Assigns a user to a project. (Must be done by an community admin) Returns TRUE or FALSE This will only return TRUE the first time.
LingotekApi::changeWorkflow in lib/Drupal/lingotek/LingotekApi.php
Updates one or more nids to belong to a given workflow

... See full list

File

lib/Drupal/lingotek/LingotekApi.php, line 1061
Defines Drupal\lingotek\LingotekApi

Class

LingotekApi
@file Defines Drupal\lingotek\LingotekApi

Code

public function request($method, $parameters = array(), $request_method = 'POST', $credentials = NULL) {
  LingotekLog::trace('<h2>@method</h2> (trace)', array(
    '@method' => $method,
  ));
  $response_data = FALSE;

  // Every v4 API request needs to have the externalID parameter present.
  // Defaults the externalId to the lingotek_login_id, unless externalId is passed as a parameter
  if (!isset($parameters['externalId'])) {
    $parameters['externalId'] = variable_get('lingotek_login_id', '');
  }
  module_load_include('php', 'lingotek', 'lib/oauth-php/library/OAuthStore');
  module_load_include('php', 'lingotek', 'lib/oauth-php/library/LingotekOAuthRequester');
  $credentials = is_null($credentials) ? array(
    'consumer_key' => variable_get('lingotek_oauth_consumer_id', ''),
    'consumer_secret' => variable_get('lingotek_oauth_consumer_secret', ''),
  ) : $credentials;
  $timer_name = $method . '-' . microtime(TRUE);
  timer_start($timer_name);
  $result = NULL;
  $response = NULL;
  $api_url = $this->api_url . '/' . $method;
  try {
    OAuthStore::instance('2Leg', $credentials);
    $request = @new LingotekOAuthRequester($api_url, $request_method, $parameters);

    // There is an error right here.  The new LingotekOAuthRequester throws it, because it barfs on $parameters
    // The error:  Warning: rawurlencode() expects parameter 1 to be string, array given in LingotekOAuthRequest->urlencode() (line 619 of .../modules/lingotek/lib/oauth-php/library/LingotekOAuthRequest.php).
    // The thing is, if you encode the params, they just get translated back to an array by the object.  They have some type of error internal to the object code that is handling things wrong.
    // I couldn't find a way to get around this without changing the library.  For now, I am just supressing the warning w/ and @ sign.
    $curl_options = array(
      CURLOPT_SSL_VERIFYPEER => FALSE,
    );

    // Check and add proxy settings if needed
    $proxy_server = variable_get('proxy_server', '');
    if (!empty($proxy_server)) {
      $curl_options[CURLOPT_PROXY] = $proxy_server;
      $proxy_port = variable_get('proxy_port', 8080);
      $curl_options[CURLOPT_PROXYPORT] = $proxy_port;

      // Proxy user/password
      $proxy_username = variable_get('proxy_username', '');
      if (!empty($proxy_username)) {
        $proxy_password = variable_get('proxy_password', '');
        $str_usr_pwd = "{$proxy_username}:{$proxy_password}";
        $curl_options[CURLOPT_PROXYUSERPWD] = $str_usr_pwd;
        $proxy_auth_method = variable_get('proxy_auth_method', CURLAUTH_BASIC);
        $curl_options[CURLOPT_PROXYAUTH] = $proxy_auth_method;
      }
    }
    $result = $request
      ->doRequest(0, $curl_options);
    $response = json_decode($result['body']);
  } catch (OAuthException2 $e) {
    LingotekLog::error('Failed OAuth request. <br />Method: @method <br />Message: @message
      <br />API URL: @url
      <br />Parameters: !params.
      <br />Response: !response', array(
      '@method' => $method,
      '@message' => $e
        ->getMessage(),
      '@url' => $api_url,
      '!params' => $parameters,
      '!response' => $response,
    ), 'api');
  }
  $timer_results = timer_stop($timer_name);

  // cleanup parameters so that the logs aren't too long
  if (isset($parameters['fprmFileContents'])) {
    $parameters['fprmFileContents'] = 'removed for brevity';
  }
  if (isset($parameters['secondaryFprmFileContents'])) {
    $parameters['secondaryFprmFileContents'] = 'removed for brevity';
  }
  $message_params = array(
    '@url' => $api_url,
    '@method' => $method,
    '!params' => $parameters,
    '!request' => $request,
    '!response' => $method == 'downloadDocument' && !isset($response->results) ? "Zipped document" : $response,
    '@response_time' => number_format($timer_results['time']) . ' ms',
  );

  /*
   Exceptions:
   downloadDocument - Returns misc data (no $response->results), and should always be sent back.
   assignProjectManager - Returns fails/falses if the person is already a community manager (which should be ignored)
  */
  if ($method == 'downloadDocument') {

    // Exception downloadDocument
    LingotekLog::api('<h1>@method</h1> <strong>API URL:</strong> @url
        <br /><strong>Response Time:</strong> @response_time<br /><strong>Request Params</strong>: !params<br /><strong>Response:</strong> !response<br/><strong>Full Request:</strong> !request', $message_params);
    $response_data['code'] = !empty($result['code']) ? $result['code'] : "";
    $response_data['body'] = !empty($result) ? $result['body'] : "";
  }
  elseif ($this
    ->returnEntireResponse($method)) {
    $result['body'] = $response;
    $response_data = $result;
  }
  elseif (!is_null($response) && $response->results == self::RESPONSE_STATUS_SUCCESS || $method == 'assignProjectManager') {

    // SUCCESS
    LingotekLog::api('<h1>@method</h1> <strong>API URL:</strong> @url
        <br /><strong>Response Time:</strong> @response_time<br /><strong>Request Params</strong>: !params<br /><strong>Response:</strong> !response<br/><strong>Full Request:</strong> !request', $message_params);
    $response_data = $response;
  }
  else {

    // ERROR
    LingotekLog::error('<h1>@method (Failed)</h1> <strong>API URL:</strong> @url
        <br /><strong>Response Time:</strong> @response_time<br /><strong>Request Params</strong>: !params<br /><strong>Response:</strong> !response<br/><strong>Full Request:</strong> !request', $message_params, 'api');
    $response_data = $response;
  }
  return $response_data;
}