11
11
12
12
namespace Discord \Http ;
13
13
14
+ use Composer \InstalledVersions ;
14
15
use Discord \Http \Exceptions \BadRequestException ;
15
16
use Discord \Http \Exceptions \ContentTooLongException ;
16
17
use Discord \Http \Exceptions \InvalidTokenException ;
24
25
use Psr \Log \LoggerInterface ;
25
26
use React \EventLoop \LoopInterface ;
26
27
use React \Promise \Deferred ;
27
- use React \Promise \ExtendedPromiseInterface ;
28
+ use React \Promise \PromiseInterface ;
28
29
use SplQueue ;
29
30
30
31
/**
@@ -127,6 +128,11 @@ class Http
127
128
*/
128
129
protected $ waiting = 0 ;
129
130
131
+ /**
132
+ * Whether react/promise v3 is used, if false, using v2
133
+ */
134
+ protected $ promiseV3 = true ;
135
+
130
136
/**
131
137
* Http wrapper constructor.
132
138
*
@@ -141,6 +147,10 @@ public function __construct(string $token, LoopInterface $loop, LoggerInterface
141
147
$ this ->logger = $ logger ;
142
148
$ this ->driver = $ driver ;
143
149
$ this ->queue = new SplQueue ;
150
+
151
+ if (str_starts_with (InstalledVersions::getVersion ('react/promise ' ), '0.3. ' )) {
152
+ $ this ->promiseV3 = true ;
153
+ }
144
154
}
145
155
146
156
/**
@@ -160,9 +170,9 @@ public function setDriver(DriverInterface $driver): void
160
170
* @param mixed $content
161
171
* @param array $headers
162
172
*
163
- * @return ExtendedPromiseInterface
173
+ * @return PromiseInterface
164
174
*/
165
- public function get ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
175
+ public function get ($ url , $ content = null , array $ headers = []): PromiseInterface
166
176
{
167
177
if (! ($ url instanceof Endpoint)) {
168
178
$ url = Endpoint::bind ($ url );
@@ -178,9 +188,9 @@ public function get($url, $content = null, array $headers = []): ExtendedPromise
178
188
* @param mixed $content
179
189
* @param array $headers
180
190
*
181
- * @return ExtendedPromiseInterface
191
+ * @return PromiseInterface
182
192
*/
183
- public function post ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
193
+ public function post ($ url , $ content = null , array $ headers = []): PromiseInterface
184
194
{
185
195
if (! ($ url instanceof Endpoint)) {
186
196
$ url = Endpoint::bind ($ url );
@@ -196,9 +206,9 @@ public function post($url, $content = null, array $headers = []): ExtendedPromis
196
206
* @param mixed $content
197
207
* @param array $headers
198
208
*
199
- * @return ExtendedPromiseInterface
209
+ * @return PromiseInterface
200
210
*/
201
- public function put ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
211
+ public function put ($ url , $ content = null , array $ headers = []): PromiseInterface
202
212
{
203
213
if (! ($ url instanceof Endpoint)) {
204
214
$ url = Endpoint::bind ($ url );
@@ -214,9 +224,9 @@ public function put($url, $content = null, array $headers = []): ExtendedPromise
214
224
* @param mixed $content
215
225
* @param array $headers
216
226
*
217
- * @return ExtendedPromiseInterface
227
+ * @return PromiseInterface
218
228
*/
219
- public function patch ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
229
+ public function patch ($ url , $ content = null , array $ headers = []): PromiseInterface
220
230
{
221
231
if (! ($ url instanceof Endpoint)) {
222
232
$ url = Endpoint::bind ($ url );
@@ -232,9 +242,9 @@ public function patch($url, $content = null, array $headers = []): ExtendedPromi
232
242
* @param mixed $content
233
243
* @param array $headers
234
244
*
235
- * @return ExtendedPromiseInterface
245
+ * @return PromiseInterface
236
246
*/
237
- public function delete ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
247
+ public function delete ($ url , $ content = null , array $ headers = []): PromiseInterface
238
248
{
239
249
if (! ($ url instanceof Endpoint)) {
240
250
$ url = Endpoint::bind ($ url );
@@ -251,9 +261,9 @@ public function delete($url, $content = null, array $headers = []): ExtendedProm
251
261
* @param mixed $content
252
262
* @param array $headers
253
263
*
254
- * @return ExtendedPromiseInterface
264
+ * @return PromiseInterface
255
265
*/
256
- public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): ExtendedPromiseInterface
266
+ public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): PromiseInterface
257
267
{
258
268
$ deferred = new Deferred ();
259
269
@@ -318,21 +328,21 @@ protected function guessContent(&$content)
318
328
* @param Request $request
319
329
* @param Deferred $deferred
320
330
*
321
- * @return ExtendedPromiseInterface
331
+ * @return PromiseInterface
322
332
*/
323
- protected function executeRequest (Request $ request , Deferred $ deferred = null ): ExtendedPromiseInterface
333
+ protected function executeRequest (Request $ request , Deferred $ deferred = null ): PromiseInterface
324
334
{
325
335
if ($ deferred === null ) {
326
336
$ deferred = new Deferred ();
327
337
}
328
338
329
339
if ($ this ->rateLimit ) {
330
- $ deferred ->reject ($ this ->rateLimit );
340
+ $ deferred ->reject (new RateLimitException ( $ this ->rateLimit ) );
331
341
332
342
return $ deferred ->promise ();
333
343
}
334
344
335
- $ this ->driver ->runRequest ($ request )->done (function (ResponseInterface $ response ) use ($ request , $ deferred ) {
345
+ $ this ->driver ->runRequest ($ request )->{ $ this -> promiseV3 ? ' then ' : ' done ' } (function (ResponseInterface $ response ) use ($ request , $ deferred ) {
336
346
$ data = json_decode ((string ) $ response ->getBody ());
337
347
$ statusCode = $ response ->getStatusCode ();
338
348
@@ -383,7 +393,7 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
383
393
});
384
394
}
385
395
386
- $ deferred ->reject ($ rateLimit ->isGlobal () ? $ this ->rateLimit : $ rateLimit );
396
+ $ deferred ->reject (new RateLimitException ( $ rateLimit ->isGlobal () ? $ this ->rateLimit : $ rateLimit) );
387
397
}
388
398
// Bad Gateway
389
399
// Cloudflare SSL Handshake error
0 commit comments