1 | <?php |
||
5 | class HttpClient implements HttpClientInterface |
||
6 | { |
||
7 | /** |
||
8 | * Client config. |
||
9 | * |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $config; |
||
13 | |||
14 | /** |
||
15 | * Headers returned in the response. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $responseHeaders = []; |
||
20 | |||
21 | /** |
||
22 | * Assign dependencies. |
||
23 | * |
||
24 | * @param array $config |
||
25 | */ |
||
26 | 20 | public function __construct(array $config = []) |
|
30 | |||
31 | /** |
||
32 | * Adds an option to config options array |
||
33 | * |
||
34 | * @param string $option |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | 4 | public function addConfigOption($option) |
|
39 | { |
||
40 | 4 | $this->config['options'][] = $option; |
|
41 | 4 | } |
|
42 | |||
43 | /** |
||
44 | * Make a POST request. |
||
45 | * |
||
46 | * @param string $path |
||
47 | * @param mixed $body |
||
48 | * @param array $headers |
||
49 | * |
||
50 | * @return Request |
||
51 | */ |
||
52 | 8 | public function post($path, $body = null, array $headers = []) |
|
56 | |||
57 | /** |
||
58 | * Send request with HTTP client. |
||
59 | * |
||
60 | * @param string $path |
||
61 | * @param mixed $body |
||
62 | * @param string $method |
||
63 | * @param array $headers |
||
64 | * |
||
65 | * @return Response |
||
66 | */ |
||
67 | 8 | protected function request($path, $body = null, $method = 'GET', array $headers = []) |
|
74 | |||
75 | /** |
||
76 | * Create request with HTTP client. |
||
77 | * |
||
78 | * @param string $method |
||
79 | * @param string $path |
||
80 | * @param mixed $body |
||
81 | * @param array $headers |
||
82 | * |
||
83 | * @return Request |
||
84 | */ |
||
85 | 8 | protected function createRequest($method, $path, $body = null, array $headers = []) |
|
95 | |||
96 | /** |
||
97 | * Send the request. |
||
98 | * |
||
99 | * @param Request $request |
||
100 | * |
||
101 | * @return Response |
||
102 | */ |
||
103 | 4 | public function send(Request $request) |
|
121 | |||
122 | /** |
||
123 | * Callback to store response headers. |
||
124 | * |
||
125 | * @param resource $curl |
||
126 | * @param string $header |
||
127 | * |
||
128 | * @return int |
||
129 | */ |
||
130 | 2 | public function headerCallback($curl, $header) |
|
140 | |||
141 | /** |
||
142 | * Prepare the request headers to be sent. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * @param array $headers |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 4 | protected function getRequestHeaders(Request $request, array $headers = []) |
|
157 | |||
158 | /** |
||
159 | * Build uri with possible base uri. |
||
160 | * |
||
161 | * @param string $uri |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 8 | protected function buildUri($uri) |
|
185 | } |
||
186 |