Repository navigation
Expand file tree
/
Copy pathClient.php
More file actions
319 lines (280 loc) · 8.17 KB
/
Copy pathClient.php
File metadata and controls
319 lines (280 loc) · 8.17 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
namespace Github;
use Github\Api\ApiInterface;
use Github\HttpClient\HttpClientInterface;
use Github\HttpClient\HttpClient;
/**
* Simple yet very cool PHP Github client
*
* @tutorial http://github.com/ornicar/php-github-api/blob/master/README.markdown
* @version 3.2
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @license MIT License
*
* Website: http://github.com/ornicar/php-github-api
* Tickets: http://github.com/ornicar/php-github-api/issues
*/
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
*/
protected $httpClient = null;
/**
* HTTP Headers
*
* @var array
*/
private $headers = array();
/**
* The list of loaded API instances
*
* @var array
*/
protected $apis = array();
/**
* Instanciate 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, $method = null)
{
$this->getHttpClient()->setOption('auth_method', $method ?: self::AUTH_URL_TOKEN);
if (self::AUTH_HTTP_PASSWORD) {
$this->getHttpClient()->setOption('login', $login)
->setOption('password', $secret);
} else {
$this->getHttpClient()->setOption('token', $secret);
}
}
/**
* Deauthenticate a user for all next requests
*/
public function deAuthenticate()
{
$this->authenticate(null, null, null);
}
/**
* 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;
}
/**
* Get the user API
*
* @return Api\User the user API
*/
public function getUserApi()
{
if (!isset($this->apis['user'])) {
$this->apis['user'] = new Api\User($this);
}
return $this->apis['user'];
}
/**
* Get the issue API
*
* @return Api\Issue the issue API
*/
public function getIssueApi()
{
if (!isset($this->apis['issue'])) {
$this->apis['issue'] = new Api\Issue($this);
}
return $this->apis['issue'];
}
/**
* Get the commit API
*
* @return Api\Commit the commit API
*/
public function getCommitApi()
{
if (!isset($this->apis['commit'])) {
$this->apis['commit'] = new Api\Commit($this);
}
return $this->apis['commit'];
}
/**
* Get the repo API
*
* @return Api\Repo the repo API
*/
public function getRepoApi()
{
if (!isset($this->apis['repo'])) {
$this->apis['repo'] = new Api\Repo($this);
}
return $this->apis['repo'];
}
/**
* Get the organization API
*
* @return Api\Organization the object API
*/
public function getOrganizationApi()
{
if (!isset($this->apis['organization'])) {
$this->apis['organization'] = new Api\Organization($this);
}
return $this->apis['organization'];
}
/**
* Get the object API
*
* @return Api\Object the object API
*/
public function getObjectApi()
{
if (!isset($this->apis['object'])) {
$this->apis['object'] = new Api\Object($this);
}
return $this->apis['object'];
}
/**
* Get the pull request API
*
* @return Api\PullRequest the pull request API
*/
public function getPullRequestApi()
{
if (!isset($this->apis['pullrequest'])) {
$this->apis['pullrequest'] = new Api\PullRequest($this);
}
return $this->apis['pullrequest'];
}
/**
* Inject an API instance
*
* @param string $name the API name
* @param ApiInterface $api the API instance
*
* @return self
*/
public function setApi($name, ApiInterface $instance)
{
$this->apis[$name] = $instance;
return $this;
}
/**
* Get any API
*
* @param string $name the API name
* @return ApiInterface the API instance
*/
public function getApi($name)
{
return $this->apis[$name];
}
public function clearHeaders()
{
$this->setHeaders(array());
}
public function setHeaders($headers)
{
$this->headers = $headers;
}
}