1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\Travis; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use React\Promise\Deferred; |
8
|
|
|
|
9
|
|
|
class Client |
10
|
|
|
{ |
11
|
|
|
const VERSION = '0.0.1-alpha1'; |
12
|
|
|
const USER_AGENT = 'wyrihaximus/travis-client/' . self::VERSION; |
13
|
|
|
const API_VERSION = 'application/vnd.travis-ci.2+json'; |
14
|
|
|
const API_HOST_OPEN_SOURCE = 'api.travis-ci.org'; |
15
|
|
|
const API_HOST_PRO = 'api.travis-ci.com'; |
16
|
|
|
const API_HOST = self::API_HOST_OPEN_SOURCE; |
17
|
|
|
const API_SCHEMA = 'https'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var callable |
21
|
|
|
*/ |
22
|
|
|
protected $handler; |
23
|
|
|
|
24
|
|
|
public function __construct(callable $handler = null) |
25
|
|
|
{ |
26
|
|
|
if ($handler === null) { |
27
|
|
|
$handler = \Aws\default_http_handler(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->handler = $handler; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function request(EndpointInterface $endpoint) |
34
|
|
|
{ |
35
|
|
|
$handler = $this->handler; |
36
|
|
|
return $endpoint->fromResponse($handler($endpoint->getRequest())->wait()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function requestAsync(EndpointInterface $endpoint) |
40
|
|
|
{ |
41
|
|
|
$deferred = new Deferred(); |
42
|
|
|
$handler = $this->handler; |
43
|
|
|
$handler($endpoint->getRequest())->then(function (ResponseInterface $response) use ($deferred, $endpoint) { |
44
|
|
|
$deferred->resolve($endpoint->fromResponse($response)); |
45
|
|
|
}, function ($error) use ($deferred) { |
46
|
|
|
$deferred->reject($error); |
47
|
|
|
}); |
48
|
|
|
return $deferred->promise(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function createRequest(string $method, string $path) |
52
|
|
|
{ |
53
|
|
|
$url = self::API_SCHEMA . '://' . self::API_HOST . '/' . $path; |
54
|
|
|
return new Request($method, $url, [ |
55
|
|
|
'User-Agent' => self::USER_AGENT, |
56
|
|
|
'Accept' => self::API_VERSION, |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|