|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ekinhbayar\GitAmp\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Amp\Http\Client\DelegateHttpClient; |
|
6
|
|
|
use Amp\Http\Client\Request; |
|
7
|
|
|
use Amp\Http\Client\Response; |
|
8
|
|
|
use Amp\NullCancellationToken; |
|
9
|
|
|
use Amp\Promise; |
|
10
|
|
|
use ekinhbayar\GitAmp\Exception\RequestFailed; |
|
11
|
|
|
use ekinhbayar\GitAmp\Github\Credentials; |
|
12
|
|
|
use ekinhbayar\GitAmp\Response\Factory; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
use function Amp\call; |
|
15
|
|
|
|
|
16
|
|
|
class GitHub implements Listener |
|
17
|
|
|
{ |
|
18
|
|
|
private const EVENT_NAMESPACE = 'ekinhbayar\GitAmp\Event\GitHub'; |
|
19
|
|
|
|
|
20
|
|
|
private const API_ENDPOINT = 'https://api.github.com/events'; |
|
21
|
|
|
private const API_VERSION = 'v3'; |
|
22
|
|
|
|
|
23
|
|
|
private DelegateHttpClient $client; |
|
24
|
|
|
|
|
25
|
|
|
private Credentials $credentials; |
|
26
|
|
|
|
|
27
|
|
|
private Factory $resultFactory; |
|
28
|
|
|
|
|
29
|
|
|
private LoggerInterface $logger; |
|
30
|
|
|
|
|
31
|
5 |
|
public function __construct( |
|
32
|
|
|
DelegateHttpClient $client, |
|
33
|
|
|
Credentials $credentials, |
|
34
|
|
|
Factory $resultFactory, |
|
35
|
|
|
LoggerInterface $logger |
|
36
|
|
|
) { |
|
37
|
5 |
|
$this->client = $client; |
|
38
|
5 |
|
$this->credentials = $credentials; |
|
39
|
5 |
|
$this->resultFactory = $resultFactory; |
|
40
|
5 |
|
$this->logger = $logger; |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
private function request(): Promise |
|
44
|
|
|
{ |
|
45
|
5 |
|
return call(function () { |
|
46
|
|
|
try { |
|
47
|
5 |
|
$request = new Request(self::API_ENDPOINT, 'GET'); |
|
48
|
|
|
|
|
49
|
5 |
|
$request->setHeaders($this->getAuthHeader()); |
|
50
|
|
|
|
|
51
|
5 |
|
$response = yield $this->client->request($request, new NullCancellationToken()); |
|
52
|
1 |
|
} catch (\Throwable $e) { |
|
53
|
1 |
|
$this->logger->error('Failed to send GET request to API endpoint', ['exception' => $e]); |
|
54
|
|
|
|
|
55
|
1 |
|
throw new RequestFailed('Failed to send GET request to API endpoint', $e->getCode(), $e); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @var Response $result */ |
|
59
|
4 |
|
if ($response->getStatus() !== 200) { |
|
60
|
3 |
|
$message = \sprintf( |
|
61
|
|
|
'A non-200 response status (%s - %s) was encountered', |
|
62
|
3 |
|
$response->getStatus(), |
|
63
|
3 |
|
$response->getReason(), |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
3 |
|
$this->logger->critical($message, ['response' => $response]); |
|
67
|
|
|
|
|
68
|
3 |
|
throw new RequestFailed($message); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return $response; |
|
72
|
5 |
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
public function listen(): Promise |
|
76
|
|
|
{ |
|
77
|
5 |
|
return call(function () { |
|
78
|
5 |
|
$response = yield $this->request(); |
|
79
|
|
|
|
|
80
|
1 |
|
return yield $this->resultFactory->buildFromResponse(self::EVENT_NAMESPACE, $response); |
|
81
|
5 |
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
private function getAuthHeader(): array |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
5 |
|
'Accept' => \sprintf('application/vnd.github.%s+json', self::API_VERSION), |
|
88
|
5 |
|
'Authorization' => \sprintf('Bearer %s', $this->credentials->getAuthenticationString()), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|