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,10 @@ public function __construct(string $token, LoopInterface $loop, LoggerInterface
141148 $ this ->logger = $ logger ;
142149 $ this ->driver = $ driver ;
143150 $ this ->queue = new SplQueue ;
151+
152+ if (str_starts_with (InstalledVersions::getVersion ('react/promise ' ), '0.3. ' )) {
153+ $ this ->promiseV3 = true ;
154+ }
144155 }
145156
146157 /**
@@ -160,9 +171,9 @@ public function setDriver(DriverInterface $driver): void
160171 * @param mixed $content
161172 * @param array $headers
162173 *
163- * @return ExtendedPromiseInterface
174+ * @return PromiseInterface
164175 */
165- public function get ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
176+ public function get ($ url , $ content = null , array $ headers = []): PromiseInterface
166177 {
167178 if (! ($ url instanceof Endpoint)) {
168179 $ url = Endpoint::bind ($ url );
@@ -178,9 +189,9 @@ public function get($url, $content = null, array $headers = []): ExtendedPromise
178189 * @param mixed $content
179190 * @param array $headers
180191 *
181- * @return ExtendedPromiseInterface
192+ * @return PromiseInterface
182193 */
183- public function post ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
194+ public function post ($ url , $ content = null , array $ headers = []): PromiseInterface
184195 {
185196 if (! ($ url instanceof Endpoint)) {
186197 $ url = Endpoint::bind ($ url );
@@ -196,9 +207,9 @@ public function post($url, $content = null, array $headers = []): ExtendedPromis
196207 * @param mixed $content
197208 * @param array $headers
198209 *
199- * @return ExtendedPromiseInterface
210+ * @return PromiseInterface
200211 */
201- public function put ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
212+ public function put ($ url , $ content = null , array $ headers = []): PromiseInterface
202213 {
203214 if (! ($ url instanceof Endpoint)) {
204215 $ url = Endpoint::bind ($ url );
@@ -214,9 +225,9 @@ public function put($url, $content = null, array $headers = []): ExtendedPromise
214225 * @param mixed $content
215226 * @param array $headers
216227 *
217- * @return ExtendedPromiseInterface
228+ * @return PromiseInterface
218229 */
219- public function patch ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
230+ public function patch ($ url , $ content = null , array $ headers = []): PromiseInterface
220231 {
221232 if (! ($ url instanceof Endpoint)) {
222233 $ url = Endpoint::bind ($ url );
@@ -232,9 +243,9 @@ public function patch($url, $content = null, array $headers = []): ExtendedPromi
232243 * @param mixed $content
233244 * @param array $headers
234245 *
235- * @return ExtendedPromiseInterface
246+ * @return PromiseInterface
236247 */
237- public function delete ($ url , $ content = null , array $ headers = []): ExtendedPromiseInterface
248+ public function delete ($ url , $ content = null , array $ headers = []): PromiseInterface
238249 {
239250 if (! ($ url instanceof Endpoint)) {
240251 $ url = Endpoint::bind ($ url );
@@ -251,9 +262,9 @@ public function delete($url, $content = null, array $headers = []): ExtendedProm
251262 * @param mixed $content
252263 * @param array $headers
253264 *
254- * @return ExtendedPromiseInterface
265+ * @return PromiseInterface
255266 */
256- public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): ExtendedPromiseInterface
267+ public function queueRequest (string $ method , Endpoint $ url , $ content , array $ headers = []): PromiseInterface
257268 {
258269 $ deferred = new Deferred ();
259270
@@ -318,9 +329,9 @@ protected function guessContent(&$content)
318329 * @param Request $request
319330 * @param Deferred $deferred
320331 *
321- * @return ExtendedPromiseInterface
332+ * @return PromiseInterface
322333 */
323- protected function executeRequest (Request $ request , Deferred $ deferred = null ): ExtendedPromiseInterface
334+ protected function executeRequest (Request $ request , Deferred $ deferred = null ): PromiseInterface
324335 {
325336 if ($ deferred === null ) {
326337 $ deferred = new Deferred ();
@@ -332,7 +343,8 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
332343 return $ deferred ->promise ();
333344 }
334345
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 ) {
336348 $ data = json_decode ((string ) $ response ->getBody ());
337349 $ statusCode = $ response ->getStatusCode ();
338350
0 commit comments