-
Notifications
You must be signed in to change notification settings - Fork 2
/
WpClient.php
81 lines (70 loc) · 2.46 KB
/
WpClient.php
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
<?php
declare(strict_types=1);
namespace RebelCode\WordPress\Http;
use InvalidArgumentException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use RebelCode\Psr7\UriResolver;
/**
* The PSR-18 client implementation.
*
* @psalm-import-type WpHandlerOptions from WpHandler
*/
class WpClient implements ClientInterface
{
/** @var HandlerInterface */
protected $handler;
/** @var UriInterface|null */
protected $baseUri;
/**
* Constructor.
*
* @param HandlerInterface $handler The handler to use for dispatching requests and receiving responses.
* @param UriInterface|null $baseUri Optional base URI for all relative requests sent using this client.
*
* @throws InvalidArgumentException If the "base_uri" option is present and is not a valid URI.
*/
public function __construct(HandlerInterface $handler, ?UriInterface $baseUri = null)
{
$this->handler = $handler;
$this->baseUri = $baseUri;
}
/** @inheritDoc */
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->handler->handle($this->prepareRequest($request));
}
/**
* Prepares the request before handing off to the handler.
*
* @param RequestInterface $request The request to prepare.
*
* @return RequestInterface The prepared requested.
*/
protected function prepareRequest(RequestInterface $request): RequestInterface
{
if ($this->baseUri !== null) {
$preserveHost = $request->hasHeader('Host');
$requestUri = $request->getUri();
$resolvedUri = UriResolver::resolve($this->baseUri, $requestUri);
$request = $request->withUri($resolvedUri, $preserveHost);
}
return $request;
}
/**
* Creates a client with the default configuration.
*
* @param UriInterface|null $baseUri Optional base URI for all relative requests sent using this client.
* @param array<string, mixed> $options Optional array of options for the underlying {@link WpHandler}.
*
* @psalm-param WpHandlerOptions $options
*
* @return WpClient The created client instance.
*/
public static function createDefault(?UriInterface $baseUri = null, array $options = []): WpClient
{
return new self(HandlerStack::createDefault($options), $baseUri);
}
}