Repository navigation
Expand file tree
/
Copy pathHttpClient.php
More file actions
267 lines (223 loc) · 6.61 KB
/
Copy pathHttpClient.php
File metadata and controls
267 lines (223 loc) · 6.61 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Github\HttpClient;
use Buzz\Browser;
use Buzz\Client\Curl;
use Buzz\Message\MessageInterface;
use Github\Exception\ApiLimitExceedException;
use Github\HttpClient\Listener\AuthListener;
/**
* Performs requests on GitHub API. API documentation should be self-explanatory.
*
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class HttpClient implements HttpClientInterface
{
/**
* @var integer
*/
public $remainingCalls;
/**
* The http client options
* @var array
*/
protected $options = array(
'url' => 'https://api.github.com/:path',
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
'http_port' => 443,
'timeout' => 10,
'api_limit' => 5000,
'auth_method' => null,
'login' => null,
'password' => null,
'token' => null,
);
/**
* @var array
*/
protected static $history = array();
/**
* @var array
*/
protected $headers = array();
/**
* @var Browser
*/
protected $browser;
/**
* @var array
*/
private $lastResponse;
/**
* Instantiated a new http client
*
* @param array $options Http client options
* @param null|Browser $browser Buzz client
*/
public function __construct(array $options = array(), Browser $browser = null)
{
$this->options = array_merge($this->options, $options);
$this->browser = $browser ?: new Browser(new Curl());
$this->browser->getClient()->setTimeout($this->options['timeout']);
$this->browser->getClient()->setVerifyPeer(false);
}
/**
* {@inheritDoc}
*/
public function authenticate()
{
$this->browser->addListener(
new AuthListener(
$this->options['auth_method'],
array('login' => $this->options['login'], 'password' => $this->options['password'], 'token' => $this->options['token'])
)
);
}
/**
* {@inheritDoc}
*/
public function setHeaders(array $headers)
{
$this->headers = $headers;
return $this;
}
/**
* {@inheritDoc}
*/
public function setOption($name, $value)
{
$this->options[$name] = $value;
return $this;
}
/**
* @return null|array
*/
public function getPagination()
{
if (null === $this->lastResponse) {
return null;
}
return $this->lastResponse['pagination'];
}
/**
* {@inheritDoc}
*/
public function get($path, array $parameters = array(), array $options = array())
{
$path .= (false === strpos($path, '?') ? '?' : '&').http_build_query($parameters, '', '&');
return $this->request($path, array(), 'GET', $options);
}
/**
* {@inheritDoc}
*/
public function post($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'POST', $options);
}
/**
* {@inheritDoc}
*/
public function patch($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'PATCH', $options);
}
/**
* {@inheritDoc}
*/
public function delete($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'DELETE', $options);
}
/**
* {@inheritDoc}
*/
public function put($path, array $options = array())
{
return $this->request($path, array(), 'PUT', $options);
}
/**
* Send a request to the server, receive a response,
* decode the response and returns an associative array
*
* @param string $path Request API path
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
* @param array $options Request options
*
* @return array Data
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
$options = array_merge($this->options, $options);
// create full url
$url = strtr($options['url'], array(
':path' => trim($path, '/')
));
// get encoded response
$this->lastResponse = $this->doRequest($url, $parameters, $httpMethod, $options);
return $this->decodeResponse($this->lastResponse['response']);
}
/**
* Send a request to the server, receive a response
*
* @param string $url Request url
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
* @param array $options Request options
*
* @return array HTTP response
*/
protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
$response = $this->browser->call($url, $httpMethod, $this->headers, json_encode($parameters));
$this->checkApiLimit($response);
return array(
'response' => $response->getContent(),
'headers' => $response->getHeaders(),
'pagination' => $this->decodePagination($response),
'errorNumber' => '',
'errorMessage' => ''
);
}
/**
* @param MessageInterface $response
*
* @return array|null
*/
protected function decodePagination(MessageInterface $response)
{
$header = $response->getHeader('Link');
if (empty($header)) {
return null;
}
$pagination = array();
foreach (explode("\n", $header) as $link) {
preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match);
if (3 === count($match)) {
$pagination[$match[2]] = $match[1];
}
}
return $pagination;
}
/**
* {@inheritDoc}
*/
protected function decodeResponse($response)
{
$content = json_decode($response, true);
if (JSON_ERROR_NONE !== json_last_error()) {
return $response;
}
return $content;
}
/**
* {@inheritDoc}
*/
protected function checkApiLimit(MessageInterface $response)
{
$this->remainingCalls = $response->getHeader('X-RateLimit-Remaining');
if (null !== $this->remainingCalls && 1 > $this->remainingCalls) {
throw new ApiLimitExceedException($this->options['api_limit']);
}
}
}