1 | <?php |
||
2 | |||
3 | namespace Resova; |
||
4 | |||
5 | use ErrorException; |
||
6 | use GuzzleHttp\RequestOptions; |
||
7 | use Psr\Http\Message\ResponseInterface; |
||
8 | use Resova\Exceptions\EmptyResults; |
||
9 | |||
10 | /** |
||
11 | * @author Paul Rock <[email protected]> |
||
12 | * @link https://drteam.rocks |
||
13 | * @license MIT |
||
14 | * @package Resova |
||
15 | */ |
||
16 | trait HttpTrait |
||
17 | { |
||
18 | /** |
||
19 | * Initial state of some variables |
||
20 | * |
||
21 | * @var null|\GuzzleHttp\Client |
||
22 | */ |
||
23 | public $client; |
||
24 | |||
25 | /** |
||
26 | * Object of main config |
||
27 | * |
||
28 | * @var \Resova\Config |
||
29 | */ |
||
30 | public $config; |
||
31 | |||
32 | /** |
||
33 | * Request executor with timeout and repeat tries |
||
34 | * |
||
35 | * @param string $type Request method |
||
36 | * @param string $url endpoint url |
||
37 | * @param mixed $params List of parameters |
||
38 | * |
||
39 | * @return \Psr\Http\Message\ResponseInterface|null |
||
40 | * @throws \ErrorException |
||
41 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
42 | */ |
||
43 | 1 | private function repeatRequest(string $type, string $url, $params): ?ResponseInterface |
|
44 | { |
||
45 | 1 | $type = strtoupper($type); |
|
46 | |||
47 | 1 | for ($i = 1; $i < $this->config->get('tries'); $i++) { |
|
48 | |||
49 | 1 | if ($this->config->get('verbose')) { |
|
50 | error_log("[$type] " . $this->config->get('base_uri') . $url); |
||
51 | } |
||
52 | |||
53 | 1 | if ($params === null) { |
|
54 | // Execute the request to server |
||
55 | 1 | $result = $this->client->request($type, $this->config->get('base_uri') . $url); |
|
56 | } else { |
||
57 | // Execute the request to server |
||
58 | $result = $this->client->request($type, $this->config->get('base_uri') . $url, [RequestOptions::FORM_PARAMS => $params->toArray()]); |
||
59 | } |
||
60 | |||
61 | // Check the code status |
||
62 | 1 | $code = $result->getStatusCode(); |
|
63 | 1 | $reason = $result->getReasonPhrase(); |
|
64 | |||
65 | // If success response from server |
||
66 | 1 | if ($code === 200 || $code === 201) { |
|
67 | 1 | return $result; |
|
68 | } |
||
69 | |||
70 | // If not "too many requests", then probably some bug on remote or our side |
||
71 | if ($code !== 429) { |
||
72 | throw new ErrorException($code . ' ' . $reason); |
||
73 | } |
||
74 | |||
75 | // Waiting in seconds |
||
76 | sleep($this->config->get('seconds')); |
||
77 | } |
||
78 | |||
79 | // Return false if loop is done but no answer from server |
||
80 | return null; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Execute request and return response |
||
85 | * |
||
86 | * @return null|object Array with data or NULL if error |
||
87 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
88 | * @throws \ErrorException |
||
89 | * @throws \Resova\Exceptions\EmptyResults |
||
90 | */ |
||
91 | 1 | public function exec() |
|
92 | { |
||
93 | 1 | return $this->doRequest($this->type, $this->endpoint, $this->params, false); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Execute query and return RAW response from remote API |
||
98 | * |
||
99 | * @return null|\Psr\Http\Message\ResponseInterface RAW response or NULL if error |
||
100 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
101 | * @throws \ErrorException |
||
102 | * @throws \Resova\Exceptions\EmptyResults |
||
103 | */ |
||
104 | public function raw(): ?ResponseInterface |
||
105 | { |
||
106 | return $this->doRequest($this->type, $this->endpoint, $this->params, true); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Make the request and analyze the result |
||
111 | * |
||
112 | * @param string $type Request method |
||
113 | * @param string $endpoint Api request endpoint |
||
114 | * @param mixed $params List of parameters |
||
115 | * @param bool $raw Return data in raw format |
||
116 | * |
||
117 | * @return null|object|ResponseInterface Array with data, RAW response or NULL if error |
||
118 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
119 | * @throws \ErrorException |
||
120 | * @throws \Resova\Exceptions\EmptyResults |
||
121 | */ |
||
122 | 1 | private function doRequest($type, $endpoint, $params = null, bool $raw = false) |
|
123 | { |
||
124 | // Null by default |
||
125 | 1 | $response = null; |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
126 | |||
127 | // Execute the request to server |
||
128 | 1 | $result = $this->repeatRequest($type, $endpoint, $params); |
|
129 | |||
130 | // If debug then return Guzzle object |
||
131 | 1 | if ($this->config->get('debug') === true) { |
|
132 | 1 | return $result; |
|
133 | } |
||
134 | |||
135 | if (null === $result) { |
||
136 | throw new EmptyResults('Empty results returned from Resova API'); |
||
137 | } |
||
138 | |||
139 | // Return RAW result if required |
||
140 | return $raw ? $result : json_decode($result->getBody(), false); |
||
141 | } |
||
142 | |||
143 | } |
||
144 |