Skip to content

Commit ac7c7f3

Browse files
committed
Promise v3 heklper
Inspired by #27 by @Exanlv
1 parent 6186d74 commit ac7c7f3

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ All methods return the decoded JSON response in an object:
6262

6363
```php
6464
// https://discord.com/api/v8/oauth2/applications/@me
65-
$http->get('oauth2/applications/@me')->done(function ($response) {
65+
$http->get('oauth2/applications/@me')->then(function ($response) {
6666
var_dump($response);
6767
}, function ($e) {
6868
echo "Error: ".$e->getMessage().PHP_EOL;
@@ -76,7 +76,7 @@ e.g. `channels/:channel_id/messages/:message_id`. You can bind parameters to the
7676
// channels/channel_id_here/messages/message_id_here
7777
$endpoint = Endpoint::bind(Endpoint::CHANNEL_MESSAGE, 'channel_id_here', 'message_id_here');
7878

79-
$http->get($endpoint)->done(...);
79+
$http->get($endpoint)->then(...);
8080
```
8181

8282
It is recommended that if the endpoint contains parameters you use the `Endpoint::bind()` function to sort requests into their correct rate limit buckets.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^7.4|^8.0",
2020
"react/http": "^1.2",
2121
"psr/log": "^1.1 || ^2.0 || ^3.0",
22-
"react/promise": "^2.2"
22+
"react/promise": "^3.2 || ^2.2"
2323
},
2424
"suggest": {
2525
"guzzlehttp/guzzle": "For alternative to ReactPHP/Http Browser"

src/Discord/Bucket.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Discord\Http;
1313

14+
use Composer\InstalledVersions;
1415
use Psr\Http\Message\ResponseInterface;
1516
use Psr\Log\LoggerInterface;
1617
use React\EventLoop\LoopInterface;
@@ -87,6 +88,11 @@ class Bucket
8788
*/
8889
protected $resetTimer;
8990

91+
/**
92+
* Whether react/promise v3 is used, if false, using v2
93+
*/
94+
protected $promiseV3 = true;
95+
9096
/**
9197
* Bucket constructor.
9298
*
@@ -100,6 +106,10 @@ public function __construct(string $name, LoopInterface $loop, LoggerInterface $
100106
$this->loop = $loop;
101107
$this->logger = $logger;
102108
$this->runRequest = $runRequest;
109+
110+
if (str_starts_with(InstalledVersions::getVersion('react/promise'), '0.3.')) {
111+
$this->promiseV3 = true;
112+
}
103113
}
104114

105115
/**
@@ -149,7 +159,7 @@ public function checkQueue()
149159
/** @var Request */
150160
$request = $this->queue->dequeue();
151161

152-
($this->runRequest)($request)->done(function (ResponseInterface $response) use (&$checkQueue) {
162+
($this->runRequest)($request)->{$this->promiseV3 ? 'then' : 'done'}(function (ResponseInterface $response) use (&$checkQueue) {
153163
$resetAfter = (float) $response->getHeaderLine('X-Ratelimit-Reset-After');
154164
$limit = $response->getHeaderLine('X-Ratelimit-Limit');
155165
$remaining = $response->getHeaderLine('X-Ratelimit-Remaining');

src/Discord/DriverInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Discord\Http;
1313

1414
use Psr\Http\Message\ResponseInterface;
15-
use React\Promise\ExtendedPromiseInterface;
15+
use React\Promise\PromiseInterface;
1616

1717
/**
1818
* Interface for an HTTP driver.
@@ -28,7 +28,7 @@ interface DriverInterface
2828
*
2929
* @param Request $request
3030
*
31-
* @return ExtendedPromiseInterface<ResponseInterface>
31+
* @return PromiseInterface<ResponseInterface>
3232
*/
33-
public function runRequest(Request $request): ExtendedPromiseInterface;
33+
public function runRequest(Request $request): PromiseInterface;
3434
}

src/Discord/Drivers/Guzzle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use GuzzleHttp\RequestOptions;
1818
use React\EventLoop\LoopInterface;
1919
use React\Promise\Deferred;
20-
use React\Promise\ExtendedPromiseInterface;
20+
use React\Promise\PromiseInterface;
2121

2222
/**
2323
* guzzlehttp/guzzle driver for Discord HTTP client. (still with React Promise).
@@ -55,7 +55,7 @@ public function __construct(?LoopInterface $loop = null, array $options = [])
5555
$this->client = new Client($options);
5656
}
5757

58-
public function runRequest(Request $request): ExtendedPromiseInterface
58+
public function runRequest(Request $request): PromiseInterface
5959
{
6060
// Create a React promise
6161
$deferred = new Deferred();

src/Discord/Drivers/React.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Discord\Http\Request;
1616
use React\EventLoop\LoopInterface;
1717
use React\Http\Browser;
18-
use React\Promise\ExtendedPromiseInterface;
18+
use React\Promise\PromiseInterface;
1919
use React\Socket\Connector;
2020

2121
/**
@@ -54,7 +54,7 @@ public function __construct(LoopInterface $loop, array $options = [])
5454
$this->browser = $browser->withRejectErrorResponse(false);
5555
}
5656

57-
public function runRequest(Request $request): ExtendedPromiseInterface
57+
public function runRequest(Request $request): PromiseInterface
5858
{
5959
return $this->browser->{$request->getMethod()}(
6060
$request->getUrl(),

src/Discord/Http.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Discord\Http;
1313

14+
use Composer\InstalledVersions;
1415
use Discord\Http\Exceptions\BadRequestException;
1516
use Discord\Http\Exceptions\ContentTooLongException;
1617
use Discord\Http\Exceptions\InvalidTokenException;
@@ -24,7 +25,7 @@
2425
use Psr\Log\LoggerInterface;
2526
use React\EventLoop\LoopInterface;
2627
use React\Promise\Deferred;
27-
use React\Promise\ExtendedPromiseInterface;
28+
use React\Promise\PromiseInterface;
2829
use SplQueue;
2930

3031
/**
@@ -127,6 +128,11 @@ class Http
127128
*/
128129
protected $waiting = 0;
129130

131+
/**
132+
* Whether react/promise v3 is used, if false, using v2
133+
*/
134+
protected $promiseV3 = true;
135+
130136
/**
131137
* Http wrapper constructor.
132138
*
@@ -141,6 +147,10 @@ public function __construct(string $token, LoopInterface $loop, LoggerInterface
141147
$this->logger = $logger;
142148
$this->driver = $driver;
143149
$this->queue = new SplQueue;
150+
151+
if (str_starts_with(InstalledVersions::getVersion('react/promise'), '0.3.')) {
152+
$this->promiseV3 = true;
153+
}
144154
}
145155

146156
/**
@@ -160,9 +170,9 @@ public function setDriver(DriverInterface $driver): void
160170
* @param mixed $content
161171
* @param array $headers
162172
*
163-
* @return ExtendedPromiseInterface
173+
* @return PromiseInterface
164174
*/
165-
public function get($url, $content = null, array $headers = []): ExtendedPromiseInterface
175+
public function get($url, $content = null, array $headers = []): PromiseInterface
166176
{
167177
if (! ($url instanceof Endpoint)) {
168178
$url = Endpoint::bind($url);
@@ -178,9 +188,9 @@ public function get($url, $content = null, array $headers = []): ExtendedPromise
178188
* @param mixed $content
179189
* @param array $headers
180190
*
181-
* @return ExtendedPromiseInterface
191+
* @return PromiseInterface
182192
*/
183-
public function post($url, $content = null, array $headers = []): ExtendedPromiseInterface
193+
public function post($url, $content = null, array $headers = []): PromiseInterface
184194
{
185195
if (! ($url instanceof Endpoint)) {
186196
$url = Endpoint::bind($url);
@@ -196,9 +206,9 @@ public function post($url, $content = null, array $headers = []): ExtendedPromis
196206
* @param mixed $content
197207
* @param array $headers
198208
*
199-
* @return ExtendedPromiseInterface
209+
* @return PromiseInterface
200210
*/
201-
public function put($url, $content = null, array $headers = []): ExtendedPromiseInterface
211+
public function put($url, $content = null, array $headers = []): PromiseInterface
202212
{
203213
if (! ($url instanceof Endpoint)) {
204214
$url = Endpoint::bind($url);
@@ -214,9 +224,9 @@ public function put($url, $content = null, array $headers = []): ExtendedPromise
214224
* @param mixed $content
215225
* @param array $headers
216226
*
217-
* @return ExtendedPromiseInterface
227+
* @return PromiseInterface
218228
*/
219-
public function patch($url, $content = null, array $headers = []): ExtendedPromiseInterface
229+
public function patch($url, $content = null, array $headers = []): PromiseInterface
220230
{
221231
if (! ($url instanceof Endpoint)) {
222232
$url = Endpoint::bind($url);
@@ -232,9 +242,9 @@ public function patch($url, $content = null, array $headers = []): ExtendedPromi
232242
* @param mixed $content
233243
* @param array $headers
234244
*
235-
* @return ExtendedPromiseInterface
245+
* @return PromiseInterface
236246
*/
237-
public function delete($url, $content = null, array $headers = []): ExtendedPromiseInterface
247+
public function delete($url, $content = null, array $headers = []): PromiseInterface
238248
{
239249
if (! ($url instanceof Endpoint)) {
240250
$url = Endpoint::bind($url);
@@ -251,9 +261,9 @@ public function delete($url, $content = null, array $headers = []): ExtendedProm
251261
* @param mixed $content
252262
* @param array $headers
253263
*
254-
* @return ExtendedPromiseInterface
264+
* @return PromiseInterface
255265
*/
256-
public function queueRequest(string $method, Endpoint $url, $content, array $headers = []): ExtendedPromiseInterface
266+
public function queueRequest(string $method, Endpoint $url, $content, array $headers = []): PromiseInterface
257267
{
258268
$deferred = new Deferred();
259269

@@ -318,21 +328,21 @@ protected function guessContent(&$content)
318328
* @param Request $request
319329
* @param Deferred $deferred
320330
*
321-
* @return ExtendedPromiseInterface
331+
* @return PromiseInterface
322332
*/
323-
protected function executeRequest(Request $request, Deferred $deferred = null): ExtendedPromiseInterface
333+
protected function executeRequest(Request $request, Deferred $deferred = null): PromiseInterface
324334
{
325335
if ($deferred === null) {
326336
$deferred = new Deferred();
327337
}
328338

329339
if ($this->rateLimit) {
330-
$deferred->reject($this->rateLimit);
340+
$deferred->reject(new RateLimitException($this->rateLimit));
331341

332342
return $deferred->promise();
333343
}
334344

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) {
336346
$data = json_decode((string) $response->getBody());
337347
$statusCode = $response->getStatusCode();
338348

@@ -383,7 +393,7 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
383393
});
384394
}
385395

386-
$deferred->reject($rateLimit->isGlobal() ? $this->rateLimit : $rateLimit);
396+
$deferred->reject(new RateLimitException($rateLimit->isGlobal() ? $this->rateLimit : $rateLimit));
387397
}
388398
// Bad Gateway
389399
// Cloudflare SSL Handshake error

0 commit comments

Comments
 (0)