Skip to content

Commit f4cb526

Browse files
authored
Added debug option
Added debug option
2 parents c54abe8 + 1abdc8b commit f4cb526

14 files changed

+271
-126
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ $sparky = new SparkPost($httpClient, ['key'=>'YOUR_API_KEY']);
100100
* Type: `Boolean`
101101
* Default: `true`
102102
* `async` defines if the `request` function sends an asynchronous or synchronous request. If your client does not support async requests set this to `false`
103-
103+
* `options.debug`
104+
* Required: No
105+
* Type: `Boolean`
106+
* Default: `false`
107+
* If `debug` is true, then then all `SparkPostResponse` and `SparkPostException` instances will return any array of the request values through the function `getRequest`
104108

105109
## Methods
106110
### request(method, uri [, payload [, headers]])
@@ -299,6 +303,8 @@ An exception will be thrown in two cases: there is a problem with the request or
299303
* Returns the exception message
300304
* **getBody()**
301305
* If there is a response body it returns it as an `Array`. Otherwise it returns `null`.
306+
* **getRequest()**
307+
* Returns an array with the request values `method`, `url`, `headers`, `body` when `debug` is `true`
302308

303309

304310
### Contributing

examples/debug/index.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, [
17+
"key" => "YOUR_API_KEY",
18+
// This will expose your API KEY - do not use this in production.
19+
"debug" => true
20+
]);
21+
22+
$promise = $sparky->request('GET', 'templates');
23+
24+
try {
25+
$response = $promise->wait();
26+
27+
var_dump($response);
28+
29+
echo "Request:\n";
30+
print_r($response->getRequest());
31+
32+
echo "Response:\n";
33+
echo $response->getStatusCode()."\n";
34+
print_r($response->getBody())."\n";
35+
} catch (\Exception $e) {
36+
echo "Request:\n";
37+
print_r($e->getRequest());
38+
39+
echo "Exception:\n";
40+
echo $e->getCode()."\n";
41+
echo $e->getMessage()."\n";
42+
}

examples/message-events/get_message_events.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$sparky = new SparkPost($httpClient, $options);
1717

1818
$promise = $sparky->request('GET', 'message-events', [
19-
'campaign_ids' => 'CAMPAIGN_ID'
19+
'campaign_ids' => 'CAMPAIGN_ID',
2020
]);
2121

2222
try {

examples/templates/create_template.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
$sparky = new SparkPost($httpClient, $options);
1717

1818
$promise = $sparky->request('POST', 'templates', [
19-
"name" => "PHP example template",
20-
"content" => [
21-
"from" => "from@YOUR_DOMAIN",
22-
"subject" => "Your Subject",
23-
"html" => "<b>Write your message here.</b>"
24-
]
19+
'name' => 'PHP example template',
20+
'content' => [
21+
'from' => 'from@YOUR_DOMAIN',
22+
'subject' => 'Your Subject',
23+
'html' => '<b>Write your message here.</b>',
24+
],
2525
]);
2626

2727
try {

examples/templates/preview_template.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
$promise = $sparky->request('POST', 'templates/TEMPLATE_ID/preview?draft=true', [
1919
'substitution_data' => [
20-
'some_key' => 'some_value'
21-
]
20+
'some_key' => 'some_value',
21+
],
2222
]);
2323

2424
try {

examples/templates/update_template.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
$promise = $sparky->request('PUT', 'templates/TEMPLATE_ID', [
1919
'options' => [
20-
'open_tracking' => true
21-
]
20+
'open_tracking' => true,
21+
],
2222
]);
2323

2424
try {

lib/SparkPost/Resource.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace SparkPost;
44

55
/**
6-
* Class Resource
7-
* @package SparkPost
6+
* Class Resource.
7+
*
88
* @deprecated Soft reservations placed on name Resource (as of PHP7)
99
*/
1010
class Resource extends ResourceBase

lib/SparkPost/ResourceBase.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace SparkPost;
44

55
/**
6-
* Class ResourceBase
7-
* @package SparkPost
6+
* Class ResourceBase.
87
*/
98
class ResourceBase
109
{

lib/SparkPost/SparkPost.php

+59-13
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
class SparkPost
1212
{
1313
/**
14-
* @var string Library version, used for setting User-Agent.
14+
* @var string Library version, used for setting User-Agent
1515
*/
1616
private $version = '2.0.3';
1717

1818
/**
19-
* @var HttpClient|HttpAsyncClient used to make requests.
19+
* @var HttpClient|HttpAsyncClient used to make requests
2020
*/
2121
private $httpClient;
2222

@@ -26,7 +26,7 @@ class SparkPost
2626
private $messageFactory;
2727

2828
/**
29-
* @var array Options for requests.
29+
* @var array Options for requests
3030
*/
3131
private $options;
3232

@@ -40,10 +40,11 @@ class SparkPost
4040
'key' => '',
4141
'version' => 'v1',
4242
'async' => true,
43+
'debug' => false,
4344
];
4445

4546
/**
46-
* @var Transmission Instance of Transmission class.
47+
* @var Transmission Instance of Transmission class
4748
*/
4849
public $transmissions;
4950

@@ -93,11 +94,13 @@ public function request($method = 'GET', $uri = '', $payload = [], $headers = []
9394
*/
9495
public function syncRequest($method = 'GET', $uri = '', $payload = [], $headers = [])
9596
{
96-
$request = $this->buildRequest($method, $uri, $payload, $headers);
97+
$requestValues = $this->buildRequestValues($method, $uri, $payload, $headers);
98+
$request = call_user_func_array(array($this, 'buildRequestInstance'), $requestValues);
99+
97100
try {
98-
return new SparkPostResponse($this->httpClient->sendRequest($request));
101+
return new SparkPostResponse($this->httpClient->sendRequest($request), $this->ifDebug($requestValues));
99102
} catch (\Exception $exception) {
100-
throw new SparkPostException($exception);
103+
throw new SparkPostException($exception, $this->ifDebug($requestValues));
101104
}
102105
}
103106

@@ -114,25 +117,26 @@ public function syncRequest($method = 'GET', $uri = '', $payload = [], $headers
114117
public function asyncRequest($method = 'GET', $uri = '', $payload = [], $headers = [])
115118
{
116119
if ($this->httpClient instanceof HttpAsyncClient) {
117-
$request = $this->buildRequest($method, $uri, $payload, $headers);
120+
$requestValues = $this->buildRequestValues($method, $uri, $payload, $headers);
121+
$request = call_user_func_array(array($this, 'buildRequestInstance'), $requestValues);
118122

119-
return new SparkPostPromise($this->httpClient->sendAsyncRequest($request));
123+
return new SparkPostPromise($this->httpClient->sendAsyncRequest($request), $this->ifDebug($requestValues));
120124
} else {
121125
throw new \Exception('Your http client does not support asynchronous requests. Please use a different client or use synchronous requests.');
122126
}
123127
}
124128

125129
/**
126-
* Builds request from given params.
130+
* Builds request values from given params.
127131
*
128132
* @param string $method
129133
* @param string $uri
130134
* @param array $payload
131135
* @param array $headers
132136
*
133-
* @return RequestInterface
137+
* @return array $requestValues
134138
*/
135-
public function buildRequest($method, $uri, $payload, $headers)
139+
public function buildRequestValues($method, $uri, $payload, $headers)
136140
{
137141
$method = trim(strtoupper($method));
138142

@@ -153,7 +157,37 @@ public function buildRequest($method, $uri, $payload, $headers)
153157
];
154158
$body = strtr(json_encode($body), $jsonReplace);
155159

156-
return $this->getMessageFactory()->createRequest($method, $url, $headers, $body);
160+
return [
161+
'method' => $method,
162+
'url' => $url,
163+
'headers' => $headers,
164+
'body' => $body,
165+
];
166+
}
167+
168+
/**
169+
* Build RequestInterface from given params.
170+
*
171+
* @param array $requestValues
172+
*
173+
* @return RequestInterface
174+
*/
175+
public function buildRequestInstance($method, $uri, $headers, $body)
176+
{
177+
return $this->getMessageFactory()->createRequest($method, $uri, $headers, $body);
178+
}
179+
180+
/**
181+
* Build RequestInterface from given params.
182+
*
183+
* @param array $requestValues
184+
*
185+
* @return RequestInterface
186+
*/
187+
public function buildRequest($method, $uri, $payload, $headers)
188+
{
189+
$requestValues = $this->buildRequestValues($method, $uri, $payload, $headers);
190+
return call_user_func_array(array($this, 'buildRequestInstance'), $requestValues);
157191
}
158192

159193
/**
@@ -245,6 +279,18 @@ public function setOptions($options)
245279
}
246280
}
247281

282+
/**
283+
* Returns the given value if debugging, an empty instance otherwise.
284+
*
285+
* @param any $param
286+
*
287+
* @return any $param
288+
*/
289+
private function ifDebug($param)
290+
{
291+
return $this->options['debug'] ? $param : null;
292+
}
293+
248294
/**
249295
* Sets up any endpoints to custom classes e.g. $this->transmissions.
250296
*/

lib/SparkPost/SparkPostException.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ class SparkPostException extends \Exception
1111
*/
1212
private $body = null;
1313

14+
/**
15+
* Array with the request values sent.
16+
*/
17+
private $request;
18+
1419
/**
1520
* Sets up the custom exception and copies over original exception values.
1621
*
1722
* @param Exception $exception - the exception to be wrapped
1823
*/
19-
public function __construct(\Exception $exception)
24+
public function __construct(\Exception $exception, $request = null)
2025
{
26+
$this->request = $request;
27+
2128
$message = $exception->getMessage();
2229
$code = $exception->getCode();
2330
if ($exception instanceof HttpException) {
@@ -29,6 +36,16 @@ public function __construct(\Exception $exception)
2936
parent::__construct($message, $code, $exception->getPrevious());
3037
}
3138

39+
/**
40+
* Returns the request values sent.
41+
*
42+
* @return array $request
43+
*/
44+
public function getRequest()
45+
{
46+
return $this->request;
47+
}
48+
3249
/**
3350
* Returns the body.
3451
*

lib/SparkPost/SparkPostPromise.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ class SparkPostPromise implements HttpPromise
1111
*/
1212
private $promise;
1313

14+
/**
15+
* Array with the request values sent.
16+
*/
17+
private $request;
18+
1419
/**
1520
* set the promise to be wrapped.
1621
*
1722
* @param HttpPromise $promise
1823
*/
19-
public function __construct(HttpPromise $promise)
24+
public function __construct(HttpPromise $promise, $request = null)
2025
{
2126
$this->promise = $promise;
27+
$this->request = $request;
2228
}
2329

2430
/**
@@ -29,13 +35,15 @@ public function __construct(HttpPromise $promise)
2935
*/
3036
public function then(callable $onFulfilled = null, callable $onRejected = null)
3137
{
32-
return $this->promise->then(function ($response) use ($onFulfilled) {
38+
$request = $this->request;
39+
40+
return $this->promise->then(function ($response) use ($onFulfilled, $request) {
3341
if (isset($onFulfilled)) {
34-
$onFulfilled(new SparkPostResponse($response));
42+
$onFulfilled(new SparkPostResponse($response, $request));
3543
}
36-
}, function ($exception) use ($onRejected) {
44+
}, function ($exception) use ($onRejected, $request) {
3745
if (isset($onRejected)) {
38-
$onRejected(new SparkPostException($exception));
46+
$onRejected(new SparkPostException($exception, $request));
3947
}
4048
});
4149
}
@@ -64,9 +72,9 @@ public function wait($unwrap = true)
6472
try {
6573
$response = $this->promise->wait($unwrap);
6674

67-
return $response ? new SparkPostResponse($response) : $response;
75+
return $response ? new SparkPostResponse($response, $this->request) : $response;
6876
} catch (\Exception $exception) {
69-
throw new SparkPostException($exception);
77+
throw new SparkPostException($exception, $this->request);
7078
}
7179
}
7280
}

0 commit comments

Comments
 (0)