You are here

class MailchimpGuzzleHttpClient in Mailchimp 8

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

@package Mailchimp

Hierarchy

Expanded class hierarchy of MailchimpGuzzleHttpClient

1 file declares its use of MailchimpGuzzleHttpClient
Mailchimp.php in lib/mailchimp-api-php/src/Mailchimp.php

File

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

Namespace

Mailchimp\http
View source
class MailchimpGuzzleHttpClient implements MailchimpHttpClientInterface {

  /**
   * The GuzzleHttp client.
   *
   * @var Client $client
   */
  private $guzzle;

  /**
   * MailchimpGuzzleHttpClient constructor.
   *
   * @param array $config
   *   Guzzle HTTP configuration options.
   *   Currently supports only 'timeout'.
   */
  public function __construct($config = []) {
    $this->guzzle = new Client($config);
  }

  /**
   * @inheritdoc
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MailchimpGuzzleHttpClient::$guzzle private property The GuzzleHttp client.
MailchimpGuzzleHttpClient::handleRequest public function @inheritdoc Overrides MailchimpHttpClientInterface::handleRequest
MailchimpGuzzleHttpClient::__construct public function MailchimpGuzzleHttpClient constructor.