1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vindi; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use Vindi\Exceptions\ValidationException; |
8
|
|
|
use Vindi\Http\Client; |
9
|
|
|
use Vindi\Exceptions\RequestException; |
10
|
|
|
use Vindi\Exceptions\RateLimitException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ApiRequester |
14
|
|
|
* |
15
|
|
|
* @package Vindi |
16
|
|
|
*/ |
17
|
|
|
class ApiRequester |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Vindi\Http\Client |
21
|
|
|
*/ |
22
|
|
|
public $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResponseInterface |
26
|
|
|
*/ |
27
|
|
|
public $lastResponse; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
public $lastOptions; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ApiRequester constructor. |
36
|
|
|
*/ |
37
|
458 |
|
public function __construct() |
38
|
|
|
{ |
39
|
458 |
|
$this->client = new Client; |
40
|
458 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $method HTTP Method. |
44
|
|
|
* @param string $endpoint Relative to API base path. |
45
|
|
|
* @param array $options Options for the request. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
49
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
50
|
|
|
* @throws \Vindi\Exceptions\RequestException |
51
|
|
|
*/ |
52
|
10 |
|
public function request($method, $endpoint, array $options = []) |
53
|
|
|
{ |
54
|
10 |
|
$this->lastOptions = $options; |
55
|
|
|
try { |
56
|
10 |
|
$response = $this->client->request($method, $endpoint, $options); |
57
|
10 |
|
} catch (ClientException $e) { |
58
|
2 |
|
$response = $e->getResponse(); |
59
|
|
|
} |
60
|
|
|
|
61
|
10 |
|
return $this->response($response); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ResponseInterface $response |
66
|
|
|
* |
67
|
|
|
* @return object |
68
|
|
|
* @throws RateLimitException |
69
|
|
|
* @throws \Vindi\Exceptions\RequestException |
70
|
|
|
*/ |
71
|
10 |
|
public function response(ResponseInterface $response) |
72
|
|
|
{ |
73
|
10 |
|
$this->lastResponse = $response; |
74
|
|
|
|
75
|
10 |
|
$content = $response->getBody()->getContents(); |
76
|
|
|
|
77
|
10 |
|
$decoded = json_decode($content); // parse as object |
78
|
10 |
|
$data = $decoded; |
79
|
10 |
|
|
80
|
|
|
if (!empty($decoded)) { |
81
|
10 |
|
reset($decoded); |
82
|
8 |
|
$data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors. |
83
|
|
|
} |
84
|
2 |
|
|
85
|
|
|
$this->checkRateLimit($response) |
86
|
|
|
->checkForErrors($response, $data); |
87
|
|
|
|
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ResponseInterface $response |
93
|
10 |
|
* |
94
|
|
|
* @return $this |
95
|
10 |
|
* @throws RateLimitException |
96
|
2 |
|
*/ |
97
|
|
|
private function checkRateLimit(ResponseInterface $response) |
98
|
|
|
{ |
99
|
8 |
|
if (429 === $response->getStatusCode()) { |
100
|
|
|
throw new RateLimitException($response); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ResponseInterface $response |
108
|
|
|
* @param mixed $data |
109
|
8 |
|
* |
110
|
|
|
* @return $this |
111
|
8 |
|
* @throws \Vindi\Exceptions\RequestException |
112
|
|
|
*/ |
113
|
8 |
|
private function checkForErrors(ResponseInterface $response, $data) |
114
|
|
|
{ |
115
|
8 |
|
$status = $response->getStatusCode(); |
116
|
|
|
|
117
|
8 |
|
$data = (array)$data; |
118
|
|
|
|
119
|
6 |
|
$statusClass = (int)($status / 100); |
120
|
2 |
|
|
121
|
4 |
|
if (($statusClass === 4) || ($statusClass === 5)) { |
122
|
4 |
|
switch ($status) { |
123
|
4 |
|
case 422: |
124
|
|
|
throw new ValidationException($status, $data, $this->lastOptions); |
125
|
|
|
default: |
126
|
2 |
|
throw new RequestException($status, $data, $this->lastOptions); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: