summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElmer Thomas <elmer@thinkingserious.com>2016-06-06 19:02:46 -0700
committerElmer Thomas <elmer@thinkingserious.com>2016-06-06 19:02:46 -0700
commitb808300d9b9e79234565be52a8cce5bdc458f8ea (patch)
tree2d667baa7f09305da914ffdfbd6359ab020079a9
parent988698f5b2f114d0a62472b1012edac20e37eca1 (diff)
downloadphp-http-client-b808300d9b9e79234565be52a8cce5bdc458f8ea.zip
php-http-client-b808300d9b9e79234565be52a8cce5bdc458f8ea.tar.gz
php-http-client-b808300d9b9e79234565be52a8cce5bdc458f8ea.tar.bz2
Version Bump v3.0.0: Made the Request and Response variables non-redundant. e.g. request.requestBody becomes request.bodyv3.0.0
-rw-r--r--.env_sample4
-rw-r--r--CHANGELOG.md4
-rw-r--r--README.md8
-rw-r--r--composer.json2
-rw-r--r--examples/example.php28
-rw-r--r--lib/SendGrid/Client.php54
-rw-r--r--lib/SendGrid/Config.php36
-rw-r--r--test/unit/ConfigTest.php28
-rw-r--r--test/unit/bootstrap.php1
9 files changed, 49 insertions, 116 deletions
diff --git a/.env_sample b/.env_sample
deleted file mode 100644
index f1a63f3..0000000
--- a/.env_sample
+++ /dev/null
@@ -1,4 +0,0 @@
-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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95f2a35..c81d435 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
+## [3.0.0] - 2016-06-06
+### Changed
+- Made the Request and Response variables non-redundant. e.g. request.requestBody becomes request.body
+
## [2.0.2] - 2016-02-29
### Fixed
- Renaming files to conform to PSR-0, git ignored the case in 2.0.1
diff --git a/README.md b/README.md
index 161bd01..981125a 100644
--- a/README.md
+++ b/README.md
@@ -42,8 +42,8 @@ $global_headers = array(Authorization: Basic XXXXXXX);
$client = SendGrid\Client('base_url', 'global_headers');
$response = $client->your()->api()->_($param)->call()->get();
print $response->statusCode();
-print $response->responseHeaders();
-print $response->responseBody();
+print $response->headers();
+print $response->body();
```
`POST /your/api/{param}/call` with headers, query parameters and a request body with versioning.
@@ -59,8 +59,8 @@ $response = $client->your()->api()->_($param)->call()->post('data',
'query_params',
'request_headers');
print $response->statusCode();
-print $response->responseHeaders();
-print $response->responseBody();
+print $response->headers();
+print $response->body();
```
# Usage
diff --git a/composer.json b/composer.json
index 62f80da..88d6761 100644
--- a/composer.json
+++ b/composer.json
@@ -2,7 +2,7 @@
"name": "sendgrid/php-http-client",
"description": "HTTP REST client, simplified for PHP",
"type": "library",
- "version": "2.0.2",
+ "version": "3.0.0",
"require-dev": {
"phpunit/phpunit": "~4.4",
"squizlabs/php_codesniffer": "2.*"
diff --git a/examples/example.php b/examples/example.php
index abf2e62..9e414eb 100644
--- a/examples/example.php
+++ b/examples/example.php
@@ -3,10 +3,8 @@
// comment out the two includes below
// require __DIR__ . '/vendor/autoload.php';
include(dirname(__DIR__).'/lib/SendGrid/client.php');
-include(dirname(__DIR__).'/lib/SendGrid/config.php');
// This gets the parent directory, for your current directory use getcwd()
$path_to_config = dirname(__DIR__);
-$config = new SendGrid\Config($path_to_config, '.env');
$api_key = getenv('SENDGRID_API_KEY');
$headers = array(
'Content-Type: application/json',
@@ -19,8 +17,8 @@ $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->statusCode();
-echo $response->responseBody();
-echo $response->responseHeaders();
+echo $response->body();
+echo $response->headers();
// POST
$request_body = array(
@@ -33,16 +31,16 @@ $request_body = array(
);
$response = $client->api_keys()->post($request_body);
echo $response->statusCode();
-echo $response->responseBody();
-echo $response->responseHeaders();
-$response_body = json_decode($response->responseBody());
+echo $response->body();
+echo $response->headers();
+$response_body = json_decode($response->body());
$api_key_id = $response_body->api_key_id;
// GET Single
$response = $client->version('/v3')->api_keys()->_($api_key_id)->get();
echo $response->statusCode();
-echo $response->responseBody();
-echo $response->responseHeaders();
+echo $response->body();
+echo $response->headers();
// PATCH
$request_body = array(
@@ -50,8 +48,8 @@ $request_body = array(
);
$response = $client->api_keys()->_($api_key_id)->patch($request_body);
echo $response->statusCode();
-echo $response->responseBody();
-echo $response->responseHeaders();
+echo $response->body();
+echo $response->headers();
// PUT
$request_body = array(
@@ -63,13 +61,13 @@ $request_body = array(
);
$response = $client->api_keys()->_($api_key_id)->put($request_body);
echo $response->statusCode();
-echo $response->responseBody();
-echo $response->responseHeaders();
+echo $response->body();
+echo $response->headers();
// DELETE
$response = $client->api_keys()->_($api_key_id)->delete();
echo $response->statusCode();
-echo $response->responseBody();
-echo $response->responseHeaders();
+echo $response->body();
+echo $response->headers();
?> \ No newline at end of file
diff --git a/lib/SendGrid/Client.php b/lib/SendGrid/Client.php
index 805ff45..860501c 100644
--- a/lib/SendGrid/Client.php
+++ b/lib/SendGrid/Client.php
@@ -1,5 +1,5 @@
<?php
-/**
+/**
* HTTP Client library
*
* PHP version 5.2
@@ -28,10 +28,10 @@ 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;
+ $this->_body = $response_body;
+ $this->_headers = $response_headers;
}
-
+
/**
* The status code
*
@@ -47,9 +47,9 @@ class Response
*
* @return array
*/
- public function responseBody()
+ public function body()
{
- return $this->_response_body;
+ return $this->_body;
}
/**
@@ -57,9 +57,9 @@ class Response
*
* @return array
*/
- public function responseHeaders()
+ public function headers()
{
- return $this->_response_headers;
+ return $this->_headers;
}
}
@@ -68,14 +68,14 @@ class Response
*/
class Client
{
-
- public
+
+ public
$host,
$request_headers,
$version,
$url_path,
- $methods;
-
+ $methods;
+
/**
* Initialize the client
*
@@ -83,7 +83,7 @@ class Client
* @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;
@@ -110,7 +110,7 @@ class Client
$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
@@ -120,7 +120,7 @@ class Client
*
* @return string
*/
- private function _buildVersionedUrl($url)
+ private function _buildVersionedUrl($url)
{
return sprintf("%s%s%s", $this->host, $this->version, $url);
}
@@ -129,10 +129,10 @@ class Client
* 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)
+ private function _buildUrl($query_params = null)
{
$url = '/'.implode('/', $this->url_path);
if (isset($query_params)) {
@@ -155,10 +155,10 @@ class Client
* @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)
+ public function makeRequest($method, $url, $request_body = null, $request_headers = null)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
@@ -179,9 +179,9 @@ class Client
$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);
}
@@ -192,10 +192,10 @@ class Client
* in your url, you must use this method.
*
* @param string $name name of the url segment
- *
+ *
* @return Client object
*/
- public function _($name = null)
+ public function _($name = null)
{
return $this->_buildClient($name);
}
@@ -206,16 +206,16 @@ class Client
*
* @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);
@@ -223,7 +223,7 @@ class Client
$request_headers = ((count($args) == 3) ? $args[2] : null);
return $this->makeRequest($name, $url, $request_body, $request_headers);
}
-
+
return $this->_($name);
}
}
diff --git a/lib/SendGrid/Config.php b/lib/SendGrid/Config.php
deleted file mode 100644
index 5c4383e..0000000
--- a/lib/SendGrid/Config.php
+++ /dev/null
@@ -1,36 +0,0 @@
-<?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;
-
-/**
- * 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);
- }
-}
-?>
diff --git a/test/unit/ConfigTest.php b/test/unit/ConfigTest.php
deleted file mode 100644
index 3ad84f2..0000000
--- a/test/unit/ConfigTest.php
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-class ConfigTest_Config extends PHPUnit_Framework_TestCase
-{
- protected
- $config,
- $base_path,
- $config_filename;
-
- protected function setUp()
- {
- if(getenv('TRAVIS')) {
- $this->base_path = '/home/travis/build/sendgrid/php-http-client/';
- } else {
- $this->base_path = basename(__DIR__).'../..';
- }
- $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
index e0da9a5..71a1608 100644
--- a/test/unit/bootstrap.php
+++ b/test/unit/bootstrap.php
@@ -1,6 +1,5 @@
<?php
include(dirname(dirname(__FILE__)) . '/../lib/SendGrid/client.php');
-include(dirname(dirname(__FILE__)) . '/../lib/SendGrid/config.php');
require __DIR__ . '/../../vendor/autoload.php';
function autoload_tests($class)
{