diff options
Diffstat (limited to 'lib/SparkPost')
-rw-r--r-- | lib/SparkPost/Response.php | 55 | ||||
-rw-r--r-- | lib/SparkPost/SparkPost.php | 55 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostException.php | 26 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostPromise.php (renamed from lib/SparkPost/Promise.php) | 8 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostResponse.php | 104 |
5 files changed, 173 insertions, 75 deletions
diff --git a/lib/SparkPost/Response.php b/lib/SparkPost/Response.php deleted file mode 100644 index c2751b6..0000000 --- a/lib/SparkPost/Response.php +++ /dev/null @@ -1,55 +0,0 @@ -<?php - -namespace SparkPost; - -use Psr\Http\Message\ResponseInterface as ResponseInterface; -use GuzzleHttp\Psr7\Response as HttpResponse; -use GuzzleHttp\Psr7\MessageTrait; - -class Response implements ResponseInterface { - - use MessageTrait; - - private $response; - - public function __construct(HttpResponse $response) { - $this->response = $response; - } - - public function getBody() - { - $body = $this->response->getBody(); - $body_string = $body->__toString(); - - if (is_string($body_string)) { - $json = json_decode($body_string, true); - - if (json_last_error() == JSON_ERROR_NONE) { - return $json; - } - else { - return $body; - } - } - - return $body; - } - - public function getStatusCode() - { - return $this->response->getStatusCode(); - } - - public function withStatus($code, $reasonPhrase = '') - { - return $this->response->withStatus($code, $reasonPhrase); - } - - public function getReasonPhrase() - { - $this->response->getReasonPhrase(); - } - -} - -?>
\ No newline at end of file diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index b37351e..70c75da 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -3,28 +3,26 @@ namespace SparkPost; use Http\Client\HttpClient; +use Http\Client\HttpAsyncClient; use GuzzleHttp\Psr7\Request as Request; -use SparkPost\Promise as SparkPostPromise; class SparkPost { private $version = '2.0.0'; - private $config; public $httpClient; private $options; - public $transmissions; - private static $defaultOptions = [ 'host' => 'api.sparkpost.com', 'protocol' => 'https', 'port' => 443, - 'strictSSL' => true, 'key' => '', 'version' => 'v1', 'timeout' => 10 ]; + public $transmissions; + public function __construct(HttpClient $httpClient, $options) { $this->setOptions($options); @@ -32,12 +30,36 @@ class SparkPost $this->setupEndpoints(); } - public function request($method = '', $uri = '', $payload = [], $headers = []) + public function request($method = 'GET', $uri = '', $payload = [], $headers = []) + { + $request = $this->buildRequest($method, $uri, $payload, $headers); + try + { + return new SparkPostResponse($this->httpClient->sendRequest($request)); + } + catch (\Exception $exception) + { + throw new SparkPostException($exception); + } + } + + public function asyncRequest($method = 'GET', $uri = '', $payload = [], $headers = []) + { + if ($this->httpClient instanceof HttpAsyncClient) { + $request = $this->buildRequest($method, $uri, $payload, $headers); + return new SparkPostPromise($this->httpClient->sendAsyncRequest($request)); + } + else { + throw new Exception('Your http client can not send asynchronous requests.'); + } + } + + private function buildRequest($method, $uri, $payload, $headers) { $method = trim(strtoupper($method)); - if ($method === 'GET') { + if ($method === 'GET'){ $params = $payload; $body = []; } @@ -49,19 +71,21 @@ class SparkPost $url = $this->getUrl($uri, $params); $headers = $this->getHttpHeaders($headers); - $request = new Request($method, $url, $headers, json_encode($body)); - - $promise = $this->httpClient->sendAsyncRequest($request); - - return new SparkPostPromise($promise); + return new Request($method, $url, $headers, json_encode($body)); } - public function getHttpHeaders($headers) + public function getHttpHeaders($headers = []) { - return [ + $constantHeaders = [ 'Authorization' => $this->options['key'], - 'Content-Type' => 'application/json', + 'Content-Type' => 'application/json' ]; + + foreach ($constantHeaders as $key => $value) { + $headers[$key] = $value; + } + + return $headers; } public function getUrl($path, $params) { @@ -71,6 +95,7 @@ class SparkPost if (is_array($params[$index])) $params[$index] = implode(',', $params); } + $paramsString = http_build_query($params); return $options['protocol'].'://'.$options['host'].($options['port'] ? ':'.$options['port'] : '').'/api/'.$options['version'].'/'.$path.($paramsString ? '?'.$paramsString : ''); diff --git a/lib/SparkPost/SparkPostException.php b/lib/SparkPost/SparkPostException.php new file mode 100644 index 0000000..92be3d5 --- /dev/null +++ b/lib/SparkPost/SparkPostException.php @@ -0,0 +1,26 @@ +<?php + +namespace SparkPost; + +use Http\Client\Exception\HttpException as HttpException; + +class SparkPostException extends \Exception { + + private $body = null; + + public function __construct(\Exception $exception) { + $message = $exception->getMessage(); + if($exception instanceof HttpException) { + $message = $exception->getResponse()->getBody()->__toString(); + $this->body = json_decode($message, true); + } + + parent::__construct($message, $exception->getCode(), $exception->getPrevious()); + } + + public function getBody() { + return $this->body; + } +} + +?>
\ No newline at end of file diff --git a/lib/SparkPost/Promise.php b/lib/SparkPost/SparkPostPromise.php index c419597..462599a 100644 --- a/lib/SparkPost/Promise.php +++ b/lib/SparkPost/SparkPostPromise.php @@ -3,10 +3,8 @@ namespace SparkPost; use Http\Promise\Promise as HttpPromise; -use SparkPost\Response as SparkPostResponse; -use SparkPost\Exception as SparkPostException; -class Promise implements HttpPromise +class SparkPostPromise implements HttpPromise { private $promise; @@ -19,7 +17,7 @@ class Promise implements HttpPromise function($response) { $onFulfilled(new SparkPostResponse($response)); }, - function(Exception $exception) { + function(\Exception $exception) { $onRejected(new SparkPostException($exception)); }); } @@ -35,7 +33,7 @@ class Promise implements HttpPromise $response = $this->promise->wait($unwrap); return new SparkPostResponse($response); } - catch (Exception $exception) + catch (\Exception $exception) { throw new SparkPostException($exception); } diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php new file mode 100644 index 0000000..161b864 --- /dev/null +++ b/lib/SparkPost/SparkPostResponse.php @@ -0,0 +1,104 @@ +<?php + +namespace SparkPost; + +use Psr\Http\Message\ResponseInterface as ResponseInterface; +use Psr\Http\Message\StreamInterface as StreamInterface; + +class SparkPostResponse implements ResponseInterface { + + private $response; + + public function __construct(ResponseInterface $response) { + $this->response = $response; + } + + public function getBody() + { + $body = $this->response->getBody(); + $body_string = $body->__toString(); + + if (is_string($body_string)) { + $json = json_decode($body_string, true); + + if (json_last_error() == JSON_ERROR_NONE) { + return $json; + } + else { + return $body; + } + } + + return $body; + } + + // pass these down to the response given in the constructor + + public function getProtocolVersion() + { + return $this->response->getProtocolVersion(); + } + + public function withProtocolVersion($version) + { + return $this->response->withProtocolVersion($version); + } + + public function getHeaders() + { + return $this->response->getHeaders(); + } + + public function hasHeader($name) + { + return $this->response->hasHeader($name); + } + + public function getHeader($name) + { + return $this->response->getHeader($name); + } + + public function getHeaderLine($name) + { + return $this->response->getHeaderLine($name); + } + + public function withHeader($name, $value) + { + return $this->response->withHeader($name, $value); + } + + public function withAddedHeader($name, $value) + { + return $this->response->withAddedHeader($name, $value); + } + + public function withoutHeader($name) + { + return $this->response->withoutHeader($name); + } + + public function withBody(StreamInterface $body) + { + return $this->response->withBody($body); + } + + public function getStatusCode() + { + return $this->response->getStatusCode(); + } + + public function withStatus($code, $reasonPhrase = '') + { + return $this->response->withStatus($code, $reasonPhrase); + } + + public function getReasonPhrase() + { + $this->response->getReasonPhrase(); + } + +} + +?>
\ No newline at end of file |