Skip to content

Commit c2097b9

Browse files
committed
Merge pull request dingo#950 from orchestral/feature/rate-limiter-on-throttle
Allow custom limiter when Dingo\Api\Contract\Http\RateLimit\Throttle also implements HasRateLimiter.
2 parents d123017 + ac3b993 commit c2097b9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Dingo\Api\Contract\Http\RateLimit;
4+
5+
use Dingo\Api\Http\Request;
6+
use Illuminate\Container\Container;
7+
8+
interface HasRateLimiter
9+
{
10+
/**
11+
* Get rate limiter callable.
12+
*
13+
* @param \Illuminate\Container\Container $app
14+
* @param \Dingo\Api\Http\Request $request
15+
*
16+
* @return string
17+
*/
18+
public function getRateLimiter(Container $app, Request $request);
19+
}

src/Http/RateLimit/Handler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Container\Container;
99
use Dingo\Api\Http\RateLimit\Throttle\Route;
1010
use Dingo\Api\Contract\Http\RateLimit\Throttle;
11+
use Dingo\Api\Contract\Http\RateLimit\HasRateLimiter;
1112

1213
class Handler
1314
{
@@ -92,7 +93,9 @@ public function rateLimitRequest(Request $request, $limit = 0, $expires = 0)
9293
// If the throttle instance is already set then we'll just carry on as
9394
// per usual.
9495
if ($this->throttle instanceof Throttle) {
95-
//
96+
if ($this->throttle instanceof HasRateLimiter) {
97+
$this->setRateLimiter([$this->throttle, 'getRateLimiter']);
98+
}
9699

97100
// If the developer specified a certain amount of requests or expiration
98101
// time on a specific route then we'll always use the route specific

0 commit comments

Comments
 (0)