Skip to content

Commit 3c4f858

Browse files
committed
Merge pull request #39 from SparkPost/issue38
version bump, error response handling
2 parents 7ff00e6 + dad35ca commit 3c4f858

File tree

6 files changed

+68
-16
lines changed

6 files changed

+68
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sparkpost/php-sparkpost",
33
"description": "Client library for interfacing with the SparkPost API.",
44
"license": "Apache 2.0",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"authors": [
77
{
88
"name": "Message Systems, Inc."
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Examples\Unwrapped;
3+
require_once (dirname(__FILE__).'/../bootstrap.php');
4+
5+
//pull in API key config
6+
$configFile = file_get_contents(dirname(__FILE__) . '/../example-config.json');
7+
$config = json_decode($configFile, true);
8+
9+
use SparkPost\SparkPost;
10+
use GuzzleHttp\Client;
11+
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
12+
13+
$httpAdapter = new Guzzle6HttpAdapter(new Client());
14+
$sparky = new SparkPost($httpAdapter, ['key'=>$config['api-key']]);
15+
16+
try {
17+
$sparky->setupUnwrapped('webhooks');
18+
19+
$results = $sparky->webhooks->get();
20+
21+
echo 'Congrats you can use your SDK!';
22+
} catch (\Exception $exception) {
23+
echo $exception->getMessage();
24+
}
25+
?>

lib/SparkPost/APIResource.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use Ivory\HttpAdapter\HttpAdapterException;
44
use SparkPost\SparkPost;
55

6+
7+
68
/**
79
* @desc SDK interface for managing SparkPost API endpoints
810
*/
@@ -186,24 +188,29 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
186188
//make request
187189
try {
188190
$response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
189-
return json_decode($response->getBody()->getContents(), true);
190-
}
191-
/*
192-
* Handles 4XX responses
193-
*/
194-
catch (HttpAdapterException $exception) {
195-
$response = $exception->getResponse();
191+
196192
$statusCode = $response->getStatusCode();
197-
if($statusCode === 404) {
198-
throw new \Exception('The specified resource does not exist', 404);
193+
194+
// Handle 4XX responses, 5XX responses will throw an HttpAdapterException
195+
if ($statusCode < 400) {
196+
return json_decode($response->getBody()->getContents(), true);
197+
} else {
198+
if ($statusCode === 404) {
199+
throw new APIResponseException('The specified resource does not exist', 404);
200+
}
201+
throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode );
199202
}
200-
throw new \Exception('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode );
201203
}
204+
202205
/*
203-
* Handles 5XX Errors, Configuration Errors, and a catch all for other errors
206+
* Configuration Errors, and a catch all for other errors
204207
*/
205208
catch (\Exception $exception) {
206-
throw new \Exception('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage());
209+
if($exception instanceof APIResponseException) {
210+
throw $exception;
211+
}
212+
213+
throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
207214
}
208215
}
209216

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace SparkPost;
4+
5+
class APIResponseException extends \Exception {
6+
7+
}
8+
9+
?>

test/unit/APIResourceTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function testCreate() {
5151
once()->
5252
with(Mockery::type('string'), 'POST', Mockery::type('array'), json_encode($testInput))->
5353
andReturn($responseMock);
54+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
5455
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
5556

56-
5757
$this->assertEquals($testBody, $this->resource->create($testInput));
5858
}
5959

@@ -65,6 +65,7 @@ public function testUpdate() {
6565
once()->
6666
with('/.*\/test/', 'PUT', Mockery::type('array'), json_encode($testInput))->
6767
andReturn($responseMock);
68+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
6869
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
6970

7071
$this->assertEquals($testBody, $this->resource->update('test', $testInput));
@@ -77,6 +78,7 @@ public function testGet() {
7778
once()->
7879
with('/.*\/test/', 'GET', Mockery::type('array'), null)->
7980
andReturn($responseMock);
81+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
8082
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
8183

8284
$this->assertEquals($testBody, $this->resource->get('test'));
@@ -88,16 +90,19 @@ public function testDelete() {
8890
once()->
8991
with('/.*\/test/', 'DELETE', Mockery::type('array'), null)->
9092
andReturn($responseMock);
93+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
9194
$responseMock->shouldReceive('getBody->getContents')->andReturn('');
9295

9396
$this->assertEquals(null, $this->resource->delete('test'));
9497
}
9598

9699
public function testAdapter404Exception() {
97100
try {
101+
$responseMock = Mockery::mock();
98102
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
99103
once()->
100-
andThrow($this->getExceptionMock(404));
104+
andReturn($responseMock);
105+
$responseMock->shouldReceive('getStatusCode')->andReturn(404);
101106

102107
$this->resource->get('test');
103108
}
@@ -108,9 +113,11 @@ public function testAdapter404Exception() {
108113

109114
public function testAdapter4XXException() {
110115
try {
116+
$responseMock = Mockery::mock();
111117
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
112118
once()->
113-
andThrow($this->getExceptionMock(400));
119+
andReturn($responseMock);
120+
$responseMock->shouldReceive('getStatusCode')->andReturn(400);
114121

115122
$this->resource->get('test');
116123
}

test/unit/TransmissionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testSend() {
3737
once()->
3838
with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))->
3939
andReturn($responseMock);
40+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
4041

4142
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
4243

@@ -50,6 +51,7 @@ public function testAllWithFilter() {
5051
once()->
5152
with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)->
5253
andReturn($responseMock);
54+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
5355

5456
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
5557

@@ -63,6 +65,7 @@ public function testAllWithOutFilter() {
6365
once()->
6466
with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)->
6567
andReturn($responseMock);
68+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
6669

6770
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
6871

@@ -76,6 +79,7 @@ public function testFind() {
7679
once()->
7780
with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)->
7881
andReturn($responseMock);
82+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
7983

8084
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
8185

0 commit comments

Comments
 (0)