|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Http\Client\Common; |
|
6
|
|
|
|
|
7
|
|
|
use Http\Message\RequestFactory; |
|
8
|
|
|
use Psr\Http\Client\ClientInterface; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
final class HttpMethodsClient implements HttpMethodsClientInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ClientInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $httpClient; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var RequestFactory |
|
21
|
|
|
*/ |
|
22
|
|
|
private $requestFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ClientInterface $httpClient The client to send requests with |
|
26
|
|
|
* @param RequestFactory $requestFactory The message factory to create requests |
|
27
|
|
|
*/ |
|
28
|
8 |
|
public function __construct(ClientInterface $httpClient, RequestFactory $requestFactory) |
|
29
|
|
|
{ |
|
30
|
8 |
|
$this->httpClient = $httpClient; |
|
31
|
8 |
|
$this->requestFactory = $requestFactory; |
|
32
|
8 |
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function get($uri, array $headers = []): ResponseInterface |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->send('GET', $uri, $headers, null); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function head($uri, array $headers = []): ResponseInterface |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $this->send('HEAD', $uri, $headers, null); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function trace($uri, array $headers = []): ResponseInterface |
|
45
|
|
|
{ |
|
46
|
1 |
|
return $this->send('TRACE', $uri, $headers, null); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function post($uri, array $headers = [], $body = null): ResponseInterface |
|
50
|
|
|
{ |
|
51
|
1 |
|
return $this->send('POST', $uri, $headers, $body); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function put($uri, array $headers = [], $body = null): ResponseInterface |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->send('PUT', $uri, $headers, $body); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function patch($uri, array $headers = [], $body = null): ResponseInterface |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->send('PATCH', $uri, $headers, $body); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function delete($uri, array $headers = [], $body = null): ResponseInterface |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->send('DELETE', $uri, $headers, $body); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function options($uri, array $headers = [], $body = null): ResponseInterface |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->send('OPTIONS', $uri, $headers, $body); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
8 |
|
public function send($method, $uri, array $headers = [], $body = null): ResponseInterface |
|
75
|
|
|
{ |
|
76
|
8 |
|
return $this->sendRequest($this->requestFactory->createRequest( |
|
77
|
8 |
|
$method, |
|
78
|
8 |
|
$uri, |
|
79
|
8 |
|
$headers, |
|
80
|
8 |
|
$body |
|
81
|
|
|
)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
8 |
|
public function sendRequest(RequestInterface $request): ResponseInterface |
|
85
|
|
|
{ |
|
86
|
8 |
|
return $this->httpClient->sendRequest($request); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|