Skip to content

Commit 640edcf

Browse files
committed
add rate limit
1 parent 51c691b commit 640edcf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": "^8.3",
2020
"illuminate/contracts": "^10.0||^11.0||^12.0",
2121
"saloonphp/laravel-plugin": "^3.0",
22+
"saloonphp/rate-limit-plugin": "^2.0",
2223
"saloonphp/saloon": "^3.0",
2324
"spatie/laravel-package-tools": "^1.16"
2425
},

src/LaravelGetResponse.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@
44

55
namespace Ziming\LaravelGetResponse;
66

7+
use Illuminate\Support\Facades\Cache;
78
use Saloon\Contracts\Authenticator;
89
use Saloon\Http\Auth\HeaderAuthenticator;
910
use Saloon\Http\Connector;
11+
use Saloon\RateLimitPlugin\Contracts\RateLimitStore;
12+
use Saloon\RateLimitPlugin\Limit;
13+
use Saloon\RateLimitPlugin\Stores\LaravelCacheStore;
14+
use Saloon\RateLimitPlugin\Stores\MemoryStore;
15+
use Saloon\RateLimitPlugin\Traits\HasRateLimits;
1016

1117
/*
1218
* Add Oauth authenticator in the future
1319
*/
1420
class LaravelGetResponse extends Connector
1521
{
22+
use HasRateLimits;
23+
1624
public function __construct(protected readonly string $token) {}
1725

1826
protected function defaultAuth(): HeaderAuthenticator
@@ -24,4 +32,24 @@ public function resolveBaseUrl(): string
2432
{
2533
return 'https://api.getresponse.com/v3/';
2634
}
35+
36+
protected function resolveLimits(): array
37+
{
38+
return [
39+
Limit::allow(80)
40+
->everySeconds(1),
41+
42+
// 600 seconds is 10 minutes
43+
Limit::allow(30_000)
44+
->everySeconds(600),
45+
];
46+
}
47+
48+
protected function resolveRateLimitStore(): RateLimitStore
49+
{
50+
return new LaravelCacheStore(
51+
Cache::store(config('cache.default')
52+
)
53+
);
54+
}
2755
}

0 commit comments

Comments
 (0)