1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telegram\Bot\HttpClients; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use GuzzleHttp\Promise; |
8
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Telegram\Bot\Exceptions\TelegramSDKException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class GuzzleHttpClient. |
15
|
|
|
*/ |
16
|
|
|
class GuzzleHttpClient implements HttpClientInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* HTTP client. |
20
|
|
|
* |
21
|
|
|
* @var Client |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PromiseInterface[] |
27
|
|
|
*/ |
28
|
|
|
private static $promises = []; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
protected $timeOut; |
32
|
|
|
|
33
|
84 |
|
/** @var int */ |
34
|
|
|
protected $connectTimeOut; |
35
|
84 |
|
|
36
|
84 |
|
/** |
37
|
|
|
* @param Client|null $client |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Client $client = null) |
40
|
|
|
{ |
41
|
42 |
|
$this->client = $client ?: new Client(); |
42
|
|
|
} |
43
|
42 |
|
|
44
|
42 |
|
/** |
45
|
|
|
* Unwrap Promises. |
46
|
|
|
*/ |
47
|
|
|
public function __destruct() |
48
|
|
|
{ |
49
|
|
|
Promise\unwrap(self::$promises); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets HTTP client. |
54
|
|
|
* |
55
|
|
|
* @param Client $client |
56
|
|
|
* |
57
|
|
|
* @return GuzzleHttpClient |
58
|
|
|
*/ |
59
|
|
|
public function setClient(Client $client) |
60
|
|
|
{ |
61
|
|
|
$this->client = $client; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
40 |
|
|
66
|
|
|
/** |
67
|
40 |
|
* Gets HTTP client for internal class use. |
68
|
|
|
* |
69
|
|
|
* @return Client |
70
|
|
|
*/ |
71
|
|
|
private function getClient() |
72
|
|
|
{ |
73
|
40 |
|
return $this->client; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function send( |
80
|
|
|
$url, |
81
|
40 |
|
$method, |
82
|
40 |
|
array $headers = [], |
83
|
|
|
array $options = [], |
84
|
|
|
$timeOut = 30, |
85
|
40 |
|
$isAsyncRequest = false, |
86
|
|
|
$connectTimeOut = 10 |
87
|
40 |
|
) { |
88
|
|
|
$this->timeOut = $timeOut; |
89
|
|
|
$this->connectTimeOut = $connectTimeOut; |
90
|
40 |
|
|
91
|
|
|
$body = isset($options['body']) ? $options['body'] : null; |
92
|
40 |
|
$options = $this->getOptions($headers, $body, $options, $timeOut, $isAsyncRequest, $connectTimeOut); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$response = $this->getClient()->requestAsync($method, $url, $options); |
96
|
|
|
|
97
|
|
|
if ($isAsyncRequest) { |
98
|
|
|
self::$promises[] = $response; |
99
|
|
|
} else { |
100
|
40 |
|
$response = $response->wait(); |
101
|
|
|
} |
102
|
|
|
} catch (RequestException $e) { |
103
|
|
|
$response = $e->getResponse(); |
104
|
|
|
|
105
|
|
|
if (!$response instanceof ResponseInterface) { |
106
|
|
|
throw new TelegramSDKException($e->getMessage(), $e->getCode()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $response; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
40 |
|
* Prepares and returns request options. |
115
|
|
|
* |
116
|
|
|
* @param array $headers |
117
|
40 |
|
* @param string $body |
118
|
40 |
|
* @param array $options |
119
|
40 |
|
* @param int $timeOut |
120
|
40 |
|
* @param bool $isAsyncRequest |
121
|
40 |
|
* @param int $connectTimeOut |
122
|
40 |
|
* |
123
|
|
|
* @return array |
124
|
40 |
|
*/ |
125
|
|
|
private function getOptions(array $headers, $body, $options, $timeOut, $isAsyncRequest = false, $connectTimeOut = 10) |
126
|
|
|
{ |
127
|
|
|
$default_options = [ |
128
|
|
|
RequestOptions::HEADERS => $headers, |
129
|
|
|
RequestOptions::BODY => $body, |
130
|
|
|
RequestOptions::TIMEOUT => $timeOut, |
131
|
|
|
RequestOptions::CONNECT_TIMEOUT => $connectTimeOut, |
132
|
|
|
RequestOptions::SYNCHRONOUS => !$isAsyncRequest, |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
return array_merge($default_options, $options); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return int|null |
140
|
|
|
*/ |
141
|
|
|
public function getTimeOut() |
142
|
|
|
{ |
143
|
|
|
return $this->timeOut; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return int|null |
148
|
|
|
*/ |
149
|
|
|
public function getConnectTimeOut() |
150
|
|
|
{ |
151
|
|
|
return $this->connectTimeOut; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|