1 | <?php |
||
17 | class HttpClient implements HttpClientInterface |
||
18 | { |
||
19 | private $accountId; |
||
20 | private $secretKey; |
||
21 | private $test; |
||
22 | private $client; |
||
23 | private $logger; |
||
24 | |||
25 | public function __construct( |
||
26 | string $accountId, |
||
27 | string $secretKey, |
||
28 | string $baseUri, |
||
29 | bool $test, |
||
30 | \Symfony\Contracts\HttpClient\HttpClientInterface $client, |
||
31 | ?LoggerInterface $logger = null |
||
32 | ) { |
||
33 | $this->accountId = $accountId; |
||
34 | $this->secretKey = $secretKey; |
||
35 | $this->test = $test; |
||
36 | $this->client = ScopingHttpClient::forBaseUri($client, $baseUri); |
||
37 | $this->logger = $logger ?? new NullLogger(); |
||
38 | } |
||
39 | |||
40 | public function request(string $api, string $method, array $arguments = []): ResponseInterface |
||
41 | { |
||
42 | $this->logger->info('[wanna-speak] Requesting WannaSpeak API {api} with method {method}.', [ |
||
43 | 'api' => $api, |
||
44 | 'method' => $method, |
||
45 | 'arguments' => $arguments, |
||
46 | ]); |
||
47 | |||
48 | if ($this->test) { |
||
49 | throw new TestModeException(); |
||
50 | } |
||
51 | |||
52 | $response = $this->doRequest($api, $method, $arguments); |
||
53 | |||
54 | $this->handleResponse($response); |
||
55 | |||
56 | return $response; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array<string,mixed> $additionalArguments Additional WannaSpeak request arguments |
||
61 | * |
||
62 | * @throws WannaSpeakApiExceptionInterface |
||
63 | * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
||
64 | * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface |
||
65 | * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
||
66 | * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
||
67 | * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
||
68 | */ |
||
69 | private function doRequest(string $api, string $method, array $additionalArguments = []): ResponseInterface |
||
104 | |||
105 | private function getAuthKey(): string |
||
111 | |||
112 | /** |
||
113 | * @throws WannaSpeakApiExceptionInterface |
||
114 | * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
||
115 | * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface |
||
116 | * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
||
117 | * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
||
118 | * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
||
119 | */ |
||
120 | private function handleResponse(ResponseInterface $response): void |
||
146 | } |
||
147 |