diff options
-rw-r--r-- | .env_sample | 4 | ||||
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | composer.json | 38 | ||||
-rw-r--r-- | examples/example.php | 8 | ||||
-rw-r--r-- | lib/client.php (renamed from php_http_client/client.php) | 109 | ||||
-rw-r--r-- | lib/config.php (renamed from php_http_client/config.php) | 1 | ||||
-rw-r--r-- | test/unit/ClientTest.php | 75 | ||||
-rw-r--r-- | test/unit/ConfigTest.php | 24 | ||||
-rw-r--r-- | test/unit/bootstrap.php | 17 |
9 files changed, 212 insertions, 68 deletions
diff --git a/.env_sample b/.env_sample new file mode 100644 index 0000000..f1a63f3 --- /dev/null +++ b/.env_sample @@ -0,0 +1,4 @@ +HOST=<base_url_for_live_api_host> +MOCK_HOST=<base_url_for_remote_mocked_api_host> +LOCAL_HOST=<base_url_for_local_mocked_api_host> +SENDGRID_API_KEY=<your_sendgrid_api_key>
\ No newline at end of file @@ -1 +1,3 @@ -.env
\ No newline at end of file +.env +*.lock +vendor/
\ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..bf4d4bd --- /dev/null +++ b/composer.json @@ -0,0 +1,38 @@ + +{ + "name": "sendgrid/php-http-client", + "description": "HTTP REST client, simplified for Python", + "type": "library", + "version": "1.0.0", + "homepage": "http://github.com/sendgrid/php-http-client", + "license": "MIT", + "keywords": ["SendGrid", "HTTP", "REST", "API", "Fluent"], + "require": { + "php": ">=5.3" + }, + "authors": [ + { + "name": "Matt Bernier", + "email": "dx@sendgrid.com", + "homepage": "https://www.sendgrid.com", + "role": "Product Manager" + }, + { + "name": "Elmer Thomas", + "email": "dx@sendgrid.com", + "homepage": "https://www.sendgrid.com", + "role": "Developer" + } + ], + "support": { + "email": "dx@sendgrid.com", + "issues": "https://github.com/sendgrid/php-http-client/issues", + "source": "https://github.com/php-http-client" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "autoload": { + "psr-0": {"SendGrid": "lib/"} + } +}
\ No newline at end of file diff --git a/examples/example.php b/examples/example.php index 05fe5eb..48fcf61 100644 --- a/examples/example.php +++ b/examples/example.php @@ -1,13 +1,13 @@ <?php -include(dirname(__DIR__).'/php_http_client/client.php'); -include(dirname(__DIR__).'/php_http_client/config.php'); -$config = new Config(dirname(__DIR__), '.env'); +include(dirname(__DIR__).'/lib/client.php'); +include(dirname(__DIR__).'/lib/config.php'); +$config = new SendGrid\Config(dirname(__DIR__), '.env'); $api_key = getenv('SENDGRID_API_KEY'); $headers = array( 'Content-Type: application/json', 'Authorization: Bearer '.$api_key ); -$client = new Client("https://e9sk3d3bfaikbpdq7.stoplight-proxy.io", $headers, "3", null); +$client = new SendGrid\Client("https://e9sk3d3bfaikbpdq7.stoplight-proxy.io", $headers, "3", null); # GET Collection $query_params = array('limit' => 100, 'offset' => 0); diff --git a/php_http_client/client.php b/lib/client.php index 77ca629..602d53b 100644 --- a/php_http_client/client.php +++ b/lib/client.php @@ -1,4 +1,5 @@ <?php +namespace SendGrid; class Response { function __construct($status_code = null, $response_body = null, $response_headers = null){ @@ -30,7 +31,14 @@ class Response { } class Client { - + + public + $host, + $request_headers, + $version, + $url_path, + $methods; + function __construct($host, $request_headers = null, $version = null, $url_path = null){ /* @param host: Base URL for the api. (e.g. https://api.sendgrid.com) @@ -45,11 +53,11 @@ class Client { $this->host = $host; $this->request_headers = ($request_headers ? $request_headers : []); - $this->_version = $version; - # _url_path keeps track of the dynamically built url - $this->_url_path = ($url_path ? $url_path : []); + $this->version = $version; + # url_path keeps track of the dynamically built url + $this->url_path = ($url_path ? $url_path : []); # These are the supported HTTP verbs - $this->_methods = ['delete', 'get', 'patch', 'post', 'put']; + $this->methods = ['delete', 'get', 'patch', 'post', 'put']; } /** @@ -58,14 +66,26 @@ class Client { * @type name: string */ private function _build_client($name = null) { - if($name != null){ - array_push($this->_url_path, $name); + 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); + $url_path = $this->url_path; + $this->url_path = []; + return new Client($this->host, $this->request_headers, $this->version, $url_path); + } + + /** + * Add the version to the path + */ + public function version($version) { + $this->version = $version; + return $this->_(null); } + public function get_version() { + return $this->version; + } + /** * Subclass this function for your own needs. * Or just pass the version as part of the URL @@ -75,7 +95,7 @@ class Client { * @return: string */ private function _build_versioned_url($url) { - return sprintf("%s/v%d%s", $this->host, $this->_get_version(), $url); + return sprintf("%s/v%d%s", $this->host, $this->get_version(), $url); } /** @@ -86,14 +106,14 @@ class Client { */ private function _build_url($query_params = null) { - $url = '/'.implode('/', $this->_url_path); + $url = '/'.implode('/', $this->url_path); if (isset($query_params)) { $url_values = http_build_query($query_params); $url = sprintf('%s?%s', $url, $url_values); } - if (null != $this->_get_version()) { + if (null != $this->get_version()) { $url = $this->_build_versioned_url($url); } else { $url = sprintf('%s%s', $this->host, $url);; @@ -120,53 +140,29 @@ class Client { * @type request: urllib.Request object * @return: */ - private function _make_request($method, $url, $request_body = null, $request_headers = null) { + public function _make_request($method, $url, $request_body = null, $request_headers = null) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, 1); - switch($method){ - case 'get': - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); - break; - case 'post': - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); - $request_body = json_encode($request_body); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); - $content_length = array('Content-Length: ' . strlen($request_body)); - $this->request_headers = array_merge($this->request_headers, $content_length); - break; - case 'patch': - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); - $request_body = json_encode($request_body); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); - $content_length = array('Content-Length: ' . strlen($request_body)); - $this->request_headers = array_merge($this->request_headers, $content_length); - break; - case 'put': - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); - $request_body = json_encode($request_body); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); - $content_length = array('Content-Length: ' . strlen($request_body)); - $this->request_headers = array_merge($this->request_headers, $content_length); - break; - case 'delete': - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); - break; - default: - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); - break; - } + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + 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)); + } if(isset($request_headers)){ $this->request_headers = array_merge($this->request_headers, $request_headers); } curl_setopt($curl, CURLOPT_HTTPHEADER, $this->request_headers); $curl_response = curl_exec($curl); $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $response_header = substr($curl_response, 0, $header_size); - $response_body = substr($curl_response, $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); } @@ -188,28 +184,15 @@ class Client { return version($name); } - if (in_array($name, $this->_methods)) { - $request_body = ($args ? $args[0] : null); + if (in_array($name, $this->methods)) { $query_params = ((count($args) >= 2) ? $args[1] : null); $url = $this->_build_url($query_params); + $request_body = ($args ? $args[0] : null); $request_headers = ((count($args) == 3) ? $args[2] : null); return $this->_make_request($name, $url, $request_body, $request_headers); } return $this->_($name); } - - /** - * Add the version to the path - */ - public function version($version) { - $this->_version = $version; - return $this->_(null); - } - - private function _get_version() { - return $this->_version; - } - } ?> diff --git a/php_http_client/config.php b/lib/config.php index ba33bef..2608700 100644 --- a/php_http_client/config.php +++ b/lib/config.php @@ -1,4 +1,5 @@ <?php +namespace SendGrid; class Config{ function __construct($base_path, $config_filename){ $handle = fopen($base_path.'/'.$config_filename, "r"); diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php new file mode 100644 index 0000000..3781668 --- /dev/null +++ b/test/unit/ClientTest.php @@ -0,0 +1,75 @@ +<?php +class MockClient extends SendGrid\Client +{ + public + $request_body, + $request_headers, + $url; + + public function _make_request($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 +{ + protected + $client, + $host, + $headers; + + protected function setUp() + { + $this->host = "https://localhost:4010"; + $this->headers = array( + 'Content-Type: application/json', + 'Authorization: Bearer SG.XXXX' + ); + $this->client = new MockClient($this->host, $this->headers, "3", null); + } + + public function testInitialization() + { + $this->assertEquals($this->client->host, $this->host); + $this->assertEquals($this->client->request_headers, $this->headers); + $this->assertEquals($this->client->version, "3"); + $this->assertEquals($this->client->url_path, []); + $this->assertEquals($this->client->methods, ['delete', 'get', 'patch', 'post', 'put']); + } + + public function test_() + { + $client = $this->client->_("test"); + $this->assertEquals($client->url_path, array("test")); + } + + public function test__call() + { + $client = $this->client->get(); + $this->assertEquals($client->url, "https://localhost:4010/v3/"); + + $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"); + + $request_body = array('name' => 'A New Hope'); + $client = $this->client->get($request_body); + $this->assertEquals($client->request_body, $request_body); + + $request_headers = array('X-Mock: 200'); + $client = $this->client->get(null, null, $request_headers); + $this->assertEquals($client->request_headers, $request_headers); + + $client = $this->client->version("4"); + $this->assertEquals($client->version, "4"); + + $client = $this->client->path_to_endpoint(); + $this->assertEquals($client->url_path, array("path_to_endpoint")); + $client = $client->one_more_segment(); + $this->assertEquals($client->url_path, array("path_to_endpoint", "one_more_segment")); + } +} +?>
\ No newline at end of file diff --git a/test/unit/ConfigTest.php b/test/unit/ConfigTest.php new file mode 100644 index 0000000..569d915 --- /dev/null +++ b/test/unit/ConfigTest.php @@ -0,0 +1,24 @@ +<?php +class ConfigTest_Config extends PHPUnit_Framework_TestCase +{ + protected + $config, + $base_path, + $config_filename; + + protected function setUp() + { + $this->base_path = dirname(".."); + $this->config_filename = '.env_sample'; + $this->config = new SendGrid\Config($this->base_path, $this->config_filename); + } + + public function testInitialization() + { + $this->assertEquals($api_key = getenv('SENDGRID_API_KEY'), "<your_sendgrid_api_key>"); + $this->assertEquals($api_key = getenv('HOST'), "<base_url_for_live_api_host>"); + $this->assertEquals($api_key = getenv('MOCK_HOST'), "<base_url_for_remote_mocked_api_host>"); + $this->assertEquals($api_key = getenv('LOCAL_HOST'), "<base_url_for_local_mocked_api_host>"); + } +} +?>
\ No newline at end of file diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php new file mode 100644 index 0000000..af10b46 --- /dev/null +++ b/test/unit/bootstrap.php @@ -0,0 +1,17 @@ +<?php +include(dirname(dirname(__FILE__)) . '/../lib/client.php'); +include(dirname(dirname(__FILE__)) . '/../lib/config.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 |