Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing an array to the constructor so we can configure the timeout #276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MailChimp
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';

const TIMEOUT = 10;
private $timeout = 10;

/* SSL Verification
Read before disabling:
Expand All @@ -36,7 +36,7 @@ class MailChimp
*
* @throws \Exception
*/
public function __construct($api_key, $api_endpoint = null)
public function __construct($api_key, $api_endpoint = null, $parameters = array())
{
if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
throw new \Exception("cURL support is required, but can't be found.");
Expand All @@ -54,6 +54,10 @@ public function __construct($api_key, $api_endpoint = null)
$this->api_endpoint = $api_endpoint;
}

if (isset($parameters['timeout'])) {
$this->timeout = $parameters['timeout'];
}

$this->last_response = array('headers' => null, 'body' => null);
}

Expand Down Expand Up @@ -140,8 +144,12 @@ public function getLastRequest()
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function delete($method, $args = array(), $timeout = self::TIMEOUT)
public function delete($method, $args = array(), $timeout = 0)
{
if ($timeout === 0) {
$timeout = $this->timeout;
}

return $this->makeRequest('delete', $method, $args, $timeout);
}

Expand All @@ -154,8 +162,12 @@ public function delete($method, $args = array(), $timeout = self::TIMEOUT)
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function get($method, $args = array(), $timeout = self::TIMEOUT)
public function get($method, $args = array(), $timeout = 0)
{
if ($timeout === 0) {
$timeout = $this->timeout;
}

return $this->makeRequest('get', $method, $args, $timeout);
}

Expand All @@ -168,8 +180,12 @@ public function get($method, $args = array(), $timeout = self::TIMEOUT)
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function patch($method, $args = array(), $timeout = self::TIMEOUT)
public function patch($method, $args = array(), $timeout = 0)
{
if ($timeout === 0) {
$timeout = $this->timeout;
}

return $this->makeRequest('patch', $method, $args, $timeout);
}

Expand All @@ -182,8 +198,12 @@ public function patch($method, $args = array(), $timeout = self::TIMEOUT)
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function post($method, $args = array(), $timeout = self::TIMEOUT)
public function post($method, $args = array(), $timeout = 0)
{
if ($timeout === 0) {
$timeout = $this->timeout;
}

return $this->makeRequest('post', $method, $args, $timeout);
}

Expand All @@ -196,8 +216,11 @@ public function post($method, $args = array(), $timeout = self::TIMEOUT)
*
* @return array|false Assoc array of API response, decoded from JSON
*/
public function put($method, $args = array(), $timeout = self::TIMEOUT)
public function put($method, $args = array(), $timeout = 0)
{
if ($timeout === 0) {
$timeout = $this->timeout;
}
return $this->makeRequest('put', $method, $args, $timeout);
}

Expand All @@ -211,8 +234,12 @@ public function put($method, $args = array(), $timeout = self::TIMEOUT)
*
* @return array|false Assoc array of decoded result
*/
private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT)
private function makeRequest($http_verb, $method, $args = array(), $timeout = 0)
{
if ($timeout === 0) {
$timeout = $this->timeout;
}

$url = $this->api_endpoint . '/' . $method;

$response = $this->prepareStateForRequest($http_verb, $method, $url, $timeout);
Expand Down