1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterApi package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace Gitter\Adapters; |
9
|
|
|
|
10
|
|
|
use Gitter\Client; |
11
|
|
|
use Gitter\Route; |
12
|
|
|
use GuzzleHttp\Client as Guzzle; |
13
|
|
|
use Monolog\Logger; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class HttpAdapter |
18
|
|
|
* @package Gitter\Adapters |
19
|
|
|
*/ |
20
|
|
|
class HttpAdapter extends AbstractClient implements SyncAdapterInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Guzzle |
24
|
|
|
*/ |
25
|
|
|
private $guzzle; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Client |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HttpAdapter constructor. |
34
|
|
|
* @param Client $client |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Client $client) |
37
|
|
|
{ |
38
|
|
|
$this->client = $client; |
39
|
|
|
$this->setOptions(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $options |
44
|
|
|
* @return AdapterInterface |
45
|
|
|
*/ |
46
|
|
|
public function setOptions(array $options = []): AdapterInterface |
47
|
|
|
{ |
48
|
|
|
parent::setOptions($this->injectToken($this->client, $options)); |
49
|
|
|
|
50
|
|
|
$this->guzzle = new Guzzle($this->options); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Client $client |
57
|
|
|
* @param array $options |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
private function injectToken(Client $client, array $options) |
61
|
|
|
{ |
62
|
|
|
$options['headers'] = array_merge( |
63
|
|
|
$options['headers'] ?? [], |
64
|
|
|
$this->buildHeaders($client) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $options; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Route $route |
72
|
|
|
* @return array |
73
|
|
|
* @throws \RuntimeException |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
public function request(Route $route): array |
77
|
|
|
{ |
78
|
|
|
list($method, $uri) = [$route->method(), $route->build()]; |
79
|
|
|
$options = $this->prepareRequestOptions($route); |
80
|
|
|
|
81
|
|
|
// Log request |
82
|
|
|
$this->client->log(' -> ' . $method . ' ' . $uri . "\n body: " . ($options['body'] ?? ''), Logger::DEBUG); |
83
|
|
|
|
84
|
|
|
$response = $this->guzzle->request($method, $uri, $options); |
85
|
|
|
|
86
|
|
|
// Log response |
87
|
|
|
$this->client->log(' <- ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase(), Logger::DEBUG); |
88
|
|
|
$this->client->log(' <- ' . (string)$response->getBody(), Logger::DEBUG); |
89
|
|
|
|
90
|
|
|
return $this->parseResponse($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Route $route |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
private function prepareRequestOptions(Route $route): array |
98
|
|
|
{ |
99
|
|
|
$options = []; |
100
|
|
|
|
101
|
|
|
if ($route->method() !== 'GET' && $route->getBody() !== null) { |
102
|
|
|
$options['body'] = $route->getBody(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $options; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param ResponseInterface $response |
110
|
|
|
* @return array |
111
|
|
|
* @throws \RuntimeException |
112
|
|
|
*/ |
113
|
|
|
private function parseResponse(ResponseInterface $response): array |
114
|
|
|
{ |
115
|
|
|
$data = json_decode((string)$response->getBody(), true); |
116
|
|
|
|
117
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
118
|
|
|
throw new \RuntimeException(json_last_error_msg()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $data; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|