MailchimpGuzzleHttpClient.php in Mailchimp 8
File
lib/mailchimp-api-php/src/http/MailchimpGuzzleHttpClient.php
View source
<?php
namespace Mailchimp\http;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Mailchimp\MailchimpAPIException;
class MailchimpGuzzleHttpClient implements MailchimpHttpClientInterface {
private $guzzle;
public function __construct($config = []) {
$this->guzzle = new Client($config);
}
public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE) {
if (!empty($parameters)) {
if ($method == 'GET') {
$options['query'] = $parameters;
}
else {
$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);
}
}
}