generated from swisnl/skeleton-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
80 lines (67 loc) · 2.65 KB
/
Client.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
<?php
declare(strict_types=1);
namespace Swis\Laravel\Bridge\PsrHttpClient;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Psr\Http\Client\ClientInterface as PsrClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class Client implements GuzzleClientInterface, PsrClientInterface
{
/**
* @var callable
*/
protected $pendingRequestFactory;
public function __construct(?callable $pendingRequestFactory = null)
{
$this->pendingRequestFactory = $pendingRequestFactory ?? static function (): PendingRequest {
return Http::withOptions([]);
};
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->newPendingRequest()
->withHeaders($request->getHeaders())
->send($request->getMethod(), (string) $request->getUri(), ['body' => $request->getBody()])
->toPsrResponse();
}
public function send(RequestInterface $request, array $options = []): ResponseInterface
{
return $this->newPendingRequest()
->withHeaders($request->getHeaders())
->send($request->getMethod(), (string) $request->getUri(), array_merge(['body' => $request->getBody()], $options))
->toPsrResponse();
}
public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
{
/** @var \GuzzleHttp\Promise\PromiseInterface */
return $this->newPendingRequest()
->async()
->withHeaders($request->getHeaders())
->send($request->getMethod(), (string) $request->getUri(), array_merge(['body' => $request->getBody()], $options));
}
public function request(string $method, $uri, array $options = []): ResponseInterface
{
return $this->newPendingRequest()
->send($method, (string) $uri, $options)
->toPsrResponse();
}
public function requestAsync(string $method, $uri, array $options = []): PromiseInterface
{
/** @var \GuzzleHttp\Promise\PromiseInterface */
return $this->newPendingRequest()
->async()
->send($method, (string) $uri, $options);
}
public function getConfig(?string $option = null)
{
$options = $this->newPendingRequest()->getOptions();
return $option === null ? $options : ($options[$option] ?? null);
}
protected function newPendingRequest(): PendingRequest
{
return call_user_func($this->pendingRequestFactory);
}
}