You are here

public function LingotekSession::request in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 6 lingotek.session.inc \LingotekSession::request()
  2. 7.7 lingotek.session.inc \LingotekSession::request()
  3. 7.3 lingotek.session.inc \LingotekSession::request()
  4. 7.4 lingotek.session.inc \LingotekSession::request()
  5. 7.5 lingotek.session.inc \LingotekSession::request()
  6. 7.6 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 88
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.
    watchdog('lingotek', "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,
    ), WATCHDOG_ERROR);
    $timeout = self::DEFAULT_API_TIMEOUT;
  }
  $response = drupal_http_request($this->url . "/" . $api, array(
    'headers' => $this->headers,
    'method' => 'POST',
    'data' => $query,
    'timeout' => $timeout,
  ));
  if ($returnJson) {
    if (isset($response->data) && ($json = json_decode($response->data)) && $json->results == "success") {
      return $json;
    }
    else {
      lingotek_error('API ' . $api . ' FAILED', array(
        'params' => $params,
        'error' => isset($response->error) ? $response->error : "",
        'response' => isset($json) ? $json : "",
        'logged_in' => $this->logged_in,
      ), 1);
      $json = new stdClass();
      $json->results = 'fail';
      return $json;
    }
  }
  else {
    return $response;
  }
}