1 | <?php |
||
13 | class GuzzleAdapter implements IHttpClient |
||
14 | { |
||
15 | /** @var \GuzzleHttp\Client The guzzle client. */ |
||
16 | private $client; |
||
17 | |||
18 | /** @var int the timeout of the client or the request. */ |
||
19 | private $timeout; |
||
20 | |||
21 | /** @var array The client headers. */ |
||
22 | private $headers = []; |
||
23 | |||
24 | /** @var bool Set if the request sends an json */ |
||
25 | private $isJson = false; |
||
26 | |||
27 | /** |
||
28 | * GuzzleAdapter constructor. |
||
29 | * |
||
30 | * @param Client $client |
||
31 | */ |
||
32 | 11 | public function __construct(Client $client) |
|
36 | |||
37 | /** |
||
38 | * Make an http request to some URL with the given http method |
||
39 | * |
||
40 | * @param string $method |
||
41 | * @param string $url |
||
42 | * @param array $data |
||
43 | * @param array $config |
||
44 | * |
||
45 | * @return \Katapoka\Ahgora\HttpResponse |
||
46 | */ |
||
47 | 3 | public function request($method, $url, $data = [], array $config = []) |
|
55 | |||
56 | /** |
||
57 | * Make a get request to an URL. |
||
58 | * |
||
59 | * @param string $url |
||
60 | * @param array $data |
||
61 | * @param array $config |
||
62 | * |
||
63 | * @return \Katapoka\Ahgora\HttpResponse |
||
64 | */ |
||
65 | 1 | public function get($url, $data = [], array $config = []) |
|
69 | |||
70 | /** |
||
71 | * Make a post request to an URL. |
||
72 | * |
||
73 | * @param string $url |
||
74 | * @param array $data |
||
75 | * @param array $config |
||
76 | * |
||
77 | * @return \Katapoka\Ahgora\HttpResponse |
||
78 | */ |
||
79 | 1 | public function post($url, $data = [], array $config = []) |
|
83 | |||
84 | /** |
||
85 | * Set a header to the request. |
||
86 | * |
||
87 | * @param string $header |
||
88 | * @param string $value |
||
89 | * |
||
90 | * @return \Katapoka\Ahgora\IHttpClient the instance of the class for method chaining |
||
91 | */ |
||
92 | 5 | public function setHeader($header, $value) |
|
106 | |||
107 | /** |
||
108 | * Unset a header to the request. |
||
109 | * |
||
110 | * @param string $header |
||
111 | * |
||
112 | * @return \Katapoka\Ahgora\IHttpClient the instance of the class for method chaining |
||
113 | */ |
||
114 | 2 | public function unsetHeader($header) |
|
122 | |||
123 | /** |
||
124 | * Retrieves the value of a given header name. |
||
125 | * |
||
126 | * @param string $header |
||
127 | * |
||
128 | * @return string|null |
||
129 | */ |
||
130 | 2 | public function getHeader($header) |
|
138 | |||
139 | /** |
||
140 | * Get all headers from the http client. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 3 | public function getHeaders() |
|
148 | |||
149 | /** |
||
150 | * Set a timeout to the connection. |
||
151 | * |
||
152 | * @param int $timeout |
||
153 | * |
||
154 | * @return \Katapoka\Ahgora\IHttpClient |
||
155 | */ |
||
156 | 1 | public function setTimeout($timeout) |
|
162 | |||
163 | /** |
||
164 | * Set if the request will response a json instead of form data. |
||
165 | * |
||
166 | * @param bool $isJson |
||
167 | * |
||
168 | * @return \Katapoka\Ahgora\IHttpClient |
||
169 | */ |
||
170 | 1 | public function setIsJson($isJson = true) |
|
185 | |||
186 | 3 | private function headerExists($header) |
|
190 | } |
||
191 |