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