-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathPendingRequest.php
More file actions
328 lines (276 loc) · 8.57 KB
/
Copy pathPendingRequest.php
File metadata and controls
328 lines (276 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
declare(strict_types=1);
namespace Saloon\Http;
use Saloon\Config;
use Saloon\Enums\Method;
use Saloon\Helpers\Helpers;
use Saloon\Traits\Macroable;
use Saloon\Helpers\URLHelper;
use Saloon\Traits\Conditionable;
use Saloon\Traits\HasMockClient;
use Saloon\Contracts\FakeResponse;
use Saloon\Http\Faking\MockClient;
use Saloon\Contracts\Authenticator;
use Saloon\Contracts\Body\BodyRepository;
use Saloon\Http\PendingRequest\MergeBody;
use Saloon\Http\PendingRequest\MergeDelay;
use Saloon\Http\Middleware\DelayMiddleware;
use Saloon\Http\PendingRequest\BootPlugins;
use Saloon\Traits\Auth\AuthenticatesRequests;
use Saloon\Http\Middleware\ValidateProperties;
use Saloon\Http\Middleware\DetermineMockResponse;
use Saloon\Exceptions\InvalidResponseClassException;
use Saloon\Exceptions\Request\FatalRequestException;
use Saloon\Traits\PendingRequest\ManagesPsrRequests;
use Saloon\Http\PendingRequest\MergeRequestProperties;
use Saloon\Http\PendingRequest\BootConnectorAndRequest;
use Saloon\Traits\RequestProperties\HasRequestProperties;
use Saloon\Http\PendingRequest\AuthenticatePendingRequest;
class PendingRequest
{
use AuthenticatesRequests;
use HasRequestProperties;
use ManagesPsrRequests;
use Conditionable;
use HasMockClient;
use Macroable;
/**
* The connector making the request.
*/
protected Connector $connector;
/**
* The request used by the instance.
*
* @var Request<mixed>
*/
protected Request $request;
/**
* The method the request will use.
*/
protected Method $method;
/**
* The URL the request will be made to.
*/
protected string $url;
/**
* The body of the request.
*/
protected ?BodyRepository $body = null;
/**
* The simulated response.
*/
protected ?FakeResponse $fakeResponse = null;
/**
* Determine if the pending request is asynchronous
*/
protected bool $asynchronous = false;
/**
* Build up the request payload.
*
* @param Request<mixed> $request
*/
public function __construct(Connector $connector, Request $request, ?MockClient $mockClient = null)
{
// Let's start by getting our PSR factory collection. This object contains all the
// relevant factories for creating PSR-7 requests as well as URIs and streams.
$this->factoryCollection = $connector->sender()->getFactoryCollection();
// Now we'll set the base properties
$this->connector = $connector;
$this->request = $request;
$this->method = $request->getMethod();
$this->url = URLHelper::join($this->connector->resolveBaseUrl(), $this->request->resolveEndpoint());
$this->authenticator = $request->getAuthenticator() ?? $connector->getAuthenticator();
$this->mockClient = $mockClient ?? $request->getMockClient() ?? $connector->getMockClient() ?? MockClient::getGlobal();
// Now, we'll register our global middleware and our mock response middleware.
// Registering these middleware first means that the mock client can set
// the fake response for every subsequent middleware.
$this->middleware()->merge(Config::globalMiddleware());
$this->middleware()->onRequest(new DetermineMockResponse, 'determineMockResponse');
// Next, we'll boot our plugins. These plugins can add headers, config variables and
// even register their own middleware. We'll use a tap method to simply apply logic
// to the PendingRequest. After that, we will merge together our request properties
// like headers, config, middleware, body and delay, and we'll follow it up by
// invoking our authenticators. We'll do this here because when middleware is
// executed, the developer will have access to any headers added by the middleware.
$this
->tap(new BootPlugins)
->tap(new MergeRequestProperties)
->tap(new MergeBody)
->tap(new MergeDelay)
->tap(new AuthenticatePendingRequest)
->tap(new BootConnectorAndRequest);
// Now, we'll register some default middleware for validating the request properties and
// running the delay that should have been set by the user.
$this->middleware()
->onRequest(new ValidateProperties, 'validateProperties')
->onRequest(new DelayMiddleware, 'delayMiddleware');
// Finally, we will execute the request middleware pipeline which will
// process the middleware in the order we added it.
$this->middleware()->executeRequestPipeline($this);
}
/**
* Authenticate the PendingRequest
*
* @return $this
*/
public function authenticate(Authenticator $authenticator): static
{
$this->authenticator = $authenticator;
// Since the PendingRequest has already been constructed we will run the set
// method on the authenticator which runs it straight away.
$this->authenticator->set($this);
return $this;
}
/**
* Execute the response pipeline.
*
* @param Response<mixed> $response
* @return Response<mixed>
*/
public function executeResponsePipeline(Response $response): Response
{
return $this->middleware()->executeResponsePipeline($response);
}
/**
* Execute the fatal pipeline.
*/
public function executeFatalPipeline(FatalRequestException $throwable): void
{
$this->middleware()->executeFatalPipeline($throwable);
}
/**
* Get the request.
*
* @return Request<mixed>
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* Get the connector.
*/
public function getConnector(): Connector
{
return $this->connector;
}
/**
* Get the URL of the request.
*/
public function getUrl(): string
{
return $this->url;
}
/**
* Set the URL of the PendingRequest
*
* Note: This will be combined with the query parameters to create
* a UriInterface that will be passed to a PSR-7 request.
*
* @return $this
*/
public function setUrl(string $url): static
{
$this->url = $url;
return $this;
}
/**
* Get the HTTP method used for the request
*/
public function getMethod(): Method
{
return $this->method;
}
/**
* Set the method of the PendingRequest
*
* @return $this
*/
public function setMethod(Method $method): static
{
$this->method = $method;
return $this;
}
/**
* Retrieve the body on the instance
*/
public function body(): ?BodyRepository
{
return $this->body;
}
/**
* Set the body repository
*
* @return $this
*/
public function setBody(?BodyRepository $body): static
{
$this->body = $body;
return $this;
}
/**
* Get the fake response
*/
public function getFakeResponse(): ?FakeResponse
{
return $this->fakeResponse;
}
/**
* Set the fake response
*
* @return $this
*/
public function setFakeResponse(?FakeResponse $fakeResponse): static
{
$this->fakeResponse = $fakeResponse;
return $this;
}
/**
* Check if a fake response has been set
*/
public function hasFakeResponse(): bool
{
return $this->fakeResponse instanceof FakeResponse;
}
/**
* Check if the request is asynchronous
*/
public function isAsynchronous(): bool
{
return $this->asynchronous;
}
/**
* Set if the request is going to be sent asynchronously
*
* @return $this
*/
public function setAsynchronous(bool $asynchronous): static
{
$this->asynchronous = $asynchronous;
return $this;
}
/**
* Get the response class
*
* @return class-string<\Saloon\Http\Response<mixed>>
* @throws \Saloon\Exceptions\InvalidResponseClassException
*/
public function getResponseClass(): string
{
$response = $this->request->resolveResponseClass() ?? $this->connector->resolveResponseClass() ?? Response::class;
if (! class_exists($response) || ! Helpers::isSubclassOf($response, Response::class)) {
throw new InvalidResponseClassException;
}
return $response;
}
/**
* Tap into the pending request
*
* @return $this
*/
protected function tap(callable $callable): static
{
$callable($this);
return $this;
}
}