From 547f84d69ab1ca529b112aab43aa6f5b901aaa25 Mon Sep 17 00:00:00 2001 From: beardyman Date: Tue, 23 Feb 2016 00:28:59 -0500 Subject: [PATCH 1/5] added logic to handle error responses --- lib/SparkPost/APIResource.php | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index a2cd5a6..e946c0f 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -3,6 +3,8 @@ use Ivory\HttpAdapter\HttpAdapterException; use SparkPost\SparkPost; + + /** * @desc SDK interface for managing SparkPost API endpoints */ @@ -186,24 +188,29 @@ private function callResource( $action, $resourcePath=null, $options=[] ) { //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()); } } From 267b9c7c3c856fc081450cc8f796dd38ce664f39 Mon Sep 17 00:00:00 2001 From: beardyman Date: Tue, 23 Feb 2016 00:31:15 -0500 Subject: [PATCH 2/5] added new exception type and unwrapped example for webhooks --- examples/unwrapped/get_webhooks.php | 25 +++++++++++++++++++++++++ lib/SparkPost/APIResponseException.php | 9 +++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/unwrapped/get_webhooks.php create mode 100644 lib/SparkPost/APIResponseException.php 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 @@ +$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/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 @@ + From 8cb5dd91551ac9e01fe926f5af4fec04137b8b71 Mon Sep 17 00:00:00 2001 From: beardyman Date: Tue, 23 Feb 2016 01:02:50 -0500 Subject: [PATCH 3/5] Updated unit tests --- test/unit/APIResourceTest.php | 13 ++++++++++--- test/unit/TransmissionTest.php | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) 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 @@ public function testCreate() { 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 @@ public function testUpdate() { 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 @@ public function testGet() { 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 @@ public function testDelete() { 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 @@ public function testDelete() { 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 @@ public function testAdapter404Exception() { 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 @@ public function testSend() { 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 @@ public function testAllWithFilter() { 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 @@ public function testAllWithOutFilter() { 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 @@ public function testFind() { once()-> with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)-> andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); From 2f7cf39694e025c696aeb3e13eb4dc2664eb7e6b Mon Sep 17 00:00:00 2001 From: beardyman Date: Tue, 23 Feb 2016 01:05:53 -0500 Subject: [PATCH 4/5] bumped version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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." From dad35ca70de8bbcdbd3a53e3deb761f961221dea Mon Sep 17 00:00:00 2001 From: Jordan Nornhold Date: Thu, 25 Feb 2016 16:09:50 -0500 Subject: [PATCH 5/5] Updated concatenations to have spaces --- lib/SparkPost/APIResource.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index e946c0f..563a56e 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -198,7 +198,7 @@ private function callResource( $action, $resourcePath=null, $options=[] ) { 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 APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode ); } } @@ -210,7 +210,7 @@ private function callResource( $action, $resourcePath=null, $options=[] ) { throw $exception; } - throw new APIResponseException('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage()); + throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage()); } }