You are here

private function DataApi::canonicalizeRequest in Acquia Lift Connector 8

Returns the canonical representation of a request.

Parameters

$method: The request method, e.g. 'GET'.

$path: The path of the request, e.g. '/dashboard/rest/[ACCOUNTNAME]/segments'.

array $parameters: An array of request parameters.

array $headers: An array of request headers.

Return value

string The canonical representation of the request.

1 call to DataApi::canonicalizeRequest()
DataApi::getAuthHeader in src/Service/Api/DataApi.php
Returns a string to use for the 'Authorization' header.

File

src/Service/Api/DataApi.php, line 123
Contains \Drupal\acquia_lift\Service\Api\DataApi.

Class

DataApi

Namespace

Drupal\acquia_lift\Service\Api

Code

private function canonicalizeRequest($method, $url, $parameters = [], $headers = []) {
  $parsed_url = parse_url($url);
  $str = strtoupper($method) . "\n";

  // Certain headers may get added to the actual request so we need to
  // add them here.
  if (!isset($headers['User-Agent'])) {
    $client_config = $this->httpClient
      ->getConfig();
    $headers['User-Agent'] = $client_config['headers']['User-Agent'];
  }
  if (!isset($headers['Host'])) {
    $headers['Host'] = $parsed_url['host'] . (!empty($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  }

  // Sort all header names alphabetically.
  $header_names = array_keys($headers);
  uasort($header_names, create_function('$a, $b', 'return strtolower($a) < strtolower($b) ? -1 : 1;'));

  // Add each header (trimmed and lowercased) and value to the string, separated by
  // a colon, and with a new line after each header:value pair.
  foreach ($header_names as $header) {
    if (!in_array($header, $this->headerWhitelist)) {
      continue;
    }
    $str .= trim(strtolower($header)) . ':' . trim($headers[$header]) . "\n";
  }

  // Add the path.
  $str .= $parsed_url['path'];

  // Sort any parameters alphabetically and add them as a querystring to our string.
  if (!empty($parameters)) {
    ksort($parameters);
    $first_param = key($parameters);
    $str .= '?' . $first_param . '=' . array_shift($parameters);
    foreach ($parameters as $key => $value) {
      $str .= '&' . $key . '=' . $value;
    }
  }
  return $str;
}