You are here

public function MailchimpGuzzleHttpClient::handleRequest in Mailchimp 8

@inheritdoc

Overrides MailchimpHttpClientInterface::handleRequest

File

lib/mailchimp-api-php/src/http/MailchimpGuzzleHttpClient.php, line 37

Class

MailchimpGuzzleHttpClient
An HTTP client for use with the Mailchimp API using Guzzle.

Namespace

Mailchimp\http

Code

public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE) {
  if (!empty($parameters)) {
    if ($method == 'GET') {

      // Send parameters as query string parameters.
      $options['query'] = $parameters;
    }
    else {

      // Send parameters as JSON in request body.
      $options['json'] = (object) $parameters;
    }
  }
  try {
    $response = $this->guzzle
      ->request($method, $uri, $options);
    $data = json_decode($response
      ->getBody(), $returnAssoc);
    return $data;
  } catch (RequestException $e) {
    $response = $e
      ->getResponse();
    if (!empty($response)) {
      $message = $e
        ->getResponse()
        ->getBody();
    }
    else {
      $message = $e
        ->getMessage();
    }
    throw new MailchimpAPIException($message, $e
      ->getCode(), $e);
  }
}