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
|
|
|
* @param bool $debug If we need a debug mode |
39
|
|
|
* |
40
|
|
|
* @return \Psr\Http\Message\ResponseInterface|null |
41
|
|
|
* @throws \ErrorException |
42
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
43
|
|
|
*/ |
44
|
|
|
private function repeatRequest(string $type, string $url, $params, bool $debug = false): ?ResponseInterface |
45
|
|
|
{ |
46
|
|
|
$type = strtoupper($type); |
47
|
|
|
|
48
|
|
|
for ($i = 1; $i < $this->config->get('tries'); $i++) { |
49
|
|
|
|
50
|
|
|
if ($params === null) { |
51
|
|
|
// Execute the request to server |
52
|
|
|
$result = $this->client->request($type, $this->config->get('base_uri') . $url); |
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
// Execute the request to server |
55
|
|
|
$result = $this->client->request($type, $this->config->get('base_uri') . $url, [RequestOptions::FORM_PARAMS => $params->toArray()]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Return request for debug mode |
59
|
|
|
if ($debug) { |
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Check the code status |
64
|
|
|
$code = $result->getStatusCode(); |
65
|
|
|
$reason = $result->getReasonPhrase(); |
66
|
|
|
|
67
|
|
|
// If success response from server |
68
|
|
|
if ($code === 200 || $code === 201) { |
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// If not "too many requests", then probably some bug on remote or our side |
73
|
|
|
if ($code !== 429) { |
74
|
|
|
throw new ErrorException($code . ' ' . $reason); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Waiting in seconds |
78
|
|
|
sleep($this->config->get('seconds')); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Return false if loop is done but no answer from server |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Execute request and return response |
87
|
|
|
* |
88
|
|
|
* @param bool $debug |
89
|
|
|
* |
90
|
|
|
* @return null|object Array with data or NULL if error |
91
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
92
|
|
|
* @throws \ErrorException |
93
|
|
|
* @throws \Resova\Exceptions\EmptyResults |
94
|
|
|
*/ |
95
|
|
|
public function exec(bool $debug = false) |
96
|
|
|
{ |
97
|
|
|
return $this->doRequest($this->type, $this->endpoint, $this->params, false, $debug); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Execute query and return RAW response from remote API |
102
|
|
|
* |
103
|
|
|
* @param bool $debug |
104
|
|
|
* |
105
|
|
|
* @return null|\Psr\Http\Message\ResponseInterface RAW response or NULL if error |
106
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
107
|
|
|
* @throws \ErrorException |
108
|
|
|
* @throws \Resova\Exceptions\EmptyResults |
109
|
|
|
*/ |
110
|
|
|
public function raw(bool $debug = false): ?ResponseInterface |
111
|
|
|
{ |
112
|
|
|
return $this->doRequest($this->type, $this->endpoint, $this->params, true, $debug); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Make the request and analyze the result |
117
|
|
|
* |
118
|
|
|
* @param string $type Request method |
119
|
|
|
* @param string $endpoint Api request endpoint |
120
|
|
|
* @param mixed $params List of parameters |
121
|
|
|
* @param bool $raw Return data in raw format |
122
|
|
|
* @param bool $debug |
123
|
|
|
* |
124
|
|
|
* @return null|object|ResponseInterface Array with data, RAW response or NULL if error |
125
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
126
|
|
|
* @throws \ErrorException |
127
|
|
|
* @throws \Resova\Exceptions\EmptyResults |
128
|
|
|
*/ |
129
|
|
|
private function doRequest($type, $endpoint, $params = null, bool $raw = false, bool $debug = false) |
130
|
|
|
{ |
131
|
|
|
// Null by default |
132
|
|
|
$response = null; |
|
|
|
|
133
|
|
|
|
134
|
|
|
// Execute the request to server |
135
|
|
|
$result = $this->repeatRequest($type, $endpoint, $params, $debug); |
136
|
|
|
|
137
|
|
|
// If debug then return Guzzle object |
138
|
|
|
if ($debug) { |
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (null === $result) { |
143
|
|
|
throw new EmptyResults('Empty results returned from Resova API'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Return RAW result if required |
147
|
|
|
return $raw ? $result : json_decode($result->getBody(), false); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.