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