You are here

public function LingotekSession::request in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 6 lingotek.session.inc \LingotekSession::request()
  2. 7.7 lingotek.session.inc \LingotekSession::request()
  3. 7.2 lingotek.session.inc \LingotekSession::request()
  4. 7.3 lingotek.session.inc \LingotekSession::request()
  5. 7.4 lingotek.session.inc \LingotekSession::request()
  6. 7.5 lingotek.session.inc \LingotekSession::request()
2 calls to LingotekSession::request()
LingotekSession::login in ./lingotek.session.inc
LingotekSession::logout in ./lingotek.session.inc

File

./lingotek.session.inc, line 96
Handles api calls, logging in and logging out of LingoTek

Class

LingotekSession

Code

public function request($api, $params = NULL, $data = NULL, $returnJson = TRUE) {
  if (!$this
    ->isLoggedIn() && !in_array($api, $this->sessionless)) {
    $this
      ->login();
  }
  $query = "";
  if (isset($params)) {
    $query = http_build_query($params, '', '&');
  }
  $this->headers["Content-Type"] = "application/x-www-form-urlencoded;";
  $timeout = variable_get('lingotek_api_timeout', self::DEFAULT_API_TIMEOUT);
  if (!is_numeric($timeout)) {

    // The site is configured with some non-numeric timeout value, go back to the default.
    LingotekLog::error("The current value for the 'lingotek_api_timeout' system variable ('@value') should be a number. Defaulting to @timeout seconds", array(
      '@value' => $timeout,
      '@timeout' => self::DEFAULT_API_TIMEOUT,
    ));
    $timeout = self::DEFAULT_API_TIMEOUT;
  }
  $api_url = $this->url . "/" . $api;
  $params = array(
    'headers' => $this->headers,
    'method' => 'POST',
    'data' => $query,
    'timeout' => $timeout,
  );
  $response = drupal_http_request($api_url, $params);
  $message_params = array(
    '@url' => $api_url,
    'api' => 'api-session',
    '@method' => $api,
    '!params' => $params,
    '!request' => array(),
    '!response' => $response,
  );
  LingotekLog::api('<h1>@method</h1> <strong>API URL:</strong> @url
        <br /><strong>Request Params</strong>: !params<br /><strong>Response:</strong> !response', $message_params);
  if (isset($response->error)) {
    $this->last_login_msg = $response->error;
  }
  else {
    $this->last_login_msg = "";
  }
  if ($returnJson) {
    if (isset($response->data) && ($json = json_decode($response->data)) && $json->results == "success") {
      return $json;
    }
    else {
      LingotekLog::error('API @api FAILED', array(
        '@api' => $api,
        'params' => $params,
        'error' => $this->last_login_msg,
        'response' => isset($json) ? $json : "",
        'logged_in' => $this->logged_in,
      ), 'api');
      $json = new stdClass();
      $json->results = 'fail';
      return $json;
    }
  }
  else {
    return $response;
  }
}