You are here

public function LingotekApi::request in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 7.7 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.

20 calls to LingotekApi::request()
LingotekApi::addContentDocument in lib/Drupal/lingotek/LingotekApi.php
Add a document to the Lingotek platform.
LingotekApi::addContentDocumentWithTargets in lib/Drupal/lingotek/LingotekApi.php
Adds a Document and one or more Translation Targets to the Lingotek platform. (only used by comments currently)
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.

... See full list

File

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

Class

LingotekApi
@file Defines Drupal\lingotek\LingotekApi

Code

public function request($method, $parameters = array(), $request_method = 'POST') {
  global $user;
  watchdog('api request', t($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/OAuthRequester');
  $credentials = array(
    'consumer_key' => variable_get('lingotek_oauth_consumer_id', ''),
    'consumer_secret' => variable_get('lingotek_oauth_consumer_secret', ''),
  );
  $timer_name = $method . '-' . microtime(TRUE);
  timer_start($timer_name);
  $response = NULL;
  try {
    OAuthStore::instance('2Leg', $credentials);
    $api_url = $this->api_url . '/' . $method;
    $request = @new OAuthRequester($api_url, $request_method, $parameters);

    // There is an error right here.  The new OAuthRequester throws it, because it barfs on $parameters
    // The error:  Warning: rawurlencode() expects parameter 1 to be string, array given in OAuthRequest->urlencode() (line 619 of .../modules/lingotek/lib/oauth-php/library/OAuthRequest.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.
    $result = $request
      ->doRequest(0, array(
      CURLOPT_SSL_VERIFYPEER => FALSE,
    ));
    $response = $method == 'downloadDocument' ? $result['body'] : json_decode($result['body']);
    watchdog('api response', '
      <h1>@method ::</h1>
      <div>!parameters</div>
      <h1>Request:</h1>
      <div>!request</div>
      <h1>Response:</h1>
      <div>!response</div>
      ', array(
      '@method' => $method,
      '!parameters' => watchdog_format_object($parameters),
      '!request' => watchdog_format_object($request),
      '!response' => $method == 'downloadDocument' ? 'Zipped Lingotek Document Data' : watchdog_format_object($response),
    ), WATCHDOG_DEBUG);
  } catch (OAuthException2 $e) {
    watchdog('lingotek', 'Failed OAuth request.
      <br />API URL: @url
      <br />Message: @message.
      <br />Method: @name.
      <br />Parameters: !params.
      <br />Response: !response', array(
      '@url' => $api_url,
      '@message' => $e
        ->getMessage(),
      '@name' => $method,
      '!params' => $this
        ->watchdog_format_object($parameters),
      '!response' => $this
        ->watchdog_format_object($response),
    ), WATCHDOG_ERROR);
  }
  $timer_results = timer_stop($timer_name);
  if ($this->debug) {
    $message_params = array(
      '@url' => $api_url,
      '@method' => $method,
      '!params' => $this
        ->watchdog_format_object($parameters),
      '!response' => $this
        ->watchdog_format_object($response),
      '@response_time' => number_format($timer_results['time']) . ' ms',
    );
    watchdog('lingotek_debug', '<strong>Called API method</strong>: @method
      <br /><strong>API URL:</strong> @url
      <br /><strong>Response Time:</strong> @response_time<br /><strong>Params</strong>: !params<br /><strong>Response:</strong> !response', $message_params, WATCHDOG_DEBUG);
  }

  /*
    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' || $method == 'assignProjectManager' || !is_null($response) && $response->results == self::RESPONSE_STATUS_SUCCESS) {
    $response_data = $response;
  }
  else {
    watchdog('lingotek', 'Failed API call.<br />Method: @name. <br />Parameters: !params. <br />Response: !response', array(
      '@name' => $method,
      '!params' => $this
        ->watchdog_format_object($parameters),
      '!response' => $this
        ->watchdog_format_object($response),
    ), WATCHDOG_ERROR);
  }
  return $response_data;
}