|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ninjacto\OrtcPhp; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
6
|
|
|
use GuzzleHttp\Message\FutureResponse; |
|
7
|
|
|
use GuzzleHttp\Pool; |
|
8
|
|
|
use ninjacto\OrtcPhp\Exceptions\BatchRequestException; |
|
9
|
|
|
use ninjacto\OrtcPhp\Exceptions\InvalidBalancerUrlException; |
|
10
|
|
|
use ninjacto\OrtcPhp\Exceptions\NetworkErrorException; |
|
11
|
|
|
use ninjacto\OrtcPhp\Exceptions\UnauthorizedException; |
|
12
|
|
|
use ninjacto\OrtcPhp\Models\Requests\OrtcRequest; |
|
13
|
|
|
|
|
14
|
|
|
class OrtcClient |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \GuzzleHttp\Client |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $guzzleClient; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var OrtcRequest |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $baseUrl; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* execute single request. |
|
33
|
|
|
* |
|
34
|
|
|
* @throws UnauthorizedException |
|
35
|
|
|
* @throws NetworkErrorException |
|
36
|
|
|
* @throws InvalidBalancerUrlException |
|
37
|
|
|
* |
|
38
|
|
|
* @return Models\Responses\OrtcResponse |
|
39
|
|
|
*/ |
|
40
|
|
|
public function execute() |
|
41
|
|
|
{ |
|
42
|
|
|
$response = null; |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
$guzzleRequest = $this->createRequest(); |
|
46
|
|
|
|
|
47
|
|
|
/** @var FutureResponse $response */ |
|
48
|
|
|
$response = $this->guzzleClient->send($guzzleRequest); |
|
49
|
|
|
} catch (ClientException $e) { |
|
50
|
|
|
if ($e->getResponse()->getStatusCode() == 401) { |
|
51
|
|
|
throw new UnauthorizedException(); |
|
52
|
|
|
} else { |
|
53
|
|
|
$networkErrorException = new NetworkErrorException(); |
|
54
|
|
|
$networkErrorException->setGuzzleClientException($e); |
|
55
|
|
|
|
|
56
|
|
|
throw $networkErrorException; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$handler = $this->request->getResponseHandler(); |
|
61
|
|
|
|
|
62
|
|
|
return $handler->handle($response); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* execute batch requests (post). |
|
67
|
|
|
* |
|
68
|
|
|
* @throws BatchRequestException |
|
69
|
|
|
* |
|
70
|
|
|
* @return Models\Responses\OrtcResponse |
|
71
|
|
|
*/ |
|
72
|
|
|
public function batchExecute() |
|
73
|
|
|
{ |
|
74
|
|
|
$guzzleRequests = $this->createBatchPostRequests(); |
|
75
|
|
|
|
|
76
|
|
|
$results = Pool::batch($this->guzzleClient, $guzzleRequests, [ |
|
77
|
|
|
'pool_size' => $this->request->getOrtcConfig()->getBatchPoolSize(), |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
if (count($results->getFailures()) > 0) { |
|
|
|
|
|
|
81
|
|
|
$batchRequestException = new BatchRequestException(); |
|
82
|
|
|
$batchRequestException->setResults($results); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
throw $batchRequestException; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$handler = $this->request->getResponseHandler(); |
|
88
|
|
|
|
|
89
|
|
|
return $handler->handle($results); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param \GuzzleHttp\Client $guzzleClient |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setGuzzleClient($guzzleClient) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->guzzleClient = $guzzleClient; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param OrtcRequest $request |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setRequest($request) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->request = $request; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $baseUrl |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setBaseUrl($baseUrl) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->baseUrl = $baseUrl; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* get request options for post requests. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $postData |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getRequestOptionsForPost($postData) |
|
124
|
|
|
{ |
|
125
|
|
|
return [ |
|
126
|
|
|
'verify' => $this->request->getOrtcConfig()->isVerifySsl(), |
|
127
|
|
|
'body' => $postData, |
|
128
|
|
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* get request options for get requests. |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function getRequestOptionsForGet() |
|
137
|
|
|
{ |
|
138
|
|
|
return [ |
|
139
|
|
|
'verify' => $this->request->getOrtcConfig()->isVerifySsl(), |
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* create guzzle GET/POST request. |
|
145
|
|
|
* |
|
146
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function createRequest() |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->request->isPost()) { |
|
151
|
|
|
return $this->guzzleClient->createRequest( |
|
|
|
|
|
|
152
|
|
|
'POST', |
|
153
|
|
|
$this->getRequestUrl(), |
|
154
|
|
|
$this->getRequestOptionsForPost($this->request->getPostData()) |
|
155
|
|
|
); |
|
156
|
|
|
} else { |
|
157
|
|
|
return $this->guzzleClient->createRequest( |
|
|
|
|
|
|
158
|
|
|
'GET', |
|
159
|
|
|
$this->getRequestUrl(), |
|
160
|
|
|
$this->getRequestOptionsForGet() |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* create batch guzzle POST requests. |
|
167
|
|
|
* |
|
168
|
|
|
* @return \GuzzleHttp\Message\Request[] |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function createBatchPostRequests() |
|
171
|
|
|
{ |
|
172
|
|
|
$requests = []; |
|
173
|
|
|
|
|
174
|
|
|
foreach ($this->request->getPostData() as $postData) { |
|
175
|
|
|
$requests[] = $this->guzzleClient->createRequest( |
|
|
|
|
|
|
176
|
|
|
'POST', |
|
177
|
|
|
$this->getRequestUrl(), |
|
178
|
|
|
$this->getRequestOptionsForPost($postData) |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $requests; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* combine path url & baseUrl if needed! |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function getRequestUrl() |
|
189
|
|
|
{ |
|
190
|
|
|
if (!$this->request->isUrlAbsolute()) { |
|
191
|
|
|
return $this->baseUrl . $this->request->getUrlPath(); |
|
192
|
|
|
} else { |
|
193
|
|
|
return $this->request->getUrlPath(); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.