You are here

public function AcquiaDAM_Client::request in Media: Acquia DAM 7

Make a request to the Acquia DAM API.

Parameters

string $endpoint: The API endpoint to request.

string|array $data: The data to send with the request.

array $headers: Any additional headers to include.

string $method: The request type.

Return value

array The result of the API request.

2 calls to AcquiaDAM_Client::request()
AcquiaDAM_Client::getSubscription in src/AcquiaDAM/AcquiaDAM_Client.inc
Get the subscription information.
AcquiaDAM_Client::getUser in src/AcquiaDAM/AcquiaDAM_Client.inc
Get the user the client is authenticated as.

File

src/AcquiaDAM/AcquiaDAM_Client.inc, line 159

Class

AcquiaDAM_Client
Client for making API requests.

Code

public function request($endpoint, $data = NULL, array $headers = [], $method = 'GET') {
  $url = sprintf('%s/%s', $this->apiBaseURL, $endpoint);

  // Convert our data into a GET request.
  if ('GET' == drupal_strtoupper($method) && is_array($data)) {
    $url = $this
      ->buildGetRequest($url, $data);
    $data = NULL;
  }
  $options = [
    'headers' => $headers,
    'method' => $method,
    'data' => $data,
  ];
  $auth_token = $this
    ->getToken();
  if (empty($auth_token['access_token'])) {
    return FALSE;
  }
  $options['headers']['Authorization'] = sprintf('%s %s', ucfirst($auth_token['token_type']), $auth_token['access_token']);
  $resp = drupal_http_request($url, $options);
  if (!empty($resp->data)) {
    $resp->data = drupal_json_decode($resp->data);
  }

  // Acquia DAM does not return proper error codes for different conditions, so
  // we can't do much beyond checking for "not a success".
  if (200 != $resp->code) {
    $msg = empty($resp->data['message']) ? $resp->error : $resp->data['message'];
    throw new UnexpectedValueException(sprintf('(%d) %s response when accessing %s', $resp->code, $msg, $url));
  }
  return $resp->data;
}