1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\ClientInterface; |
10
|
|
|
use GuzzleHttp\Exception\ClientException; |
11
|
|
|
use GuzzleHttp\Exception\RequestException; |
12
|
|
|
use React\Promise\PromiseInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use React\Promise\Deferred; |
16
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\DummyLogger; |
17
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData; |
18
|
|
|
|
19
|
|
|
class GuzzleRequestHandler implements RequestHandlerInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var LoggerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $logger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $httpClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* GuzzleRequestHandler constructor. |
33
|
|
|
* |
34
|
|
|
* @param ClientInterface $client |
35
|
|
|
* @param LoggerInterface $logger |
36
|
|
|
*/ |
37
|
16 |
|
public function __construct(?ClientInterface $client = null, LoggerInterface $logger = null) |
38
|
|
|
{ |
39
|
16 |
|
if ($logger === null) |
40
|
|
|
{ |
41
|
|
|
$logger = new DummyLogger(); |
42
|
|
|
} |
43
|
16 |
|
$this->logger = $logger; |
44
|
|
|
|
45
|
16 |
|
if ($client === null) |
46
|
|
|
{ |
47
|
16 |
|
$client = new Client(); |
48
|
|
|
} |
49
|
16 |
|
$this->httpClient = $client; |
50
|
16 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $uri |
54
|
|
|
* |
55
|
|
|
* @return ResponseInterface |
56
|
|
|
*/ |
57
|
|
|
public function get(string $uri): ResponseInterface |
58
|
|
|
{ |
59
|
|
|
return $this->httpClient->get($uri); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
64
|
|
|
* |
65
|
|
|
* @param string $uri |
66
|
|
|
* @param array $formData |
67
|
|
|
* |
68
|
|
|
* @return TelegramRawData |
69
|
|
|
*/ |
70
|
|
|
public function post(string $uri, array $formData = []): TelegramRawData |
71
|
|
|
{ |
72
|
|
|
$e = null; |
73
|
|
|
$this->logger->debug('About to perform HTTP call to Telegram\'s API'); |
74
|
|
|
try { |
75
|
|
|
$response = $this->httpClient->post($uri, $formData); |
76
|
|
|
$this->logger->debug('Got response back from Telegram, applying json_decode'); |
77
|
|
|
} |
78
|
|
|
catch (ClientException $e) { |
79
|
|
|
$response = $e->getResponse(); |
80
|
|
|
// It can happen that we have a network problem, in such case, we can't do nothing about it, so rethrow |
81
|
|
|
if (empty($response)) { |
82
|
|
|
throw $e; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
finally { |
86
|
|
|
return new TelegramRawData((string) $response->getBody(), $e); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $uri |
92
|
|
|
* @param array $formData |
93
|
|
|
* |
94
|
|
|
* @return PromiseInterface |
95
|
|
|
*/ |
96
|
|
|
public function postAsync(string $uri, array $formData = []): PromiseInterface |
97
|
|
|
{ |
98
|
|
|
$this->logger->debug('About to perform async HTTP call to Telegram\'s API'); |
99
|
|
|
$deferred = new Deferred(); |
100
|
|
|
|
101
|
|
|
$promise = $this->httpClient->postAsync($uri, $formData); |
102
|
|
|
$promise->then(function (ResponseInterface $response) use ($deferred) { |
103
|
|
|
$deferred->resolve(new TelegramRawData((string) $response->getBody())); |
104
|
|
|
}, |
105
|
|
|
function (RequestException $exception) use ($deferred) { |
106
|
|
|
if (!empty($exception->getResponse()->getBody())) |
107
|
|
|
$deferred->resolve(new TelegramRawData((string) $exception->getResponse()->getBody(), $exception)); |
108
|
|
|
else |
109
|
|
|
$deferred->reject($exception); |
110
|
|
|
}); |
111
|
|
|
|
112
|
|
|
return $deferred->promise(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $uri |
117
|
|
|
* |
118
|
|
|
* @return PromiseInterface |
119
|
|
|
*/ |
120
|
|
|
public function getAsync(string $uri): PromiseInterface |
121
|
|
|
{ |
122
|
|
|
$this->logger->debug('About to perform async HTTP call to Telegram\'s API'); |
123
|
|
|
$deferred = new Deferred(); |
124
|
|
|
|
125
|
|
|
$promise = $this->httpClient->getAsync($uri); |
126
|
|
|
$promise->then(function (ResponseInterface $response) use ($deferred) { |
127
|
|
|
$deferred->resolve(new TelegramRawData((string) $response->getBody())); |
128
|
|
|
}, |
129
|
|
|
function (RequestException $exception) use ($deferred) { |
130
|
|
|
if (!empty($exception->getResponse()->getBody())) |
131
|
|
|
$deferred->resolve(new TelegramRawData((string) $exception->getResponse()->getBody(), $exception)); |
132
|
|
|
else |
133
|
|
|
$deferred->reject($exception); |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
return $deferred->promise(); |
137
|
|
|
} |
138
|
|
|
} |