-
Notifications
You must be signed in to change notification settings - Fork 96
Add timeout options and some maintenance stuff. #28
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="autoload.php" | ||
> | ||
<testsuites> | ||
<testsuite name="php-rest-api"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src/</directory> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. allows for PHPUnit to generate a coverage report (and easy PHPUnit runs from the commandline, i.e. just |
||
</whitelist> | ||
</filter> | ||
</phpunit> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,10 @@ | |
*/ | ||
class HttpClient | ||
{ | ||
const REQUEST_GET = "GET"; | ||
const REQUEST_POST = "POST"; | ||
const REQUEST_DELETE = "DELETE"; | ||
const REQUEST_PUT = "PUT"; | ||
const REQUEST_GET = 'GET'; | ||
const REQUEST_POST = 'POST'; | ||
const REQUEST_DELETE = 'DELETE'; | ||
const REQUEST_PUT = 'PUT'; | ||
|
||
const HTTP_NO_CONTENT = 204; | ||
|
||
|
@@ -27,19 +27,51 @@ class HttpClient | |
/** | ||
* @var array | ||
*/ | ||
protected $userAgent = array (); | ||
protected $userAgent = array(); | ||
|
||
/** | ||
* @var Common\Authentication | ||
*/ | ||
protected $Authentication; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $timeout = 10; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $connectionTimeout = 2; | ||
|
||
/** | ||
* @param string $endpoint | ||
* @param int $timeout > 0 | ||
* @param int $connectionTimeout >= 0 | ||
* | ||
* @throws \InvalidArgumentException if timeout settings are invalid | ||
*/ | ||
public function __construct($endpoint) | ||
public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added timeout options without BC breaks |
||
{ | ||
$this->endpoint = $endpoint; | ||
|
||
if (!is_int($timeout) || $timeout < 1) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Timeout must be an int > 0, got "%s".', | ||
is_object($timeout) ? get_class($timeout) : gettype($timeout).' '.var_export($timeout, true)) | ||
); | ||
} | ||
|
||
$this->timeout = $timeout; | ||
|
||
if (!is_int($connectionTimeout) || $connectionTimeout < 0) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Connection timeout must be an int >= 0, got "%s".', | ||
is_object($connectionTimeout) ? get_class($connectionTimeout) : gettype($connectionTimeout).' '.var_export($connectionTimeout, true)) | ||
); | ||
} | ||
|
||
$this->connectionTimeout = $connectionTimeout; | ||
} | ||
|
||
/** | ||
|
@@ -59,8 +91,8 @@ public function setAuthentication(Common\Authentication $Authentication) | |
} | ||
|
||
/** | ||
* @param $resourceName | ||
* @param $query | ||
* @param string $resourceName | ||
* @param mixed $query | ||
* | ||
* @return string | ||
*/ | ||
|
@@ -73,14 +105,15 @@ public function getRequestUrl($resourceName, $query) | |
} | ||
$requestUrl .= '?' . $query; | ||
} | ||
|
||
return $requestUrl; | ||
} | ||
|
||
/** | ||
* @param $method | ||
* @param $resourceName | ||
* @param null $query | ||
* @param null $body | ||
* @param string $method | ||
* @param string $resourceName | ||
* @param mixed $query | ||
* @param string|null $body | ||
* | ||
* @return array | ||
* @throws Exceptions\HttpException | ||
|
@@ -92,17 +125,17 @@ public function performHttpRequest($method, $resourceName, $query = null, $body | |
$headers = array ( | ||
'User-agent: ' . implode(' ', $this->userAgent), | ||
'Accepts: application/json', | ||
"Content-Type: application/json", | ||
"Accept-Charset: utf-8", | ||
sprintf("Authorization: AccessKey %s", $this->Authentication->accessKey) | ||
'Content-Type: application/json', | ||
'Accept-Charset: utf-8', | ||
sprintf('Authorization: AccessKey %s', $this->Authentication->accessKey) | ||
); | ||
|
||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt($curl, CURLOPT_HEADER, true); | ||
curl_setopt($curl, CURLOPT_URL, $this->getRequestUrl($resourceName, $query)); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, 10); | ||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); | ||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout); | ||
|
||
if ($method === self::REQUEST_GET) { | ||
curl_setopt($curl, CURLOPT_HTTPGET, true); | ||
|
@@ -117,10 +150,11 @@ public function performHttpRequest($method, $resourceName, $query = null, $body | |
} | ||
|
||
// Some servers have outdated or incorrect certificates, Use the included CA-bundle | ||
$caFile = realpath(dirname(__FILE__) . "/../ca-bundle.crt"); | ||
$caFile = realpath(__DIR__ . '/../ca-bundle.crt'); | ||
if (!file_exists($caFile)) { | ||
throw new Exceptions\HttpException('Unable to find CA-bundle file: ' . $caFile); | ||
throw new Exceptions\HttpException(sprintf('Unable to find CA-bundle file "%s".', __DIR__ . '/../ca-bundle.crt')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the file does not exists |
||
} | ||
|
||
curl_setopt($curl, CURLOPT_CAINFO, $caFile); | ||
|
||
$response = curl_exec($curl); | ||
|
@@ -133,11 +167,10 @@ public function performHttpRequest($method, $resourceName, $query = null, $body | |
|
||
// Split the header and body | ||
$parts = explode("\r\n\r\n", $response, 3); | ||
list($responseHeader, $responseBody) = ($parts[0] == 'HTTP/1.1 100 Continue') ? array ($parts[1], $parts[2]) : array ($parts[0], $parts[1]); | ||
list($responseHeader, $responseBody) = ($parts[0] === 'HTTP/1.1 100 Continue') ? array ($parts[1], $parts[2]) : array ($parts[0], $parts[1]); | ||
|
||
curl_close($curl); | ||
|
||
return array ($responseStatus, $responseHeader, $responseBody); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
use MessageBird\Client; | ||
use MessageBird\Common\HttpClient; | ||
|
||
class HttpClientTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testHttpClient() | ||
{ | ||
$client = new HttpClient(Client::ENDPOINT); | ||
|
||
$url = $client->getRequestUrl('a', null); | ||
$this->assertSame(Client::ENDPOINT.'/a', $url); | ||
|
||
$url = $client->getRequestUrl('a', array('b' => 1)); | ||
$this->assertSame(Client::ENDPOINT.'/a?b=1', $url); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessageRegExp #^Timeout must be an int > 0, got "integer 0".$# | ||
*/ | ||
public function testHttpClientInvalidTimeout() | ||
{ | ||
new HttpClient(Client::ENDPOINT, 0); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessageRegExp #^Connection timeout must be an int >= 0, got "stdClass".$# | ||
*/ | ||
public function testHttpClientInvalidConnectionTimeout() | ||
{ | ||
new HttpClient(Client::ENDPOINT, 10, new \stdClass()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prep. for upcoming 7.1 release