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,8 @@ 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
+ $ this ->promiseV3 = str_starts_with (InstalledVersions::getVersion ('react/promise ' ), '0.3. ' );
144
153
}
145
154
146
155
/**
@@ -160,9 +169,9 @@ public function setDriver(DriverInterface $driver): void
160
169
* @param mixed $content
161
170
* @param array $headers
162
171
*
163
- * @return ExtendedPromiseInterface
172
+ * @return PromiseInterface
164
173
*/
165
- public function get ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
174
+ public function get ($ url , $ content = null , array $ headers = []): PromiseInterface
166
175
{
167
176
if (! ($ url instanceof Endpoint)) {
168
177
$ url = Endpoint::bind ($ url );
@@ -178,9 +187,9 @@ public function get($url, $content = null, array $headers = []): ExtendedPromise
178
187
* @param mixed $content
179
188
* @param array $headers
180
189
*
181
- * @return ExtendedPromiseInterface
190
+ * @return PromiseInterface
182
191
*/
183
- public function post ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
192
+ public function post ($ url , $ content = null , array $ headers = []): PromiseInterface
184
193
{
185
194
if (! ($ url instanceof Endpoint)) {
186
195
$ url = Endpoint::bind ($ url );
@@ -196,9 +205,9 @@ public function post($url, $content = null, array $headers = []): ExtendedPromis
196
205
* @param mixed $content
197
206
* @param array $headers
198
207
*
199
- * @return ExtendedPromiseInterface
208
+ * @return PromiseInterface
200
209
*/
201
- public function put ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
210
+ public function put ($ url , $ content = null , array $ headers = []): PromiseInterface
202
211
{
203
212
if (! ($ url instanceof Endpoint)) {
204
213
$ url = Endpoint::bind ($ url );
@@ -214,9 +223,9 @@ public function put($url, $content = null, array $headers = []): ExtendedPromise
214
223
* @param mixed $content
215
224
* @param array $headers
216
225
*
217
- * @return ExtendedPromiseInterface
226
+ * @return PromiseInterface
218
227
*/
219
- public function patch ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
228
+ public function patch ($ url , $ content = null , array $ headers = []): PromiseInterface
220
229
{
221
230
if (! ($ url instanceof Endpoint)) {
222
231
$ url = Endpoint::bind ($ url );
@@ -232,9 +241,9 @@ public function patch($url, $content = null, array $headers = []): ExtendedPromi
232
241
* @param mixed $content
233
242
* @param array $headers
234
243
*
235
- * @return ExtendedPromiseInterface
244
+ * @return PromiseInterface
236
245
*/
237
- public function delete ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
246
+ public function delete ($ url , $ content = null , array $ headers = []): PromiseInterface
238
247
{
239
248
if (! ($ url instanceof Endpoint)) {
240
249
$ url = Endpoint::bind ($ url );
@@ -251,9 +260,9 @@ public function delete($url, $content = null, array $headers = []): ExtendedProm
251
260
* @param mixed $content
252
261
* @param array $headers
253
262
*
254
- * @return ExtendedPromiseInterface
263
+ * @return PromiseInterface
255
264
*/
256
- public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): ExtendedPromiseInterface
265
+ public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): PromiseInterface
257
266
{
258
267
$ deferred = new Deferred ();
259
268
@@ -318,9 +327,9 @@ protected function guessContent(&$content)
318
327
* @param Request $request
319
328
* @param Deferred $deferred
320
329
*
321
- * @return ExtendedPromiseInterface
330
+ * @return PromiseInterface
322
331
*/
323
- protected function executeRequest (Request $ request , Deferred $ deferred = null ): ExtendedPromiseInterface
332
+ protected function executeRequest (Request $ request , Deferred $ deferred = null ): PromiseInterface
324
333
{
325
334
if ($ deferred === null ) {
326
335
$ deferred = new Deferred ();
@@ -332,7 +341,8 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
332
341
return $ deferred ->promise ();
333
342
}
334
343
335
- $ this ->driver ->runRequest ($ request )->done (function (ResponseInterface $ response ) use ($ request , $ deferred ) {
344
+ // Promises v3 changed `->then` to behave as `->done` and removed `->then`. We still need the behaviour of `->done` in projects using v2
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
0 commit comments