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\Contracts\IHttpResponse |
||
46 | */ |
||
47 | 3 | public function request($method, $url, $data = [], array $config = []) |
|
66 | |||
67 | /** |
||
68 | * Make a get request to an URL. |
||
69 | * |
||
70 | * @param string $url |
||
71 | * @param array $data |
||
72 | * @param array $config |
||
73 | * |
||
74 | * @return \Katapoka\Ahgora\Contracts\IHttpResponse |
||
75 | */ |
||
76 | 1 | public function get($url, $data = [], array $config = []) |
|
80 | |||
81 | /** |
||
82 | * Make a post request to an URL. |
||
83 | * |
||
84 | * @param string $url |
||
85 | * @param array $data |
||
86 | * @param array $config |
||
87 | * |
||
88 | * @return \Katapoka\Ahgora\Contracts\IHttpResponse |
||
89 | */ |
||
90 | 1 | public function post($url, $data = [], array $config = []) |
|
96 | |||
97 | /** |
||
98 | * Set a header to the request. |
||
99 | * |
||
100 | * @param string $header |
||
101 | * @param string $value |
||
102 | * |
||
103 | 5 | * @return \Katapoka\Ahgora\Contracts\IHttpClient the instance of the class for method chaining |
|
104 | */ |
||
105 | 5 | public function setHeader($header, $value) |
|
119 | |||
120 | /** |
||
121 | * Unset a header to the request. |
||
122 | * |
||
123 | * @param string $header |
||
124 | * |
||
125 | 2 | * @return \Katapoka\Ahgora\Contracts\IHttpClient the instance of the class for method chaining |
|
126 | */ |
||
127 | 2 | public function unsetHeader($header) |
|
135 | |||
136 | /** |
||
137 | * Retrieves the value of a given header name. |
||
138 | * |
||
139 | * @param string $header |
||
140 | * |
||
141 | 2 | * @return string|null |
|
142 | */ |
||
143 | 2 | public function getHeader($header) |
|
151 | |||
152 | /** |
||
153 | * Get all headers from the http client. |
||
154 | * |
||
155 | 3 | * @return array |
|
156 | */ |
||
157 | 3 | public function getHeaders() |
|
161 | |||
162 | /** |
||
163 | * Set a timeout to the connection. |
||
164 | * |
||
165 | * @param int $timeout |
||
166 | * |
||
167 | 1 | * @return \Katapoka\Ahgora\Contracts\IHttpClient |
|
168 | */ |
||
169 | 1 | public function setTimeout($timeout) |
|
175 | |||
176 | /** |
||
177 | * Set if the request will response a json instead of form data. |
||
178 | * |
||
179 | * @param bool $isJson |
||
180 | * |
||
181 | 1 | * @return \Katapoka\Ahgora\Contracts\IHttpClient |
|
182 | */ |
||
183 | 1 | public function setIsJson($isJson = true) |
|
198 | |||
199 | 3 | private function headerExists($header) |
|
203 | } |
||
204 |