Repository navigation
Expand file tree
/
Copy pathClient.php
More file actions
260 lines (225 loc) · 6.98 KB
/
Copy pathClient.php
File metadata and controls
260 lines (225 loc) · 6.98 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
<?php
namespace Github;
use Github\Api\ApiInterface;
use Github\HttpClient\HttpClientInterface;
use Github\HttpClient\HttpClient;
/**
* Simple yet very cool PHP Github client
*
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @author Joseph Bielawski <stloyd@gmail.com>
*
* Website: http://github.com/KnpLabs/php-github-api
*/
class Client
{
/**
* Constant for authentication method. Indicates the default, but deprecated
* login with username and token in URL.
*/
const AUTH_URL_TOKEN = 'url_token';
/**
* Constant for authentication method. Indicates the new favored login method
* with username and password via HTTP Authentication.
*/
const AUTH_HTTP_PASSWORD = 'http_password';
/**
* Constant for authentication method. Indicates the new login method with
* with username and token via HTTP Authentication.
*/
const AUTH_HTTP_TOKEN = 'http_token';
/**
* The httpClient instance used to communicate with GitHub
*
* @var HttpClientInterface
*/
private $httpClient = null;
/**
* The list of loaded API instances
*
* @var array
*/
private $apis = array();
/**
* HTTP Headers
*
* @var array
*/
private $headers = array();
/**
* Instantiate a new GitHub client
*
* @param HttpClientInterface $httpClient custom http client
*/
public function __construct(HttpClientInterface $httpClient = null)
{
$this->httpClient = $httpClient ?: new HttpClient();
}
/**
* Authenticate a user for all next requests
*
* @param string $login GitHub username
* @param string $secret GitHub private token or Github password if $method == AUTH_HTTP_PASSWORD
* @param null|string $method One of the AUTH_* class constants
*/
public function authenticate($login, $secret = null, $method = null)
{
$this->getHttpClient()->setOption('auth_method', $method);
if ($method === self::AUTH_HTTP_PASSWORD) {
$this
->getHttpClient()
->setOption('login', $login)
->setOption('password', $secret)
;
} else {
$this->getHttpClient()->setOption('token', $secret);
}
$this->getHttpClient()->authenticate();
}
/**
* Call any path, GET method
* Ex: $api->get('repos/show/my-username/my-repo')
*
* @param string $path the GitHub path
* @param array $parameters GET parameters
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function get($path, array $parameters = array(), $requestOptions = array())
{
return $this->getHttpClient()->get($path, $parameters, $requestOptions);
}
/**
* Call any path, POST method
* Ex: $api->post('repos/show/my-username', array('email' => 'my-new-email@provider.org'))
*
* @param string $path the GitHub path
* @param array $parameters POST parameters
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function post($path, array $parameters = array(), $requestOptions = array())
{
return $this->getHttpClient()->post($path, $parameters, $requestOptions);
}
/**
* Call any path, PUT method
*
* @param string $path the GitHub path
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function put($path, $requestOptions = array())
{
return $this->getHttpClient()->put($path, $requestOptions);
}
/**
* Call any path, PATCH method
*
* @param string $path the GitHub path
* @param array $parameters Patch parameters
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function patch($path, array $parameters = array(), $requestOptions = array())
{
return $this->getHttpClient()->patch($path, $parameters, $requestOptions);
}
/**
* Call any path, DELETE method
*
* @param string $path the GitHub path
* @param array $parameters DELETE parameters
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function delete($path, array $parameters = array(), $requestOptions = array())
{
return $this->getHttpClient()->delete($path, $parameters, $requestOptions);
}
/**
* Get the http client.
*
* @return HttpClientInterface a request instance
*/
public function getHttpClient()
{
$this->httpClient->setHeaders($this->headers);
return $this->httpClient;
}
/**
* Inject another http client
*
* @param HttpClientInterface $httpClient The httpClient instance
*/
public function setHttpClient(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
/**
* @param string $name
*
* @return ApiInterface
*
* @throws \InvalidArgumentException
*/
public function api($name)
{
if (!isset($this->apis[$name])) {
switch ($name) {
case 'current_user':
$api = new Api\CurrentUser($this);
break;
case 'git_data':
$api = new Api\GitData($this);
break;
case 'gists':
$api = new Api\Gists($this);
break;
case 'issue':
$api = new Api\Issue($this);
break;
case 'markdown':
$api = new Api\Markdown($this);
break;
case 'organization':
$api = new Api\Organization($this);
break;
case 'pull_request':
$api = new Api\PullRequest($this);
break;
case 'repo':
$api = new Api\Repo($this);
break;
case 'user':
$api = new Api\User($this);
break;
default:
throw new \InvalidArgumentException();
}
$this->apis[$name] = $api;
}
return $this->apis[$name];
}
/**
* @return mixed
*/
public function getRateLimit()
{
return $this->get('rate_limit');
}
/**
* Clears used headers
*/
public function clearHeaders()
{
$this->setHeaders(array());
}
/**
* @param array $headers
*/
public function setHeaders($headers)
{
$this->headers = $headers;
}
}