-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractApi.php
More file actions
487 lines (424 loc) · 11.6 KB
/
AbstractApi.php
File metadata and controls
487 lines (424 loc) · 11.6 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
namespace Scion\GitHub;
use Scion\File\Parser\Json as JsonParser;
use Scion\Http\Client\Curl;
use Scion\Http\Request;
use Scion\Utils\Strings;
use Scion\Validator\Json as JsonValidator;
abstract class AbstractApi {
/** API version */
const API_VERSION = 'v3';
/** API constants */
const API_URL = 'https://api.github.com';
const API_UPLOADS = 'https://uploads.github.com';
const API_RAW_URL = 'https://raw.github.com';
const CONTENT_TYPE = 'application/json';
const DEFAULT_ACCEPT = 'application/vnd.github.' . self::API_VERSION . '+json';
const USER_AGENT = 'scion.github-api';
/** Archive constants */
const ARCHIVE_TARBALL = 'tarball';
const ARCHIVE_ZIPBALL = 'zipball';
/** Authentication constants */
const OAUTH_AUTH = 0;
const OAUTH2_HEADER_AUTH = 1;
const OAUTH2_PARAMETERS_AUTH = 2;
/** Branch constants */
const BRANCH_MASTER = 'master';
const BRANCH_DEVELOP = 'develop';
/** Direction constants */
const DIRECTION_ASC = 'asc';
const DIRECTION_DESC = 'desc';
/** Environment constants */
const ENVIRONMENT_PRODUCTION = 'production';
const ENVIRONMENT_STAGING = 'staging';
const ENVIRONMENT_QA = 'qa';
/** Events constants */
const EVENTS_PULL = 'pull';
const EVENTS_PULL_REQUEST = 'pull_request';
const EVENTS_PUSH = 'push';
/** Filter constants */
const FILTER_ALL = 'all';
const FILTER_ASSIGNED = 'assigned';
const FILTER_CREATED = 'created';
const FILTER_MENTIONED = 'mentioned';
const FILTER_SUBSCRIBED = 'subscribed';
/** Media types constants */
const MEDIA_TYPE_JSON = 'json';
const MEDIA_TYPE_RAW = 'raw';
const MEDIA_TYPE_FULL = 'full';
const MEDIA_TYPE_TEXT = 'text';
/** Modes constants */
const MODE_MARKDOWN = 'markdown';
const MODE_GFM = 'gfm';
/** Permissions constants */
const PERMISSION_ADMIN = 'admin';
const PERMISSION_PULL = 'pull';
const PERMISSION_PUSH = 'push';
/** Sort constants */
const SORT_COMPLETENESS = 'completeness';
const SORT_CREATED = 'created';
const SORT_DUE_DATE = 'due_date';
const SORT_FULL_NAME = 'full_name';
const SORT_NEWEST = 'newest';
const SORT_OLDEST = 'oldest';
const SORT_PUSHED = 'pushed';
const SORT_STARGAZERS = 'stargazers';
const SORT_UPDATED = 'updated';
/** State constants */
const STATE_ACTIVE = 'active';
const STATE_ALL = 'all';
const STATE_CLOSED = 'closed';
const STATE_ERROR = 'error';
const STATE_FAILURE = 'failure';
const STATE_OPEN = 'open';
const STATE_PENDING = 'pending';
const STATE_SUCCESS = 'success';
/** Task constants */
const TASK_DEPLOY = 'deploy';
const TASK_DEPLOY_MIGRATIONS = 'deploy:migrations';
/** Type constants */
const TYPE_ALL = 'all';
const TYPE_COMMENTS = 'comments';
const TYPE_GISTS = 'gists';
const TYPE_HOOKS = 'hooks';
const TYPE_ISSUES = 'issues';
const TYPE_MEMBER = 'member';
const TYPE_MILESTONES = 'milestones';
const TYPE_ORGS = 'orgs';
const TYPE_OWNER = 'owner';
const TYPE_PAGES = 'pages';
const TYPE_PUBLIC = 'public';
const TYPE_PULLS = 'pulls';
const TYPE_PRIVATE = 'private';
const TYPE_REPOS = 'repos';
const TYPE_USERS = 'users';
/** Properties */
protected $accept = self::DEFAULT_ACCEPT;
protected $apiUrl = self::API_URL;
protected $authentication = self::OAUTH_AUTH;
protected $clientId;
protected $clientSecret;
protected $contentType = self::CONTENT_TYPE;
protected $failure;
protected $headers = [];
protected $httpAuth = ['username' => '', 'password' => ''];
protected $jsonValidator;
protected $success;
protected $string;
protected $timeout = 240;
protected $token;
/**
* Constructor
*/
public function __construct() {
$this->string = new Strings();
$this->jsonValidator = new JsonValidator();
}
/**
* Get accept
* @return mixed
*/
public function getAccept() {
return $this->accept;
}
/**
* Set accept
* @param array|string $accept
* @return AbstractApi
*/
public function setAccept($accept) {
$this->accept = $accept;
return $this;
}
/**
* Get authentication
* @return int
*/
public function getAuthentication() {
return $this->authentication;
}
/**
* Set authentication
* @param int $authentication
* @return $this
*/
public function setAuthentication($authentication) {
$this->authentication = $authentication;
return $this;
}
/**
* Get apiUrl
* @return string
*/
public function getApiUrl() {
return $this->apiUrl;
}
/**
* Set apiUrl
* @param mixed $apiUrl
* @return $this
*/
public function setApiUrl($apiUrl) {
$this->apiUrl = $apiUrl;
return $this;
}
/**
* Get clientId
* @return null|int
*/
public function getClientId() {
return $this->clientId;
}
/**
* Set clientId
* @param mixed $clientId
* @return $this
*/
public function setClientId($clientId) {
$this->clientId = $clientId;
return $this;
}
/**
* Get clientSecret
* @return null|string
*/
public function getClientSecret() {
return $this->clientSecret;
}
/**
* Set clientSecret
* @param mixed $clientSecret
* @return $this
*/
public function setClientSecret($clientSecret) {
$this->clientSecret = $clientSecret;
return $this;
}
/**
* Get httpAuth
* @return array
*/
public function getHttpAuth() {
return $this->httpAuth;
}
/**
* Set httpAuth
* @param string $username
* @param string $password
* @return $this
*/
public function setHttpAuth($username, $password = '') {
$this->httpAuth['username'] = $username;
$this->httpAuth['password'] = $password;
return $this;
}
/**
* Get token
* @return string
*/
public function getToken() {
return $this->token;
}
/**
* Set token
* @param string $token
* @param int $authentication
* @return $this
*/
public function setToken($token, $authentication = self::OAUTH_AUTH) {
$this->token = $token;
$this->setAuthentication($authentication);
return $this;
}
/**
* Get timeout
* @return int
*/
public function getTimeout() {
return $this->timeout;
}
/**
* Set timeout
* @param int $timeout
* @return $this
*/
public function setTimeout($timeout) {
$this->timeout = $timeout;
return $this;
}
/**
* Get contentType
* @return string
*/
public function getContentType() {
return $this->contentType;
}
/**
* Set contentType
* @param string $contentType
* @return AbstractApi
*/
public function setContentType($contentType) {
$this->contentType = $contentType;
return $this;
}
/**
* Get string
* @return \Scion\Utils\Strings
*/
public function getString() {
return $this->string;
}
/**
* Get headers
* @return array
*/
public function getHeaders() {
return $this->headers;
}
/**
* Curl request
* @param string $url
* @param string $method
* @param array $postFields
* @param null|string $apiUrl
* @return string
*/
public function request($url, $method = Request::METHOD_GET, $postFields = [], $apiUrl = null) {
/** Building url */
if (null === $apiUrl) {
$apiUrl = $this->getApiUrl();
}
$url = $apiUrl . $url;
/**
* OAuth2 Key/Secret authentication
* @see https://developer.github.com/v3/#oauth2-keysecret
*/
if (null !== $this->getClientId() && null !== $this->getClientSecret()) {
$url .= (strstr($url, '?') !== false ? '&' : '?');
$url .= http_build_query(['client_id' => $this->getClientId(), 'client_secret' => $this->getClientSecret()]);
}
/**
* Basic authentication via OAuth2 Token (sent as a parameter)
* @see https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter
*/
else if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) {
$url .= http_build_query(['access_token' => $this->getToken()]);
}
/** Call curl */
$curl = new Curl();
$curl->setOption([
CURLOPT_USERAGENT => self::USER_AGENT,
CURLOPT_TIMEOUT => $this->getTimeout(),
CURLOPT_HEADER => false, // Use $client->getHeaders() to get full header
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => [
'Accept: ' . $this->getAccept(),
'Content-Type: ' . $this->getContentType()
],
CURLOPT_URL => $url
]);
/**
* Basic authentication via username and Password
* @see https://developer.github.com/v3/auth/#via-username-and-password
*/
if (!empty($this->getHttpAuth())) {
if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) {
$curl->setOption([
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->getHttpAuth()['username']
]);
}
else {
$curl->setOption([
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => sprintf('%s:%s', $this->getHttpAuth()['username'], $this->getHttpAuth()['password'])
]);
}
}
if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) {
/**
* Basic authentication via OAuth token
* @see https://developer.github.com/v3/auth/#via-oauth-tokens
**/
if ($this->getAuthentication() === self::OAUTH_AUTH) {
$curl->setOption([
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => sprintf('%s:x-oauth-basic', $this->getToken())
]);
}
/**
* Basic authentication via OAuth2 Token (sent in a header)
* @see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
*/
else if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) {
$curl->setOption([
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())]
]);
}
}
/** Methods */
switch ($method) {
/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */
case Request::METHOD_DELETE:
/** @see http://tools.ietf.org/html/rfc5789 */
case Request::METHOD_PATCH:
$curl->setOption([
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(array_filter($postFields))
]);
break;
/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */
case Request::METHOD_GET:
$curl->setOption(CURLOPT_HTTPGET, true);
break;
/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */
case Request::METHOD_HEAD:
$curl->setOption([
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_NOBODY => true
]);
break;
/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */
case Request::METHOD_POST:
$curl->setOption([
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(array_filter($postFields))
]);
break;
/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */
case Request::METHOD_PUT:
$curl->setOption([
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_PUT => true,
CURLOPT_HTTPHEADER => [
'X-HTTP-Method-Override: PUT',
'Content-type: application/x-www-form-urlencoded'
]
]);
break;
default:
break;
}
$curl->success(function (Curl $instance) {
$this->headers = $instance->getHeaders();
$this->success = $instance->getResponse();
if ($this->jsonValidator->isValid($this->success)) {
$this->success = JsonParser::decode($this->success);
}
});
$curl->error(function (Curl $instance) {
$this->headers = $instance->getHeaders();
$this->failure = $instance->getResponse();
if ($this->jsonValidator->isValid($this->failure)) {
$this->failure = JsonParser::decode($this->failure);
}
});
$curl->perform();
return (null != $this->success ? $this->success : $this->failure);
}
}