|
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\InternalFunctionality\TelegramResponse; |
|
14
|
|
|
|
|
15
|
|
|
class HttpClientRequestHandler implements RequestHandlerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Client |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* HttpClientRequestHandler constructor. |
|
24
|
|
|
* @param LoopInterface $loop |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(LoopInterface $loop) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->client = new Client($loop); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $uri |
|
33
|
|
|
* @return PromiseInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
public function get(string $uri): PromiseInterface |
|
36
|
|
|
{ |
|
37
|
|
|
$request = $this->client->request('GET', $uri); |
|
38
|
|
|
return $this->processRequest($request); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $uri |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
* @return PromiseInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
public function post(string $uri, array $options): PromiseInterface |
|
47
|
|
|
{ |
|
48
|
|
|
$headers = !empty($options['headers']) ? $options['headers'] : []; |
|
49
|
|
|
$request = $this->client->request('POST', $uri, $headers); |
|
50
|
|
|
return $this->processRequest($request, (!empty($options['body']) ? $options['body'] : null)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param Request $request |
|
55
|
|
|
* @param mixed $data |
|
56
|
|
|
* @return PromiseInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function processRequest(Request $request, $data = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$deferred = new Deferred(); |
|
61
|
|
|
|
|
62
|
|
|
$receivedData = ''; |
|
63
|
|
|
$request->on('response', function (Response $response) use ($deferred, &$receivedData) { |
|
64
|
|
|
$response->on('data', function ($chunk) use (&$receivedData) { |
|
65
|
|
|
$receivedData .= $chunk; |
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
$response->on('end', function () use (&$receivedData, $deferred, $response) { |
|
69
|
|
|
$deferred->resolve(new TelegramResponse($receivedData, $response->getHeaders())); |
|
70
|
|
|
}); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
$request->on('error', function (\Exception $exception) use ($deferred, $receivedData) { |
|
74
|
|
|
// First check if the data we received thus far actually is valid. |
|
75
|
|
|
// Else, wipe it. |
|
76
|
|
|
if (!json_decode($receivedData)) { |
|
77
|
|
|
$receivedData = ''; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$deferred->reject(new TelegramResponse($receivedData, [], $exception)); |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
if (!empty($data)) { |
|
84
|
|
|
$request->write($data); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $deferred->promise(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope