diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | composer.json | 17 | ||||
-rw-r--r-- | examples/example.php | 2 | ||||
-rw-r--r-- | lib/Client.php | 174 | ||||
-rw-r--r-- | lib/Response.php | 73 | ||||
-rw-r--r-- | lib/SendGrid/Client.php | 232 | ||||
-rw-r--r-- | test/unit/ClientTest.php | 76 | ||||
-rw-r--r-- | test/unit/MockClient.php | 20 | ||||
-rw-r--r-- | test/unit/ResponseTest.php | 44 | ||||
-rw-r--r-- | test/unit/bootstrap.php | 15 |
10 files changed, 357 insertions, 297 deletions
@@ -2,3 +2,4 @@ *.lock vendor/ *.old +.idea/ diff --git a/composer.json b/composer.json index 8ca92eb..48eaec9 100644 --- a/composer.json +++ b/composer.json @@ -5,15 +5,15 @@ "version": "3.1.0", "require-dev": { "phpunit/phpunit": "~4.4", - "squizlabs/php_codesniffer": "2.*" + "squizlabs/php_codesniffer": "~2.0" }, "homepage": "http://github.com/sendgrid/php-http-client", "keywords": ["SendGrid", "HTTP", "REST", "API", "Fluent"], "license": "MIT", "authors": [ { - "name": "Matt Bernier", - "email": "dx@sendgrid.com" + "name": "Matt Bernier", + "email": "dx@sendgrid.com" }, { "name": "Elmer Thomas", @@ -21,11 +21,16 @@ } ], "require": { - "php": ">=5.3" + "php": ">=5.4" }, "autoload": { - "psr-0": { - "SendGrid": "lib/" + "psr-4": { + "SendGrid\\": "lib/" + } + }, + "autoload-dev": { + "psr-4": { + "SendGrid\\Test\\": "test/unit/" } } } diff --git a/examples/example.php b/examples/example.php index 1eb8c41..a3119d1 100644 --- a/examples/example.php +++ b/examples/example.php @@ -2,7 +2,7 @@ // If running this outside of this context, use the following include and // comment out the two includes below // require __DIR__ . '/vendor/autoload.php'; -include(dirname(__DIR__).'/lib/SendGrid/client.php'); +include(dirname(__DIR__) . '/lib/Client.php'); // This gets the parent directory, for your current directory use getcwd() $path_to_config = dirname(__DIR__); $api_key = getenv('SENDGRID_API_KEY'); diff --git a/lib/Client.php b/lib/Client.php new file mode 100644 index 0000000..c14344a --- /dev/null +++ b/lib/Client.php @@ -0,0 +1,174 @@ +<?php + +/** + * HTTP Client library + * + * PHP version 5.4 + * + * @author Matt Bernier <dx@sendgrid.com> + * @author Elmer Thomas <dx@sendgrid.com> + * @copyright 2016 SendGrid + * @license https://opensource.org/licenses/MIT The MIT License + * @version GIT: <git_id> + * @link http://packagist.org/packages/sendgrid/php-http-client + */ + +namespace SendGrid; + +/** + * Quickly and easily access any REST or REST-like API. + */ +class Client +{ + /** @var string */ + protected $host; + /** @var array */ + protected $headers; + /** @var string */ + protected $version; + /** @var array */ + protected $path; + /** @var array */ + private $methods; + + /** + * Initialize the client + * + * @param string $host the base url (e.g. https://api.sendgrid.com) + * @param array $headers global request headers + * @param string $version api version (configurable) + * @param array $path holds the segments of the url path + */ + public function __construct($host, $headers = null, $version = null, $path = null) + { + $this->host = $host; + $this->headers = $headers ?: []; + $this->version = $version; + $this->path = $path ?: []; + // These are the supported HTTP verbs + $this->methods = ['delete', 'get', 'patch', 'post', 'put']; + } + + /** + * Make a new Client object + * + * @param string $name name of the url segment + * + * @return Client object + */ + private function buildClient($name = null) + { + if (isset($name)) { + $this->path[] = $name; + } + $client = new Client($this->host, $this->headers, $this->version, $this->path); + $this->path = []; + return $client; + } + + /** + * Build the final URL to be passed + * + * @param array $queryParams an array of all the query parameters + * + * @return string + */ + private function buildUrl($queryParams = null) + { + $path = '/' . implode('/', $this->path); + if (isset($queryParams)) { + $path .= '?' . http_build_query($queryParams); + } + return sprintf('%s%s%s', $this->host, $this->version ?: '', $path); + } + + /** + * Make the API call and return the response. This is separated into + * it's own function, so we can mock it easily for testing. + * + * @param string $method the HTTP verb + * @param string $url the final url to call + * @param array $body request body + * @param array $headers any additional request headers + * + * @return Response object + */ + public function makeRequest($method, $url, $body = null, $headers = null) + { + $curl = curl_init($url); + + curl_setopt_array($curl, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => 1, + CURLOPT_CUSTOMREQUEST => strtoupper($method), + CURLOPT_SSL_VERIFYPEER => false, + ]); + + if (isset($headers)) { + $this->headers = array_merge($this->headers, $headers); + } + if (isset($body)) { + $encodedBody = json_encode($body); + curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedBody); + $this->headers = array_merge($this->headers, ['Content-Type: application/json']); + } + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); + + $response = curl_exec($curl); + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + + $responseBody = substr($response, $headerSize); + $responseHeaders = substr($response, 0, $headerSize); + + $responseHeaders = explode("\n", $responseHeaders); + + curl_close($curl); + + return new Response($statusCode, $responseBody, $responseHeaders); + } + + /** + * Add variable values to the url. + * (e.g. /your/api/{variable_value}/call) + * Another example: if you have a PHP reserved word, such as and, + * in your url, you must use this method. + * + * @param string $name name of the url segment + * + * @return Client object + */ + public function _($name = null) + { + return $this->buildClient($name); + } + + /** + * Dynamically add method calls to the url, then call a method. + * (e.g. client.name.name.method()) + * + * @param string $name name of the dynamic method call or HTTP verb + * @param array $args parameters passed with the method call + * + * @return Client or Response object + */ + public function __call($name, $args) + { + $name = strtolower($name); + + if ($name === 'version') { + $this->version = $args[0]; + return $this->_(); + } + + if (in_array($name, $this->methods, true)) { + $body = isset($args[0]) ? $args[0] : null; + $queryParams = isset($args[1]) ? $args[1] : null; + $url = $this->buildUrl($queryParams); + $headers = isset($args[2]) ? $args[2] : null; + return $this->makeRequest($name, $url, $body, $headers); + } + + return $this->_($name); + } +} diff --git a/lib/Response.php b/lib/Response.php new file mode 100644 index 0000000..bc04e1f --- /dev/null +++ b/lib/Response.php @@ -0,0 +1,73 @@ +<?php + +/** + * HTTP Client library + * + * PHP version 5.4 + * + * @author Matt Bernier <dx@sendgrid.com> + * @author Elmer Thomas <dx@sendgrid.com> + * @copyright 2016 SendGrid + * @license https://opensource.org/licenses/MIT The MIT License + * @version GIT: <git_id> + * @link http://packagist.org/packages/sendgrid/php-http-client + */ + +namespace SendGrid; + +/** + * Holds the response from an API call. + */ +class Response +{ + /** @var int */ + protected $statusCode; + /** @var string */ + protected $body; + /** @var array */ + protected $headers; + + /** + * Setup the response data + * + * @param int $statusCode the status code. + * @param string $body the response body. + * @param array $headers an array of response headers. + */ + public function __construct($statusCode = null, $body = null, $headers = null) + { + $this->statusCode = $statusCode; + $this->body = $body; + $this->headers = $headers; + } + + /** + * The status code + * + * @return int + */ + public function statusCode() + { + return $this->statusCode; + } + + /** + * The response body + * + * @return string + */ + public function body() + { + return $this->body; + } + + /** + * The response headers + * + * @return array + */ + public function headers() + { + return $this->headers; + } +}
\ No newline at end of file diff --git a/lib/SendGrid/Client.php b/lib/SendGrid/Client.php deleted file mode 100644 index e1fbad6..0000000 --- a/lib/SendGrid/Client.php +++ /dev/null @@ -1,232 +0,0 @@ -<?php -/** - * HTTP Client library - * - * PHP version 5.2 - * - * @author Matt Bernier <dx@sendgrid.com> - * @author Elmer Thomas <dx@sendgrid.com> - * @copyright 2016 SendGrid - * @license https://opensource.org/licenses/MIT The MIT License - * @version GIT: <git_id> - * @link http://packagist.org/packages/sendgrid/php-http-client - */ -namespace SendGrid; - -/** - * Holds the response from an API call. - */ -class Response -{ - /** - * Setup the response data - * - * @param int $status_code the status code. - * @param array $response_body the response body as an array. - * @param array $response_headers an array of response headers. - */ - function __construct($status_code = null, $response_body = null, $response_headers = null) - { - $this->_status_code = $status_code; - $this->_body = $response_body; - $this->_headers = $response_headers; - } - - /** - * The status code - * - * @return integer - */ - public function statusCode() - { - return $this->_status_code; - } - - /** - * The response body - * - * @return array - */ - public function body() - { - return $this->_body; - } - - /** - * The response headers - * - * @return array - */ - public function headers() - { - return $this->_headers; - } -} - -/** - * Quickly and easily access any REST or REST-like API. - */ -class Client -{ - - public - $host, - $request_headers, - $version, - $url_path, - $methods; - - /** - * Initialize the client - * - * @param string $host the base url (e.g. https://api.sendgrid.com) - * @param array $request_headers global request headers - * @param string $version api version (configurable) - * @param array $url_path holds the segments of the url path - */ - function __construct($host, $request_headers = null, $version = null, $url_path = null) - { - $this->host = $host; - $this->request_headers = ($request_headers ? $request_headers : []); - $this->version = $version; - $this->url_path = ($url_path ? $url_path : []); - // These are the supported HTTP verbs - $this->methods = ['delete', 'get', 'patch', 'post', 'put']; - } - - /** - * Make a new Client object - * - * @param string $name name of the url segment - * - * @return Client object - */ - private function _buildClient($name = null) - { - if(isset($name)) { - array_push($this->url_path, $name); - } - $url_path = $this->url_path; - $this->url_path = []; - return new Client($this->host, $this->request_headers, $this->version, $url_path); - } - - /** - * Subclass this function for your own needs. - * Or just pass the version as part of the URL - * (e.g. client._('/v3')) - * - * @param string $url URI portion of the full URL being requested - * - * @return string - */ - private function _buildVersionedUrl($url) - { - return sprintf("%s%s%s", $this->host, $this->version, $url); - } - - /** - * Build the final URL to be passed - * - * @param array $query_params an array of all the query parameters - * - * @return string - */ - private function _buildUrl($query_params = null) - { - $url = '/'.implode('/', $this->url_path); - if (isset($query_params)) { - $url_values = http_build_query($query_params); - $url = sprintf('%s?%s', $url, $url_values); - } - if (isset($this->version)) { - $url = $this->_buildVersionedUrl($url); - } else { - $url = sprintf('%s%s', $this->host, $url);; - } - return $url; - } - - /** - * Make the API call and return the response. This is separated into - * it's own function, so we can mock it easily for testing. - * - * @param array $method the HTTP verb - * @param string $url the final url to call - * @param array $request_body request body - * @param array $request_headers any additional request headers - * - * @return Response object - */ - public function makeRequest($method, $url, $request_body = null, $request_headers = null) - { - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HEADER, 1); - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method)); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - if(isset($request_headers)) { - $this->request_headers = array_merge($this->request_headers, $request_headers); - } - if(isset($request_body)) { - $request_body = json_encode($request_body); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); - $content_length = array('Content-Length: ' . strlen($request_body)); - $content_type = array('Content-Type: application/json'); - $this->request_headers = array_merge($this->request_headers, $content_type); - } - curl_setopt($curl, CURLOPT_HTTPHEADER, $this->request_headers); - $curl_response = curl_exec($curl); - $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - $response_body = substr($curl_response, $header_size); - $response_header = substr($curl_response, 0, $header_size); - - curl_close($curl); - - return new Response($status_code, $response_body, $response_header); - } - - /** - * Add variable values to the url. - * (e.g. /your/api/{variable_value}/call) - * Another example: if you have a PHP reserved word, such as and, - * in your url, you must use this method. - * - * @param string $name name of the url segment - * - * @return Client object - */ - public function _($name = null) - { - return $this->_buildClient($name); - } - - /** - * Dynamically add method calls to the url, then call a method. - * (e.g. client.name.name.method()) - * - * @param string $name name of the dynamic method call or HTTP verb - * @param array $args parameters passed with the method call - * - * @return Client or Response object - */ - public function __call($name, $args) - { - if($name == 'version') { - $this->version = $args[0]; - return $this->_(); - } - - if (in_array($name, $this->methods)) { - $query_params = ((count($args) >= 2) ? $args[1] : null); - $url = $this->_buildUrl($query_params); - $request_body = ($args ? $args[0] : null); - $request_headers = ((count($args) == 3) ? $args[2] : null); - return $this->makeRequest($name, $url, $request_body, $request_headers); - } - - return $this->_($name); - } -} -?> diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index 51cfd76..18cab59 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -1,76 +1,64 @@ <?php -class MockClient extends SendGrid\Client -{ - public - $request_body, - $request_headers, - $url; - - public function makeRequest($method, $url, $request_body = null, $request_headers = null) - { - $this->request_body = $request_body; - $this->request_headers = $request_headers; - $this->url = $url; - return $this; - } -} -class ClientTest_Client extends PHPUnit_Framework_TestCase +namespace SendGrid\Test; + +class ClientTest extends \PHPUnit_Framework_TestCase { - protected - $client, - $host, - $headers; + /** @var MockClient */ + private $client; + /** @var string */ + private $host; + /** @var array */ + private $headers; protected function setUp() { $this->host = 'https://localhost:4010'; - $this->headers = array( + $this->headers = [ 'Content-Type: application/json', 'Authorization: Bearer SG.XXXX' - ); + ]; $this->client = new MockClient($this->host, $this->headers, '/v3', null); } - public function testInitialization() + public function testConstructor() { - $this->assertEquals($this->client->host, $this->host); - $this->assertEquals($this->client->request_headers, $this->headers); - $this->assertEquals($this->client->version, '/v3'); - $this->assertEquals($this->client->url_path, []); - $this->assertEquals($this->client->methods, ['delete', 'get', 'patch', 'post', 'put']); + $this->assertAttributeEquals($this->host, 'host', $this->client); + $this->assertAttributeEquals($this->headers, 'headers', $this->client); + $this->assertAttributeEquals('/v3', 'version', $this->client); + $this->assertAttributeEquals([], 'path', $this->client); + $this->assertAttributeEquals(['delete', 'get', 'patch', 'post', 'put'], 'methods', $this->client); } public function test_() { $client = $this->client->_('test'); - $this->assertEquals($client->url_path, array('test')); + $this->assertAttributeEquals(['test'], 'path', $client); } public function test__call() { $client = $this->client->get(); - $this->assertEquals($client->url, 'https://localhost:4010/v3/'); + $this->assertAttributeEquals('https://localhost:4010/v3/', 'url', $client); - $query_params = array('limit' => 100, 'offset' => 0); - $client = $this->client->get(null, $query_params); - $this->assertEquals($client->url, 'https://localhost:4010/v3/?limit=100&offset=0'); + $queryParams = ['limit' => 100, 'offset' => 0]; + $client = $this->client->get(null, $queryParams); + $this->assertAttributeEquals('https://localhost:4010/v3/?limit=100&offset=0', 'url', $client); - $request_body = array('name' => 'A New Hope'); - $client = $this->client->get($request_body); - $this->assertEquals($client->request_body, $request_body); + $requestBody = ['name' => 'A New Hope']; + $client = $this->client->get($requestBody); + $this->assertAttributeEquals($requestBody, 'requestBody', $client); - $request_headers = array('X-Mock: 200'); - $client = $this->client->get(null, null, $request_headers); - $this->assertEquals($client->request_headers, $request_headers); + $requestHeaders = ['X-Mock: 200']; + $client = $this->client->get(null, null, $requestHeaders); + $this->assertAttributeEquals($requestHeaders, 'requestHeaders', $client); $client = $this->client->version('/v4'); - $this->assertEquals($client->version, '/v4'); + $this->assertAttributeEquals('/v4', 'version', $client); $client = $this->client->path_to_endpoint(); - $this->assertEquals($client->url_path, array('path_to_endpoint')); + $this->assertAttributeEquals(['path_to_endpoint'], 'path', $client); $client = $client->one_more_segment(); - $this->assertEquals($client->url_path, array('path_to_endpoint', 'one_more_segment')); + $this->assertAttributeEquals(['path_to_endpoint', 'one_more_segment'], 'path', $client); } -} -?>
\ No newline at end of file +}
\ No newline at end of file diff --git a/test/unit/MockClient.php b/test/unit/MockClient.php new file mode 100644 index 0000000..0254e5e --- /dev/null +++ b/test/unit/MockClient.php @@ -0,0 +1,20 @@ +<?php + +namespace SendGrid\Test; + +use SendGrid\Client; + +class MockClient extends Client +{ + protected $requestBody; + protected $requestHeaders; + protected $url; + + public function makeRequest($method, $url, $requestBody = null, $requestHeaders = null) + { + $this->requestBody = $requestBody; + $this->requestHeaders = $requestHeaders; + $this->url = $url; + return $this; + } +}
\ No newline at end of file diff --git a/test/unit/ResponseTest.php b/test/unit/ResponseTest.php new file mode 100644 index 0000000..04bd27b --- /dev/null +++ b/test/unit/ResponseTest.php @@ -0,0 +1,44 @@ +<?php + +namespace SendGrid\Test; + +use SendGrid\Response; + +class ResponseTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $response = new Response(); + + $this->assertAttributeEquals(null, 'statusCode', $response); + $this->assertAttributeEquals(null, 'body', $response); + $this->assertAttributeEquals(null, 'headers', $response); + + $response = new Response(200, 'test', ['Content-Encoding: gzip']); + + $this->assertAttributeEquals(200, 'statusCode', $response); + $this->assertAttributeEquals('test', 'body', $response); + $this->assertAttributeEquals(['Content-Encoding: gzip'], 'headers', $response); + } + + public function testStatusCode() + { + $response = new Response(404); + + $this->assertEquals(404, $response->statusCode()); + } + + public function testBody() + { + $response = new Response(null, 'foo'); + + $this->assertEquals('foo', $response->body()); + } + + public function testHeaders() + { + $response = new Response(null, null, ['Content-Type: text/html']); + + $this->assertEquals(['Content-Type: text/html'], $response->headers()); + } +}
\ No newline at end of file diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php index 71a1608..19d4b20 100644 --- a/test/unit/bootstrap.php +++ b/test/unit/bootstrap.php @@ -1,16 +1,3 @@ <?php -include(dirname(dirname(__FILE__)) . '/../lib/SendGrid/client.php'); + require __DIR__ . '/../../vendor/autoload.php'; -function autoload_tests($class) -{ - if (strpos($class, 'PHPHTTPClientTest_') !== 0) { - return; - } - $class = substr($class, 13); - $file = str_replace('_', '/', $class); - if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) { - require_once(dirname(__FILE__) . '/' . $file . '.php'); - } -} -spl_autoload_register('autoload_tests'); -?>
\ No newline at end of file |