Repository navigation
Expand file tree
/
Copy pathHttpClient.php
More file actions
218 lines (187 loc) · 5.68 KB
/
Copy pathHttpClient.php
File metadata and controls
218 lines (187 loc) · 5.68 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
<?php
namespace Github\HttpClient;
use Buzz\Browser;
use Buzz\Client\Curl;
use Buzz\Message\Response;
/**
* Performs requests on GitHub API. API documentation should be self-explanatory.
*
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @license MIT License
*/
class HttpClient implements HttpClientInterface
{
/**
* 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,
'login' => null,
'password' => null,
'token' => null,
);
/**
* @var array
*/
protected static $history = array();
/**
* @var array
*/
protected $headers = array();
/**
* @var Buzz\Browser
*/
protected $browser;
/**
* 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);
if ($this->options['login']) {
$this->browser->addListener(
new Listener\AuthListener(
$this->options['auth_method'],
array($this->options['login'], $this->options['password'])
)
);
}
}
public function setHeaders($headers)
{
$this->headers = $headers;
}
/**
* Change an option value.
*
* @param string $name The option name
* @param mixed $value The value
*
* @return self The current object instance
*/
public function setOption($name, $value)
{
$this->options[$name] = $value;
return $this;
}
/**
* {@inheridoc}
*/
public function get($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'GET', $options);
}
/**
* {@inheridoc}
*/
public function post($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'POST', $options);
}
/**
* {@inheridoc}
*/
public function patch($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'PATCH', $options);
}
/**
* {@inheridoc}
*/
public function delete($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'DELETE', $options);
}
/**
* {@inheridoc}
*/
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
$response = $this->doRequest($url, $parameters, $httpMethod, $options);
return $this->decodeResponse($response['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 string 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(),
'errorNumber' => '',
'errorMessage' => ''
);
}
/**
* Get a JSON response and transform it to a PHP array
*
* @param string $response The response
*
* @return array The content of response
*
* @throws \RuntimeException
*/
protected function decodeResponse($response)
{
$content = json_decode($response, true);
if (JSON_ERROR_NONE !== json_last_error()) {
return $response;
}
return $content;
}
/**
* Report to user he reached his GitHub API limit.
*
* @param Response $response
*
* @throws \RuntimeException
*/
protected function checkApiLimit(Response $response)
{
$limit = $response->getHeader('X-RateLimit-Remaining');
if (null !== $limit && 1 > $limit) {
throw new \RuntimeException('You have reached GitHub hour limit! Actual limit is: '. $this->options['api_limit']);
}
}
}