Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
25 changes: 25 additions & 0 deletions examples/unwrapped/get_webhooks.php
Original file line number Diff line number Diff line change
@@ -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();
}
?>
31 changes: 19 additions & 12 deletions lib/SparkPost/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Ivory\HttpAdapter\HttpAdapterException;
use SparkPost\SparkPost;



/**
* @desc SDK interface for managing SparkPost API endpoints
*/
Expand Down Expand Up @@ -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());
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/SparkPost/APIResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SparkPost;

class APIResponseException extends \Exception {

}

?>
13 changes: 10 additions & 3 deletions test/unit/APIResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
Expand All @@ -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'));
Expand All @@ -88,16 +90,19 @@ 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'));
}

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');
}
Expand All @@ -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');
}
Expand Down
4 changes: 4 additions & 0 deletions test/unit/TransmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand Down