summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md3
-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
7 files changed, 70 insertions, 17 deletions
diff --git a/README.md b/README.md
index c8ea169..99fa423 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,7 @@ $sparky = new SparkPost($httpAdapter, ['key'=>'YOUR API KEY']);
```
## Getting Started: Your First Mailing
+For this example to work as is, [Guzzle 6 will need to be installed](http://docs.guzzlephp.org/en/latest/overview.html#installation). Otherwise another adapter can be used for your specific setup. See "Setting up a Request Adapter" above.
```php
require 'vendor/autoload.php';
@@ -72,7 +73,7 @@ try {
'from'=>'From Envelope <from@sparkpostbox.com>',
'html'=>'<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
'text'=>'Congratulations, {{name}}!! You just sent your very first mailing!',
- 'substitutionData'=>['name'=>'YOUR FIRST NAME']
+ 'substitutionData'=>['name'=>'YOUR FIRST NAME'],
'subject'=>'First Mailing From PHP',
'recipients'=>[
[
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));