1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI; |
6
|
|
|
|
7
|
|
|
use React\EventLoop\LoopInterface; |
8
|
|
|
use React\HttpClient\Client; |
9
|
|
|
use React\HttpClient\Request; |
10
|
|
|
use React\HttpClient\Response; |
11
|
|
|
use React\Promise\Deferred; |
12
|
|
|
use React\Promise\PromiseInterface; |
13
|
|
|
use unreal4u\TelegramAPI\Exceptions\ClientException; |
14
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse; |
15
|
|
|
|
16
|
|
|
class HttpClientRequestHandlerReact implements RequestHandlerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HttpClientRequestHandler constructor. |
25
|
|
|
* @param LoopInterface $loop |
26
|
|
|
*/ |
27
|
|
|
public function __construct(LoopInterface $loop) |
28
|
|
|
{ |
29
|
|
|
$this->client = new Client($loop); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $uri |
34
|
|
|
* @return PromiseInterface |
35
|
|
|
*/ |
36
|
|
|
public function get(string $uri): PromiseInterface |
37
|
|
|
{ |
38
|
|
|
$request = $this->client->request('GET', $uri); |
39
|
|
|
return $this->processRequest($request); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $uri |
44
|
|
|
* @param array $options |
45
|
|
|
* @return PromiseInterface |
46
|
|
|
*/ |
47
|
|
|
public function post(string $uri, array $options): PromiseInterface |
48
|
|
|
{ |
49
|
|
|
$headers = !empty($options['headers']) ? $options['headers'] : []; |
50
|
|
|
$request = $this->client->request('POST', $uri, $headers); |
51
|
|
|
return $this->processRequest($request, (!empty($options['body']) ? $options['body'] : null)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param mixed $data |
57
|
|
|
* @return PromiseInterface |
58
|
|
|
*/ |
59
|
|
|
private function processRequest(Request $request, $data = null): PromiseInterface |
60
|
|
|
{ |
61
|
|
|
$deferred = new Deferred(); |
62
|
|
|
|
63
|
|
|
$receivedData = ''; |
64
|
|
|
$request->on('response', function (Response $response) use ($deferred, &$receivedData) { |
65
|
|
|
$response->on('data', function ($chunk) use (&$receivedData) { |
66
|
|
|
$receivedData .= $chunk; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$response->on('end', function () use (&$receivedData, $deferred, $response) { |
70
|
|
|
try { |
71
|
|
|
$endResponse = new TelegramResponse($receivedData, $response->getHeaders()); |
72
|
|
|
$deferred->resolve($endResponse); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
// Capture any exceptions thrown from TelegramResponse and reject the response |
75
|
|
|
$deferred->reject($e); |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$request->on('error', function (\Exception $e) use ($deferred) { |
81
|
|
|
$deferred->reject(new ClientException($e->getMessage(), $e->getCode(), $e)); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$request->end($data); |
85
|
|
|
|
86
|
|
|
return $deferred->promise(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|