1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
5
|
|
|
* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Mailgun\Api; |
11
|
|
|
|
12
|
|
|
use Http\Client\Exception as HttplugException; |
13
|
|
|
use Http\Client\HttpClient; |
14
|
|
|
use Mailgun\Deserializer\ResponseDeserializer; |
15
|
|
|
use Mailgun\Exception\HttpServerException; |
16
|
|
|
use Mailgun\RequestBuilder; |
17
|
|
|
use Mailgun\Resource\Api\ErrorResponse; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class HttpApi |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The HTTP client. |
27
|
|
|
* |
28
|
|
|
* @var HttpClient |
29
|
|
|
*/ |
30
|
|
|
private $httpClient; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ResponseDeserializer |
34
|
|
|
*/ |
35
|
|
|
protected $serializer; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RequestBuilder |
39
|
|
|
*/ |
40
|
|
|
protected $requestBuilder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param HttpClient $httpClient |
44
|
|
|
* @param RequestBuilder $requestBuilder |
45
|
|
|
* @param ResponseDeserializer $deserializer |
46
|
|
|
*/ |
47
|
4 |
|
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, ResponseDeserializer $deserializer) |
48
|
|
|
{ |
49
|
4 |
|
$this->httpClient = $httpClient; |
50
|
4 |
|
$this->requestBuilder = $requestBuilder; |
51
|
4 |
|
$this->deserializer = $deserializer; |
|
|
|
|
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Attempts to safely deserialize the response into the given class. |
56
|
|
|
* If the HTTP return code != 200, deserializes into SimpleResponse::class |
57
|
|
|
* to contain the error message and any other information provided. |
58
|
|
|
* |
59
|
|
|
* @param ResponseInterface $response |
60
|
|
|
* @param string $className |
61
|
|
|
* |
62
|
|
|
* @return $class|SimpleResponse |
|
|
|
|
63
|
|
|
*/ |
64
|
2 |
|
protected function safeDeserialize(ResponseInterface $response, $className) |
65
|
|
|
{ |
66
|
2 |
|
if ($response->getStatusCode() !== 200) { |
67
|
|
|
return $this->deserializer->deserialize($response, ErrorResponse::class); |
|
|
|
|
68
|
|
|
} else { |
69
|
2 |
|
return $this->deserializer->deserialize($response, $className); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Send a GET request with query parameters. |
75
|
|
|
* |
76
|
|
|
* @param string $path Request path. |
77
|
|
|
* @param array $parameters GET parameters. |
78
|
|
|
* @param array $requestHeaders Request Headers. |
79
|
|
|
* |
80
|
|
|
* @return ResponseInterface |
81
|
|
|
*/ |
82
|
|
|
protected function httpGet($path, array $parameters = [], array $requestHeaders = []) |
83
|
|
|
{ |
84
|
|
|
if (count($parameters) > 0) { |
85
|
|
|
$path .= '?'.http_build_query($parameters); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
$response = $this->httpClient->sendRequest( |
90
|
|
|
$this->requestBuilder->create('GET', $path, $requestHeaders) |
91
|
|
|
); |
92
|
|
|
} catch (HttplugException\NetworkException $e) { |
93
|
|
|
throw HttpServerException::networkError($e); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Send a POST request with JSON-encoded parameters. |
101
|
|
|
* |
102
|
|
|
* @param string $path Request path. |
103
|
|
|
* @param array $parameters POST parameters to be JSON encoded. |
104
|
|
|
* @param array $requestHeaders Request headers. |
105
|
|
|
* |
106
|
|
|
* @return ResponseInterface |
107
|
|
|
*/ |
108
|
|
|
protected function httpPost($path, array $parameters = [], array $requestHeaders = []) |
109
|
|
|
{ |
110
|
|
|
return $this->httpPostRaw($path, $this->createJsonBody($parameters), $requestHeaders); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Send a POST request with raw data. |
115
|
|
|
* |
116
|
|
|
* @param string $path Request path. |
117
|
|
|
* @param array|string $body Request body. |
118
|
|
|
* @param array $requestHeaders Request headers. |
119
|
|
|
* |
120
|
|
|
* @return ResponseInterface |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
protected function httpPostRaw($path, $body, array $requestHeaders = []) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
try { |
125
|
|
|
$response = $this->httpClient->sendRequest( |
126
|
|
|
$this->requestBuilder->create('POST', $path, $requestHeaders, $body) |
127
|
|
|
); |
128
|
|
|
} catch (HttplugException\NetworkException $e) { |
129
|
|
|
throw HttpServerException::networkError($e); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $response; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Send a PUT request with JSON-encoded parameters. |
137
|
|
|
* |
138
|
|
|
* @param string $path Request path. |
139
|
|
|
* @param array $parameters POST parameters to be JSON encoded. |
140
|
|
|
* @param array $requestHeaders Request headers. |
141
|
|
|
* |
142
|
|
|
* @return ResponseInterface |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
protected function httpPut($path, array $parameters = [], array $requestHeaders = []) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
$response = $this->httpClient->sendRequest( |
148
|
|
|
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createJsonBody($parameters)) |
149
|
|
|
); |
150
|
|
|
} catch (HttplugException\NetworkException $e) { |
151
|
|
|
throw HttpServerException::networkError($e); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $response; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
159
|
|
|
* |
160
|
|
|
* @param string $path Request path. |
161
|
|
|
* @param array $parameters POST parameters to be JSON encoded. |
162
|
|
|
* @param array $requestHeaders Request headers. |
163
|
|
|
* |
164
|
|
|
* @return ResponseInterface |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
try { |
169
|
|
|
$response = $this->httpClient->sendRequest( |
170
|
|
|
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createJsonBody($parameters)) |
171
|
|
|
); |
172
|
|
|
} catch (HttplugException\NetworkException $e) { |
173
|
|
|
throw HttpServerException::networkError($e); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $response; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create a JSON encoded version of an array of parameters. |
181
|
|
|
* |
182
|
|
|
* @param array $parameters Request parameters |
183
|
|
|
* |
184
|
|
|
* @return null|string |
185
|
|
|
*/ |
186
|
|
|
protected function createJsonBody(array $parameters) |
187
|
|
|
{ |
188
|
|
|
return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.