1111
1212namespace Discord \Http ;
1313
14+ use Composer \InstalledVersions ;
1415use Discord \Http \Exceptions \BadRequestException ;
1516use Discord \Http \Exceptions \ContentTooLongException ;
1617use Discord \Http \Exceptions \InvalidTokenException ;
2425use Psr \Log \LoggerInterface ;
2526use React \EventLoop \LoopInterface ;
2627use React \Promise \Deferred ;
27- use React \Promise \ExtendedPromiseInterface ;
28+ use React \Promise \PromiseInterface ;
2829use SplQueue ;
2930
3031/**
@@ -127,6 +128,12 @@ class Http
127128 */
128129 protected $ waiting = 0 ;
129130
131+
132+ /**
133+ * Whether react/promise v3 is used, if false, using v2
134+ */
135+ protected $ promiseV3 = true ;
136+
130137 /**
131138 * Http wrapper constructor.
132139 *
@@ -141,6 +148,8 @@ public function __construct(string $token, LoopInterface $loop, LoggerInterface
141148 $ this ->logger = $ logger ;
142149 $ this ->driver = $ driver ;
143150 $ this ->queue = new SplQueue ;
151+
152+ $ this ->promiseV3 = str_starts_with (InstalledVersions::getVersion ('react/promise ' ), '0.3. ' );
144153 }
145154
146155 /**
@@ -160,9 +169,9 @@ public function setDriver(DriverInterface $driver): void
160169 * @param mixed $content
161170 * @param array $headers
162171 *
163- * @return ExtendedPromiseInterface
172+ * @return PromiseInterface
164173 */
165- public function get ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
174+ public function get ($ url , $ content = null , array $ headers = []): PromiseInterface
166175 {
167176 if (! ($ url instanceof Endpoint)) {
168177 $ url = Endpoint::bind ($ url );
@@ -178,9 +187,9 @@ public function get($url, $content = null, array $headers = []): ExtendedPromise
178187 * @param mixed $content
179188 * @param array $headers
180189 *
181- * @return ExtendedPromiseInterface
190+ * @return PromiseInterface
182191 */
183- public function post ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
192+ public function post ($ url , $ content = null , array $ headers = []): PromiseInterface
184193 {
185194 if (! ($ url instanceof Endpoint)) {
186195 $ url = Endpoint::bind ($ url );
@@ -196,9 +205,9 @@ public function post($url, $content = null, array $headers = []): ExtendedPromis
196205 * @param mixed $content
197206 * @param array $headers
198207 *
199- * @return ExtendedPromiseInterface
208+ * @return PromiseInterface
200209 */
201- public function put ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
210+ public function put ($ url , $ content = null , array $ headers = []): PromiseInterface
202211 {
203212 if (! ($ url instanceof Endpoint)) {
204213 $ url = Endpoint::bind ($ url );
@@ -214,9 +223,9 @@ public function put($url, $content = null, array $headers = []): ExtendedPromise
214223 * @param mixed $content
215224 * @param array $headers
216225 *
217- * @return ExtendedPromiseInterface
226+ * @return PromiseInterface
218227 */
219- public function patch ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
228+ public function patch ($ url , $ content = null , array $ headers = []): PromiseInterface
220229 {
221230 if (! ($ url instanceof Endpoint)) {
222231 $ url = Endpoint::bind ($ url );
@@ -232,9 +241,9 @@ public function patch($url, $content = null, array $headers = []): ExtendedPromi
232241 * @param mixed $content
233242 * @param array $headers
234243 *
235- * @return ExtendedPromiseInterface
244+ * @return PromiseInterface
236245 */
237- public function delete ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
246+ public function delete ($ url , $ content = null , array $ headers = []): PromiseInterface
238247 {
239248 if (! ($ url instanceof Endpoint)) {
240249 $ url = Endpoint::bind ($ url );
@@ -251,9 +260,9 @@ public function delete($url, $content = null, array $headers = []): ExtendedProm
251260 * @param mixed $content
252261 * @param array $headers
253262 *
254- * @return ExtendedPromiseInterface
263+ * @return PromiseInterface
255264 */
256- public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): ExtendedPromiseInterface
265+ public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): PromiseInterface
257266 {
258267 $ deferred = new Deferred ();
259268
@@ -318,9 +327,9 @@ protected function guessContent(&$content)
318327 * @param Request $request
319328 * @param Deferred $deferred
320329 *
321- * @return ExtendedPromiseInterface
330+ * @return PromiseInterface
322331 */
323- protected function executeRequest (Request $ request , Deferred $ deferred = null ): ExtendedPromiseInterface
332+ protected function executeRequest (Request $ request , Deferred $ deferred = null ): PromiseInterface
324333 {
325334 if ($ deferred === null ) {
326335 $ deferred = new Deferred ();
@@ -332,7 +341,8 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
332341 return $ deferred ->promise ();
333342 }
334343
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 ) {
336346 $ data = json_decode ((string ) $ response ->getBody ());
337347 $ statusCode = $ response ->getStatusCode ();
338348
0 commit comments