|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Postal; |
4 | 6 |
|
| 7 | +use GuzzleHttp\Client as HttpClient; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | + |
5 | 10 | class Client |
6 | 11 | { |
7 | | - public function __construct($host, $serverKey) |
| 12 | + public MessagesService $messages; |
| 13 | + |
| 14 | + public SendService $send; |
| 15 | + |
| 16 | + protected HttpClient $httpClient; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + string $host, |
| 20 | + string $apiKey, |
| 21 | + HttpClient $httpClient = null |
| 22 | + ) { |
| 23 | + $this->httpClient = $httpClient ?: HttpClientFactory::create($host, $apiKey); |
| 24 | + $this->messages = new MessagesService($this); |
| 25 | + $this->send = new SendService($this); |
| 26 | + } |
| 27 | + |
| 28 | + public function getHttpClient(): HttpClient |
| 29 | + { |
| 30 | + return $this->httpClient; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @template T |
| 35 | + * @param class-string<T> $class |
| 36 | + * @return T |
| 37 | + */ |
| 38 | + public function prepareResponse(ResponseInterface $response, $class) |
| 39 | + { |
| 40 | + return new $class($this->validateResponse($response)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @template T |
| 45 | + * @param class-string<T> $class |
| 46 | + * @return array<T> |
| 47 | + */ |
| 48 | + public function prepareListResponse(ResponseInterface $response, $class) |
| 49 | + { |
| 50 | + $list = $this->validateResponse($response); |
| 51 | + |
| 52 | + // if (! array_is_list($list)) { |
| 53 | + if (! $this->arrayIsList($list)) { |
| 54 | + throw new ApiException('Unexpected response received, expected a list'); |
| 55 | + } |
| 56 | + |
| 57 | + return array_map(fn ($item) => new $class($item), $list); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array<string|int, mixed> |
| 62 | + */ |
| 63 | + protected function validateResponse(ResponseInterface $response): array |
8 | 64 | { |
9 | | - $this->host = $host; |
10 | | - $this->serverKey = $serverKey; |
| 65 | + $json = json_decode((string) $response->getBody(), true); |
| 66 | + |
| 67 | + if (json_last_error() !== JSON_ERROR_NONE || ! is_array($json)) { |
| 68 | + throw new ApiException('Malformed response body received'); |
| 69 | + } |
| 70 | + |
| 71 | + if (! isset($json['status']) || $json['status'] !== 'success') { |
| 72 | + $message = $json['data']['message'] ?? 'An unexpected error was received'; |
| 73 | + $code = 0; |
| 74 | + if (isset($json['data']['code'])) { |
| 75 | + $message = $json['data']['code'] . ': ' . $message; |
| 76 | + $code = $json['data']['code']; |
| 77 | + } |
| 78 | + |
| 79 | + throw new ApiException($message, $code); |
| 80 | + } |
| 81 | + |
| 82 | + if (! isset($json['data'])) { |
| 83 | + throw new ApiException('Unexpected response received'); |
| 84 | + } |
| 85 | + |
| 86 | + return $json['data']; |
11 | 87 | } |
12 | 88 |
|
13 | | - public function makeRequest($controller, $action, $parameters) |
| 89 | + /** |
| 90 | + * @param array<mixed> $array |
| 91 | + */ |
| 92 | + private function arrayIsList(array $array): bool |
14 | 93 | { |
15 | | - $url = sprintf('%s/api/v1/%s/%s', $this->host, $controller, $action); |
16 | | - |
17 | | - // Headers |
18 | | - $headers = [ |
19 | | - 'x-server-api-key' => $this->serverKey, |
20 | | - 'content-type' => 'application/json', |
21 | | - ]; |
22 | | - |
23 | | - // Make the body |
24 | | - $json = json_encode($parameters); |
25 | | - |
26 | | - // Make the request |
27 | | - $response = \Requests::post($url, $headers, $json); |
28 | | - |
29 | | - if ($response->status_code === 200) { |
30 | | - $json = json_decode($response->body); |
31 | | - |
32 | | - if ($json->status == 'success') { |
33 | | - return $json->data; |
34 | | - } else { |
35 | | - if (isset($json->data->code)) { |
36 | | - throw new Error(sprintf('[%s] %s', $json->data->code, $json->data->message)); |
37 | | - } else { |
38 | | - throw new Error($json->data->message); |
39 | | - } |
| 94 | + $i = 0; |
| 95 | + foreach ($array as $k => $v) { |
| 96 | + if ($k !== $i++) { |
| 97 | + return false; |
40 | 98 | } |
41 | 99 | } |
42 | 100 |
|
43 | | - throw new Error('Couldn’t send message to API'); |
| 101 | + return true; |
44 | 102 | } |
45 | 103 | } |
0 commit comments