summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElmer Thomas <elmer@thinkingserious.com>2016-03-20 14:59:34 -0700
committerElmer Thomas <elmer@thinkingserious.com>2016-03-20 14:59:34 -0700
commit87cf790028f26c9a9c24eb57ea1db5bab8900034 (patch)
treea8ff1692151fe2fd90c7ad2d3e392be89dba9fe9
parente5ab6e0e867f58452dbdd03ee0469c48c9c841fe (diff)
downloadphp-http-client-87cf790028f26c9a9c24eb57ea1db5bab8900034.zip
php-http-client-87cf790028f26c9a9c24eb57ea1db5bab8900034.tar.gz
php-http-client-87cf790028f26c9a9c24eb57ea1db5bab8900034.tar.bz2
Added comments and formatting based on php_codesniffer
-rw-r--r--composer.json3
-rw-r--r--examples/example.php42
-rw-r--r--lib/client.php386
-rw-r--r--lib/config.php38
-rw-r--r--test/unit/ClientTest.php63
-rw-r--r--test/unit/bootstrap.php16
6 files changed, 303 insertions, 245 deletions
diff --git a/composer.json b/composer.json
index bf4d4bd..cd4a1b6 100644
--- a/composer.json
+++ b/composer.json
@@ -30,7 +30,8 @@
"source": "https://github.com/php-http-client"
},
"require-dev": {
- "phpunit/phpunit": "~4.4"
+ "phpunit/phpunit": "~4.4",
+ "squizlabs/php_codesniffer": "2.*"
},
"autoload": {
"psr-0": {"SendGrid": "lib/"}
diff --git a/examples/example.php b/examples/example.php
index 48fcf61..590382b 100644
--- a/examples/example.php
+++ b/examples/example.php
@@ -7,15 +7,15 @@ $headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$api_key
);
-$client = new SendGrid\Client("https://e9sk3d3bfaikbpdq7.stoplight-proxy.io", $headers, "3", null);
+$client = new SendGrid\Client('https://e9sk3d3bfaikbpdq7.stoplight-proxy.io', $headers, '/v3', null);
# GET Collection
$query_params = array('limit' => 100, 'offset' => 0);
$request_headers = array('X-Mock: 200');
$response = $client->api_keys()->get(null, $query_params, $request_headers);
-echo $response->status_code();
-echo $response->response_body();
-echo $response->response_headers();
+echo $response->statusCode();
+echo $response->responseBody();
+echo $response->responseHeaders();
# POST
$request_body = array(
@@ -27,26 +27,26 @@ $request_body = array(
)
);
$response = $client->api_keys()->post($request_body);
-echo $response->status_code();
-echo $response->response_body();
-echo $response->response_headers();
-$response_body = json_decode($response->response_body());
+echo $response->statusCode();
+echo $response->responseBody();
+echo $response->responseHeaders();
+$response_body = json_decode($response->responseBody());
$api_key_id = $response_body->api_key_id;
# GET Single
-$response = $client->version('3')->api_keys()->_($api_key_id)->get();
-echo $response->status_code();
-echo $response->response_body();
-echo $response->response_headers();
+$response = $client->version('/v3')->api_keys()->_($api_key_id)->get();
+echo $response->statusCode();
+echo $response->responseBody();
+echo $response->responseHeaders();
# PATCH
$request_body = array(
'name' => 'A New Hope'
);
$response = $client->api_keys()->_($api_key_id)->patch($request_body);
-echo $response->status_code();
-echo $response->response_body();
-echo $response->response_headers();
+echo $response->statusCode();
+echo $response->responseBody();
+echo $response->responseHeaders();
# PUT
$request_body = array(
@@ -57,14 +57,14 @@ $request_body = array(
)
);
$response = $client->api_keys()->_($api_key_id)->put($request_body);
-echo $response->status_code();
-echo $response->response_body();
-echo $response->response_headers();
+echo $response->statusCode();
+echo $response->responseBody();
+echo $response->responseHeaders();
# DELETE
$response = $client->api_keys()->_($api_key_id)->delete();
-echo $response->status_code();
-echo $response->response_body();
-echo $response->response_headers();
+echo $response->statusCode();
+echo $response->responseBody();
+echo $response->responseHeaders();
?> \ No newline at end of file
diff --git a/lib/client.php b/lib/client.php
index 602d53b..805ff45 100644
--- a/lib/client.php
+++ b/lib/client.php
@@ -1,198 +1,230 @@
<?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;
-class Response {
- function __construct($status_code = null, $response_body = null, $response_headers = null){
- $this->_status_code = $status_code;
- $this->_response_body = $response_body;
- $this->_response_headers = $response_headers;
- }
-
- /**
- * @return: integer, status code of API call
- */
- public function status_code() {
- return $this->_status_code;
- }
-
- /**
- * @return: response from the API
- */
- public function response_body() {
- return $this->_response_body;
- }
-
- /**
- * @return: dict of response headers
- */
- public function response_headers() {
- return $this->_response_headers;
- }
-}
-
-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)
- @type host: string
-
- @param request_headers: A dictionary of the headers you want applied on all calls
- @type request_headers: dictionary
-
- @param version: The version number of the API.
- @type integer:
+/**
+ * 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->_response_body = $response_body;
+ $this->_response_headers = $response_headers;
+ }
+
+ /**
+ * The status code
+ *
+ * @return integer
*/
+ public function statusCode()
+ {
+ return $this->_status_code;
+ }
- $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 : []);
- # These are the supported HTTP verbs
- $this->methods = ['delete', 'get', 'patch', 'post', 'put'];
- }
-
- /**
- * Takes the method chained call and adds to the url path.
- * @param name: The name of the method call
- * @type name: string
+ /**
+ * The response body
+ *
+ * @return array
*/
- private function _build_client($name = null) {
- if(isset($name)){
- array_push($this->url_path, $name);
+ public function responseBody()
+ {
+ return $this->_response_body;
}
- $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
- * (e.g. client._('/v3'))
- * @param url: URI portion of the full URL being requested
- * @type url: string
- * @return: string
- */
- private function _build_versioned_url($url) {
- return sprintf("%s/v%d%s", $this->host, $this->get_version(), $url);
- }
-
- /**
- * Build the final URL to be passed
- * @param query_params: A dictionary of all the query parameters
- * @type query_params: dictionary
- * @return:
- */
- private function _build_url($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);
+ /**
+ * The response headers
+ *
+ * @return array
+ */
+ public function responseHeaders()
+ {
+ return $this->_response_headers;
}
+}
+
+/**
+ * Quickly and easily access any REST or REST-like API.
+ */
+class Client
+{
- if (null != $this->get_version()) {
- $url = $this->_build_versioned_url($url);
- } else {
- $url = sprintf('%s%s', $this->host, $url);;
+ 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'];
}
- return $url;
- }
-
- /**
- * Build the headers for the request
- * @param request_headers: headers to set for the API call
- * @type response: dict
- * @return:
- */
- private function _set_headers($request_headers) {
- $this->request_headers = array_merge($this->request_headers, $request_headers);
- }
+ /**
+ * 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);
+ }
- /**
- * 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 opener:
- * @type opener:
- * @param request: url payload to request
- * @type request: urllib.Request object
- * @return:
- */
- 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);
- 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));
+ /**
+ * 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;
}
- if(isset($request_headers)){
- $this->request_headers = array_merge($this->request_headers, $request_headers);
+
+ /**
+ * 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_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);
+ $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);
}
- 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)
- */
- public function _($name){
- return $this->_build_client($name);
- }
+ /**
+ * 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())
- */
- public function __call($name, $args){
+ /**
+ * 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($name == 'version'){
- return version($name);
- }
-
- 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);
+ 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);
}
-
- return $this->_($name);
- }
}
?>
diff --git a/lib/config.php b/lib/config.php
index 2608700..5c4383e 100644
--- a/lib/config.php
+++ b/lib/config.php
@@ -1,12 +1,36 @@
<?php
+/**
+ * Environment Variable Configuration
+ *
+ * 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;
-class Config{
- function __construct($base_path, $config_filename){
- $handle = fopen($base_path.'/'.$config_filename, "r");
- while (($line = fgets($handle)) !== false) {
- putenv(trim(preg_replace('/\s+/', ' ', $line)));
+
+/**
+ * Sets environment variables.
+ */
+class Config
+{
+ /**
+ * Setup the environment variables
+ *
+ * @param string $base_path path to your config file.
+ * @param string $config_filename name of the config file.
+ */
+ function __construct($base_path, $config_filename)
+ {
+ $handle = fopen($base_path.'/'.$config_filename, "r");
+ while (($line = fgets($handle)) !== false) {
+ putenv(trim(preg_replace('/\s+/', ' ', $line)));
+ }
+ fclose($handle);
}
- fclose($handle);
- }
}
?>
diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php
index 3781668..d06962f 100644
--- a/test/unit/ClientTest.php
+++ b/test/unit/ClientTest.php
@@ -1,17 +1,18 @@
<?php
class MockClient extends SendGrid\Client
{
- public
- $request_body,
- $request_headers,
- $url;
+ 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;
- }
+ 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
@@ -28,48 +29,48 @@ class ClientTest_Client extends PHPUnit_Framework_TestCase
'Content-Type: application/json',
'Authorization: Bearer SG.XXXX'
);
- $this->client = new MockClient($this->host, $this->headers, "3", null);
+ $this->client = new MockClient($this->host, $this->headers, "/v3", 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->version, "/v3");
$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"));
+ $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/");
+ $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");
+ $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_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);
+ $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->version("/v4");
+ $this->assertEquals($client->version, "/v4");
- $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"));
+ $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/bootstrap.php b/test/unit/bootstrap.php
index af10b46..8b4e24a 100644
--- a/test/unit/bootstrap.php
+++ b/test/unit/bootstrap.php
@@ -4,14 +4,14 @@ 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');
- }
+ 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