summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Leland <rich@richleland.com>2016-02-25 16:38:28 -0500
committerRichard Leland <rich@richleland.com>2016-02-25 16:38:28 -0500
commit3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8 (patch)
tree79dc5fb54be024a023e02340e12be34614cb12fe
parent7ff00e6104ac19d9a2fe20a8a03df469cb6910f6 (diff)
parentdad35ca70de8bbcdbd3a53e3deb761f961221dea (diff)
downloadphp-sparkpost-3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8.zip
php-sparkpost-3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8.tar.gz
php-sparkpost-3c4f858f9d099ae753993a3b2997d1bc1c2dd1d8.tar.bz2
Merge pull request #39 from SparkPost/issue38
version bump, error response handling
-rw-r--r--composer.json2
-rw-r--r--examples/unwrapped/get_webhooks.php25
-rw-r--r--lib/SparkPost/APIResource.php31
-rw-r--r--lib/SparkPost/APIResponseException.php9
-rw-r--r--test/unit/APIResourceTest.php13
-rw-r--r--test/unit/TransmissionTest.php4
6 files changed, 68 insertions, 16 deletions
diff --git a/composer.json b/composer.json
index b8233e0..3c9d0ee 100644
--- a/composer.json
+++ b/composer.json
@@ -2,7 +2,7 @@
"name": "sparkpost/php-sparkpost",
"description": "Client library for interfacing with the SparkPost API.",
"license": "Apache 2.0",
- "version": "1.0.0",
+ "version": "1.0.1",
"authors": [
{
"name": "Message Systems, Inc."
diff --git a/examples/unwrapped/get_webhooks.php b/examples/unwrapped/get_webhooks.php
new file mode 100644
index 0000000..b9ed723
--- /dev/null
+++ b/examples/unwrapped/get_webhooks.php
@@ -0,0 +1,25 @@
+<?php
+namespace Examples\Unwrapped;
+require_once (dirname(__FILE__).'/../bootstrap.php');
+
+//pull in API key config
+$configFile = file_get_contents(dirname(__FILE__) . '/../example-config.json');
+$config = json_decode($configFile, true);
+
+use SparkPost\SparkPost;
+use GuzzleHttp\Client;
+use Ivory\HttpAdapter\Guzzle6HttpAdapter;
+
+$httpAdapter = new Guzzle6HttpAdapter(new Client());
+$sparky = new SparkPost($httpAdapter, ['key'=>$config['api-key']]);
+
+try {
+ $sparky->setupUnwrapped('webhooks');
+
+ $results = $sparky->webhooks->get();
+
+ echo 'Congrats you can use your SDK!';
+} catch (\Exception $exception) {
+ echo $exception->getMessage();
+}
+?>
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index a2cd5a6..563a56e 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -3,6 +3,8 @@ namespace SparkPost;
use Ivory\HttpAdapter\HttpAdapterException;
use SparkPost\SparkPost;
+
+
/**
* @desc SDK interface for managing SparkPost API endpoints
*/
@@ -186,24 +188,29 @@ class APIResource {
//make request
try {
$response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
- return json_decode($response->getBody()->getContents(), true);
- }
- /*
- * Handles 4XX responses
- */
- catch (HttpAdapterException $exception) {
- $response = $exception->getResponse();
+
$statusCode = $response->getStatusCode();
- if($statusCode === 404) {
- throw new \Exception('The specified resource does not exist', 404);
+
+ // Handle 4XX responses, 5XX responses will throw an HttpAdapterException
+ if ($statusCode < 400) {
+ return json_decode($response->getBody()->getContents(), true);
+ } else {
+ if ($statusCode === 404) {
+ throw new APIResponseException('The specified resource does not exist', 404);
+ }
+ throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode );
}
- throw new \Exception('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode );
}
+
/*
- * Handles 5XX Errors, Configuration Errors, and a catch all for other errors
+ * Configuration Errors, and a catch all for other errors
*/
catch (\Exception $exception) {
- throw new \Exception('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage());
+ if($exception instanceof APIResponseException) {
+ throw $exception;
+ }
+
+ throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
}
}
diff --git a/lib/SparkPost/APIResponseException.php b/lib/SparkPost/APIResponseException.php
new file mode 100644
index 0000000..cc0842c
--- /dev/null
+++ b/lib/SparkPost/APIResponseException.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace SparkPost;
+
+class APIResponseException extends \Exception {
+
+}
+
+?>
diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php
index 8253b1e..18b0d3f 100644
--- a/test/unit/APIResourceTest.php
+++ b/test/unit/APIResourceTest.php
@@ -51,9 +51,9 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
once()->
with(Mockery::type('string'), 'POST', Mockery::type('array'), json_encode($testInput))->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
-
$this->assertEquals($testBody, $this->resource->create($testInput));
}
@@ -65,6 +65,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*\/test/', 'PUT', Mockery::type('array'), json_encode($testInput))->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
$this->assertEquals($testBody, $this->resource->update('test', $testInput));
@@ -77,6 +78,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*\/test/', 'GET', Mockery::type('array'), null)->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
$this->assertEquals($testBody, $this->resource->get('test'));
@@ -88,6 +90,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*\/test/', 'DELETE', Mockery::type('array'), null)->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn('');
$this->assertEquals(null, $this->resource->delete('test'));
@@ -95,9 +98,11 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
public function testAdapter404Exception() {
try {
+ $responseMock = Mockery::mock();
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
once()->
- andThrow($this->getExceptionMock(404));
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(404);
$this->resource->get('test');
}
@@ -108,9 +113,11 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
public function testAdapter4XXException() {
try {
+ $responseMock = Mockery::mock();
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
once()->
- andThrow($this->getExceptionMock(400));
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(400);
$this->resource->get('test');
}
diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php
index d262ee0..a985883 100644
--- a/test/unit/TransmissionTest.php
+++ b/test/unit/TransmissionTest.php
@@ -37,6 +37,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
@@ -50,6 +51,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
@@ -63,6 +65,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
@@ -76,6 +79,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
once()->
with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)->
andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(200);
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));