1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\Travis\Transport; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use React\Promise\Deferred; |
10
|
|
|
use WyriHaximus\Travis\Resource\Async; |
11
|
|
|
use WyriHaximus\Travis\Resource\Sync; |
12
|
|
|
|
13
|
|
|
class Client |
14
|
|
|
{ |
15
|
|
|
const VERSION = '0.0.1-alpha1'; |
16
|
|
|
const USER_AGENT = 'wyrihaximus/travis-client/' . self::VERSION; |
17
|
|
|
const API_VERSION = 'application/vnd.travis-ci.2+json'; |
18
|
|
|
const API_HOST_OPEN_SOURCE = 'api.travis-ci.org'; |
19
|
|
|
const API_HOST_PRO = 'api.travis-ci.com'; |
20
|
|
|
const API_HOST = self::API_HOST_OPEN_SOURCE; |
21
|
|
|
const API_SCHEMA = 'https'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var callable |
25
|
|
|
*/ |
26
|
|
|
protected $handler; |
27
|
|
|
protected $loop; |
28
|
|
|
protected $options = []; |
29
|
|
|
protected $hydrator; |
30
|
|
|
|
31
|
|
|
public function __construct(LoopInterface $loop, callable $handler = null, $options = []) |
32
|
|
|
{ |
33
|
|
|
$this->loop = $loop; |
34
|
|
|
if ($handler === null) { |
35
|
|
|
$handler = \Aws\default_http_handler(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->handler = $handler; |
39
|
|
|
$this->options = $options; |
40
|
|
|
$this->hydrator = new Hydrator($this, $options); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function request($path) |
44
|
|
|
{ |
45
|
|
|
$deferred = new Deferred(); |
46
|
|
|
$handler = $this->handler; |
47
|
|
|
$handler($this->createRequest('GET', $path))->then(function (ResponseInterface $response) use ($deferred) { |
48
|
|
|
$deferred->resolve(json_decode($response->getBody()->getContents(), true)); |
49
|
|
|
}, function ($error) use ($deferred) { |
50
|
|
|
$deferred->reject($error); |
51
|
|
|
}); |
52
|
|
|
return $deferred->promise(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getHydrator(): Hydrator |
56
|
|
|
{ |
57
|
|
|
return $this->hydrator; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function createRequest(string $method, string $path) |
61
|
|
|
{ |
62
|
|
|
$url = self::API_SCHEMA . '://' . self::API_HOST . '/' . $path; |
63
|
|
|
return new Request($method, $url, [ |
64
|
|
|
'User-Agent' => self::USER_AGENT, |
65
|
|
|
'Accept' => self::API_VERSION, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return LoopInterface |
71
|
|
|
*/ |
72
|
|
|
public function getLoop() |
73
|
|
|
{ |
74
|
|
|
return $this->loop; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|