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,12 @@ class Http
127
128
*/
128
129
protected $ waiting = 0 ;
129
130
131
+
132
+ /**
133
+ * Whether react/promise v3 is used, if false, using v2
134
+ */
135
+ protected $ promiseV3 = true ;
136
+
130
137
/**
131
138
* Http wrapper constructor.
132
139
*
@@ -141,6 +148,10 @@ public function __construct(string $token, LoopInterface $loop, LoggerInterface
141
148
$ this ->logger = $ logger ;
142
149
$ this ->driver = $ driver ;
143
150
$ this ->queue = new SplQueue ;
151
+
152
+ if (str_starts_with (InstalledVersions::getVersion ('react/promise ' ), '0.3. ' )) {
153
+ $ this ->promiseV3 = true ;
154
+ }
144
155
}
145
156
146
157
/**
@@ -160,9 +171,9 @@ public function setDriver(DriverInterface $driver): void
160
171
* @param mixed $content
161
172
* @param array $headers
162
173
*
163
- * @return ExtendedPromiseInterface
174
+ * @return PromiseInterface
164
175
*/
165
- public function get ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
176
+ public function get ($ url , $ content = null , array $ headers = []): PromiseInterface
166
177
{
167
178
if (! ($ url instanceof Endpoint)) {
168
179
$ url = Endpoint::bind ($ url );
@@ -178,9 +189,9 @@ public function get($url, $content = null, array $headers = []): ExtendedPromise
178
189
* @param mixed $content
179
190
* @param array $headers
180
191
*
181
- * @return ExtendedPromiseInterface
192
+ * @return PromiseInterface
182
193
*/
183
- public function post ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
194
+ public function post ($ url , $ content = null , array $ headers = []): PromiseInterface
184
195
{
185
196
if (! ($ url instanceof Endpoint)) {
186
197
$ url = Endpoint::bind ($ url );
@@ -196,9 +207,9 @@ public function post($url, $content = null, array $headers = []): ExtendedPromis
196
207
* @param mixed $content
197
208
* @param array $headers
198
209
*
199
- * @return ExtendedPromiseInterface
210
+ * @return PromiseInterface
200
211
*/
201
- public function put ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
212
+ public function put ($ url , $ content = null , array $ headers = []): PromiseInterface
202
213
{
203
214
if (! ($ url instanceof Endpoint)) {
204
215
$ url = Endpoint::bind ($ url );
@@ -214,9 +225,9 @@ public function put($url, $content = null, array $headers = []): ExtendedPromise
214
225
* @param mixed $content
215
226
* @param array $headers
216
227
*
217
- * @return ExtendedPromiseInterface
228
+ * @return PromiseInterface
218
229
*/
219
- public function patch ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
230
+ public function patch ($ url , $ content = null , array $ headers = []): PromiseInterface
220
231
{
221
232
if (! ($ url instanceof Endpoint)) {
222
233
$ url = Endpoint::bind ($ url );
@@ -232,9 +243,9 @@ public function patch($url, $content = null, array $headers = []): ExtendedPromi
232
243
* @param mixed $content
233
244
* @param array $headers
234
245
*
235
- * @return ExtendedPromiseInterface
246
+ * @return PromiseInterface
236
247
*/
237
- public function delete ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
248
+ public function delete ($ url , $ content = null , array $ headers = []): PromiseInterface
238
249
{
239
250
if (! ($ url instanceof Endpoint)) {
240
251
$ url = Endpoint::bind ($ url );
@@ -251,9 +262,9 @@ public function delete($url, $content = null, array $headers = []): ExtendedProm
251
262
* @param mixed $content
252
263
* @param array $headers
253
264
*
254
- * @return ExtendedPromiseInterface
265
+ * @return PromiseInterface
255
266
*/
256
- public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): ExtendedPromiseInterface
267
+ public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): PromiseInterface
257
268
{
258
269
$ deferred = new Deferred ();
259
270
@@ -318,9 +329,9 @@ protected function guessContent(&$content)
318
329
* @param Request $request
319
330
* @param Deferred $deferred
320
331
*
321
- * @return ExtendedPromiseInterface
332
+ * @return PromiseInterface
322
333
*/
323
- protected function executeRequest (Request $ request , Deferred $ deferred = null ): ExtendedPromiseInterface
334
+ protected function executeRequest (Request $ request , Deferred $ deferred = null ): PromiseInterface
324
335
{
325
336
if ($ deferred === null ) {
326
337
$ deferred = new Deferred ();
@@ -332,7 +343,8 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
332
343
return $ deferred ->promise ();
333
344
}
334
345
335
- $ this ->driver ->runRequest ($ request )->done (function (ResponseInterface $ response ) use ($ request , $ deferred ) {
346
+ // Promises v3 changed `->then` to behave as `->done` and removed `->then`. We still need the behaviour of `->done` in projects using v2
347
+ $ this ->driver ->runRequest ($ request )->{$ this ->promiseV3 ? 'then ' : 'done ' }(function (ResponseInterface $ response ) use ($ request , $ deferred ) {
336
348
$ data = json_decode ((string ) $ response ->getBody ());
337
349
$ statusCode = $ response ->getStatusCode ();
338
350
0 commit comments