RestClient.php
380 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2013 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\Connection; |
| 11 | |
| 12 | use AmeliaHttp\Client\HttpClient; |
| 13 | use AmeliaHttp\Discovery\HttpClientDiscovery; |
| 14 | use AmeliaHttp\Discovery\MessageFactoryDiscovery; |
| 15 | use AmeliaHttp\Message\MultipartStream\MultipartStreamBuilder; |
| 16 | use Mailgun\Connection\Exceptions\GenericHTTPError; |
| 17 | use Mailgun\Connection\Exceptions\InvalidCredentials; |
| 18 | use Mailgun\Connection\Exceptions\MissingEndpoint; |
| 19 | use Mailgun\Connection\Exceptions\MissingRequiredParameters; |
| 20 | use Mailgun\Constants\Api; |
| 21 | use Mailgun\Constants\ExceptionMessages; |
| 22 | use AmeliaPsr\Http\Message\ResponseInterface; |
| 23 | |
| 24 | /** |
| 25 | * This class is a wrapper for the HTTP client. |
| 26 | * |
| 27 | * @deprecated Will be removed in 3.0 |
| 28 | */ |
| 29 | class RestClient |
| 30 | { |
| 31 | /** |
| 32 | * Your API key. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $apiKey; |
| 37 | |
| 38 | /** |
| 39 | * @var HttpClient |
| 40 | */ |
| 41 | protected $httpClient; |
| 42 | |
| 43 | /** |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $apiHost; |
| 47 | |
| 48 | /** |
| 49 | * The version of the API to use. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $apiVersion = 'v2'; |
| 54 | |
| 55 | /** |
| 56 | * If we should use SSL or not. |
| 57 | * |
| 58 | * @var bool |
| 59 | * |
| 60 | * @deprecated To be removed in 3.0 |
| 61 | */ |
| 62 | protected $sslEnabled = true; |
| 63 | |
| 64 | /** |
| 65 | * @param string $apiKey |
| 66 | * @param string $apiHost |
| 67 | * @param HttpClient $httpClient |
| 68 | */ |
| 69 | public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) |
| 70 | { |
| 71 | $this->apiKey = $apiKey; |
| 72 | $this->apiHost = $apiHost; |
| 73 | $this->httpClient = $httpClient; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $method |
| 78 | * @param string $uri |
| 79 | * @param mixed $body |
| 80 | * @param array $files |
| 81 | * @param array $headers |
| 82 | * |
| 83 | * @throws GenericHTTPError |
| 84 | * @throws InvalidCredentials |
| 85 | * @throws MissingEndpoint |
| 86 | * @throws MissingRequiredParameters |
| 87 | * |
| 88 | * @return \stdClass |
| 89 | */ |
| 90 | protected function send($method, $uri, $body = null, $files = [], array $headers = []) |
| 91 | { |
| 92 | $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; |
| 93 | $headers['Authorization'] = 'Basic '.base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey)); |
| 94 | |
| 95 | if (!empty($files)) { |
| 96 | $builder = new MultipartStreamBuilder(); |
| 97 | foreach ($files as $file) { |
| 98 | $builder->addResource($file['name'], $file['contents'], $file); |
| 99 | } |
| 100 | $body = $builder->build(); |
| 101 | $headers['Content-Type'] = 'multipart/form-data; boundary="'.$builder->getBoundary().'"'; |
| 102 | } elseif (is_array($body)) { |
| 103 | $body = http_build_query($body); |
| 104 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 105 | } |
| 106 | |
| 107 | $request = MessageFactoryDiscovery::find()->createRequest($method, $this->getApiUrl($uri), $headers, $body); |
| 108 | $response = $this->getHttpClient()->sendRequest($request); |
| 109 | |
| 110 | return $this->responseHandler($response); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param string $url |
| 115 | * |
| 116 | * @throws GenericHTTPError |
| 117 | * @throws InvalidCredentials |
| 118 | * @throws MissingEndpoint |
| 119 | * @throws MissingRequiredParameters |
| 120 | * |
| 121 | * @return \stdClass |
| 122 | */ |
| 123 | public function getAttachment($url) |
| 124 | { |
| 125 | $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; |
| 126 | $headers['Authorization'] = 'Basic '.base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey)); |
| 127 | $request = MessageFactoryDiscovery::find()->createRequest('get', $url, $headers); |
| 128 | $response = HttpClientDiscovery::find()->sendRequest($request); |
| 129 | |
| 130 | return $this->responseHandler($response); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param string $endpointUrl |
| 135 | * @param array $postData |
| 136 | * @param array $files |
| 137 | * |
| 138 | * @throws GenericHTTPError |
| 139 | * @throws InvalidCredentials |
| 140 | * @throws MissingEndpoint |
| 141 | * @throws MissingRequiredParameters |
| 142 | * |
| 143 | * @return \stdClass |
| 144 | */ |
| 145 | public function post($endpointUrl, array $postData = [], $files = []) |
| 146 | { |
| 147 | $postFiles = []; |
| 148 | |
| 149 | $fields = ['message', 'attachment', 'inline']; |
| 150 | foreach ($fields as $fieldName) { |
| 151 | if (isset($files[$fieldName])) { |
| 152 | if (is_array($files[$fieldName])) { |
| 153 | foreach ($files[$fieldName] as $file) { |
| 154 | $postFiles[] = $this->prepareFile($fieldName, $file); |
| 155 | } |
| 156 | } else { |
| 157 | $postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $postDataMultipart = []; |
| 163 | foreach ($postData as $key => $value) { |
| 164 | if (is_array($value)) { |
| 165 | foreach ($value as $subValue) { |
| 166 | $postDataMultipart[] = [ |
| 167 | 'name' => $key, |
| 168 | 'contents' => $subValue, |
| 169 | ]; |
| 170 | } |
| 171 | } else { |
| 172 | $postDataMultipart[] = [ |
| 173 | 'name' => $key, |
| 174 | 'contents' => $value, |
| 175 | ]; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return $this->send('POST', $endpointUrl, [], array_merge($postDataMultipart, $postFiles)); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @param string $endpointUrl |
| 184 | * @param array $queryString |
| 185 | * |
| 186 | * @throws GenericHTTPError |
| 187 | * @throws InvalidCredentials |
| 188 | * @throws MissingEndpoint |
| 189 | * @throws MissingRequiredParameters |
| 190 | * |
| 191 | * @return \stdClass |
| 192 | */ |
| 193 | public function get($endpointUrl, $queryString = []) |
| 194 | { |
| 195 | return $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @param string $endpointUrl |
| 200 | * |
| 201 | * @throws GenericHTTPError |
| 202 | * @throws InvalidCredentials |
| 203 | * @throws MissingEndpoint |
| 204 | * @throws MissingRequiredParameters |
| 205 | * |
| 206 | * @return \stdClass |
| 207 | */ |
| 208 | public function delete($endpointUrl) |
| 209 | { |
| 210 | return $this->send('DELETE', $endpointUrl); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @param string $endpointUrl |
| 215 | * @param mixed $putData |
| 216 | * |
| 217 | * @throws GenericHTTPError |
| 218 | * @throws InvalidCredentials |
| 219 | * @throws MissingEndpoint |
| 220 | * @throws MissingRequiredParameters |
| 221 | * |
| 222 | * @return \stdClass |
| 223 | */ |
| 224 | public function put($endpointUrl, $putData) |
| 225 | { |
| 226 | return $this->send('PUT', $endpointUrl, $putData); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @param ResponseInterface $responseObj |
| 231 | * |
| 232 | * @throws GenericHTTPError |
| 233 | * @throws InvalidCredentials |
| 234 | * @throws MissingEndpoint |
| 235 | * @throws MissingRequiredParameters |
| 236 | * |
| 237 | * @return \stdClass |
| 238 | */ |
| 239 | public function responseHandler(ResponseInterface $responseObj) |
| 240 | { |
| 241 | $httpResponseCode = (int) $responseObj->getStatusCode(); |
| 242 | |
| 243 | switch ($httpResponseCode) { |
| 244 | case 200: |
| 245 | $data = (string) $responseObj->getBody(); |
| 246 | $jsonResponseData = json_decode($data, false); |
| 247 | $result = new \stdClass(); |
| 248 | // return response data as json if possible, raw if not |
| 249 | $result->http_response_body = $data && null === $jsonResponseData ? $data : $jsonResponseData; |
| 250 | $result->http_response_code = $httpResponseCode; |
| 251 | |
| 252 | return $result; |
| 253 | case 400: |
| 254 | throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS.$this->getResponseExceptionMessage($responseObj)); |
| 255 | case 401: |
| 256 | throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); |
| 257 | case 404: |
| 258 | throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT.$this->getResponseExceptionMessage($responseObj)); |
| 259 | default: |
| 260 | throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param ResponseInterface $responseObj |
| 266 | * |
| 267 | * @return string |
| 268 | */ |
| 269 | protected function getResponseExceptionMessage(ResponseInterface $responseObj) |
| 270 | { |
| 271 | $body = (string) $responseObj->getBody(); |
| 272 | $response = json_decode($body); |
| 273 | if (JSON_ERROR_NONE == json_last_error() && isset($response->message)) { |
| 274 | return ' '.$response->message; |
| 275 | } |
| 276 | |
| 277 | return ''; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Prepare a file for the postBody. |
| 282 | * |
| 283 | * @param string $fieldName |
| 284 | * @param string|array $filePath |
| 285 | * |
| 286 | * @return array |
| 287 | */ |
| 288 | protected function prepareFile($fieldName, $filePath) |
| 289 | { |
| 290 | $filename = null; |
| 291 | |
| 292 | if (is_array($filePath) && isset($filePath['fileContent'])) { |
| 293 | // File from memory |
| 294 | $filename = $filePath['filename']; |
| 295 | $resource = fopen('php://temp', 'r+'); |
| 296 | fwrite($resource, $filePath['fileContent']); |
| 297 | rewind($resource); |
| 298 | } else { |
| 299 | // Backward compatibility code |
| 300 | if (is_array($filePath) && isset($filePath['filePath'])) { |
| 301 | $filename = $filePath['remoteName']; |
| 302 | $filePath = $filePath['filePath']; |
| 303 | } |
| 304 | |
| 305 | // Remove leading @ symbol |
| 306 | if (0 === strpos($filePath, '@')) { |
| 307 | $filePath = substr($filePath, 1); |
| 308 | } |
| 309 | |
| 310 | $resource = fopen($filePath, 'r'); |
| 311 | } |
| 312 | |
| 313 | return [ |
| 314 | 'name' => $fieldName, |
| 315 | 'contents' => $resource, |
| 316 | 'filename' => $filename, |
| 317 | ]; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @return HttpClient |
| 322 | */ |
| 323 | protected function getHttpClient() |
| 324 | { |
| 325 | if (null === $this->httpClient) { |
| 326 | $this->httpClient = HttpClientDiscovery::find(); |
| 327 | } |
| 328 | |
| 329 | return $this->httpClient; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @param string $uri |
| 334 | * |
| 335 | * @return string |
| 336 | */ |
| 337 | private function getApiUrl($uri) |
| 338 | { |
| 339 | return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @param string $apiEndpoint |
| 344 | * @param string $apiVersion |
| 345 | * @param bool $ssl |
| 346 | * |
| 347 | * @return string |
| 348 | */ |
| 349 | private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) |
| 350 | { |
| 351 | return ($ssl ? 'https://' : 'http://').$apiEndpoint.'/'.$apiVersion.'/'; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @param string $apiVersion |
| 356 | * |
| 357 | * @return RestClient |
| 358 | */ |
| 359 | public function setApiVersion($apiVersion) |
| 360 | { |
| 361 | $this->apiVersion = $apiVersion; |
| 362 | |
| 363 | return $this; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @param bool $sslEnabled |
| 368 | * |
| 369 | * @return RestClient |
| 370 | * |
| 371 | * @deprecated To be removed in 3.0 |
| 372 | */ |
| 373 | public function setSslEnabled($sslEnabled) |
| 374 | { |
| 375 | $this->sslEnabled = $sslEnabled; |
| 376 | |
| 377 | return $this; |
| 378 | } |
| 379 | } |
| 380 |