You are here

public function WSConnectorSimpleHTTP::call in Web Service Data 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/WSConnector/WSConnectorSimpleHTTP.php \Drupal\wsdata\Plugin\WSConnector\WSConnectorSimpleHTTP::call()

Make the connector call.

Overrides WSConnectorBase::call

2 calls to WSConnectorSimpleHTTP::call()
WSConnectorGraphQL::call in src/Plugin/WSConnector/WSConnectorGraphQL.php
Make the connector call.
WSConnectorSimpleHTTPWithLangReplacement::call in src/Plugin/WSConnector/WSConnectorSimpleHTTPWithLangReplacement.php
Make the connector call.
3 methods override WSConnectorSimpleHTTP::call()
WSConnectorGraphQL::call in src/Plugin/WSConnector/WSConnectorGraphQL.php
Make the connector call.
WSConnectorSimpleHTTPWithLangReplacement::call in src/Plugin/WSConnector/WSConnectorSimpleHTTPWithLangReplacement.php
Make the connector call.
WSConnectorSOAP::call in src/Plugin/WSConnector/WSConnectorSOAP.php
Make the connector call.

File

src/Plugin/WSConnector/WSConnectorSimpleHTTP.php, line 187

Class

WSConnectorSimpleHTTP
HTTP Connector.

Namespace

Drupal\wsdata\Plugin\WSConnector

Code

public function call($options, $method, $replacements = [], $data = NULL, array $tokens = []) {
  $this->status = [];
  $token_service = \Drupal::token();
  if (!in_array($method, $this
    ->getMethods())) {
    throw new WSDataInvalidMethodException(sprintf('Invalid method %s on connector type %s', $method, __CLASS__));
  }
  $uri = $this->endpoint . '/' . $options['path'];
  $uri = $this
    ->applyReplacements($uri, $replacements, $tokens);
  $options['http_errors'] = FALSE;

  // Perform the token replace on the headers.
  if (!empty($options['headers'])) {
    for ($i = 0; $i < count($options['headers']); $i++) {
      if (!empty($options['headers'][$i]['key_' . $i])) {
        $options['headers'][$options['headers'][$i]['key_' . $i]] = $token_service
          ->replace($options['headers'][$i]['value_' . $i], $tokens);
      }
      unset($options['headers'][$i]['key_' . $i]);
      unset($options['headers'][$i]['value_' . $i]);
      unset($options['headers'][$i]);
    }
  }
  if (!empty($data)) {
    $options['body'] = $data;
  }
  if (isset($options['body'])) {
    $options['body'] = $token_service
      ->replace($options['body'], $tokens);
  }
  $wscall_cid = $this
    ->getCache() . $method . ':' . $uri;
  $response = FALSE;
  $from_cache = FALSE;
  if ($this
    ->supportsCaching($method)) {
    $cache = \Drupal::cache('wsdata')
      ->get($wscall_cid);
    if ($cache) {
      $response = $cache->data;
      $this->expires = 0;
    }
  }
  if ($response) {
    $from_cache = TRUE;
    $this->status['cache']['cached'] = TRUE;
    $this->status['cache']['debug'] = $this
      ->t('Returning response from cache');
    $options['expires'] = -1;
  }
  else {
    try {
      $result = $this->http_client
        ->request($method, $uri, $options);
      $response = [
        'code' => $result
          ->getStatusCode(),
        'reason' => $result
          ->getReasonPhrase(),
        'expires' => $this
          ->setCacheExpire($result),
        'body' => (string) $result
          ->getBody(),
      ];
      $from_cache = FALSE;
    } catch (\Throwable $e) {
      $this
        ->setError(get_class($e), $e
        ->getMessage());
      return FALSE;
    }
  }
  $this->status['method'] = $method;
  $this->status['uri'] = $uri;
  $this->status['response']['code'] = $response['code'];
  if (\Drupal::state()
    ->get('wsdata_debug_mode')) {
    $this->status['options'] = $options;
    $this->status['response']['body'] = $response['body'];
  }

  // Set the cache expire time.
  if (isset($options['expires']) && !empty($options['expires'])) {
    $this->expires = (int) $options['expires'];
  }
  if ($response['code'] >= 199 and $response['code'] <= 300) {
    if (!$from_cache && $this
      ->supportsCaching($method)) {
      \Drupal::cache('wsdata')
        ->set($wscall_cid, $response, time() + $this->expires + 2, [
        __CLASS__,
      ]);
    }
    return (string) $response['body'];
  }
  $this
    ->setError($response['code'], $response['reason']);
  return FALSE;
}