Skip to content

Commit

Permalink
Support request methods selection (#12)
Browse files Browse the repository at this point in the history
* Fix bug concerning null ApiRateLimit instance in RateLimitHandler

* Support request methods selection
  • Loading branch information
baudev authored and IndraGunawan committed Jul 16, 2019
1 parent 8811924 commit 55ade1a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Annotation/ApiRateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ final class ApiRateLimit
* @var array
*/
public $throttle = [];

/**
* @var array
* @example ["GET", "POST"]
*/
public $methods = [];
}
4 changes: 3 additions & 1 deletion Resources/doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Configuration per resource
-------------------------------

If you wish to configure the rate limits differently on some resources, you can use the `ApiRateLimit` annotation and set the `throttle` property in the same way you do in your main configuration.
You can define a rate limits only for some specific methods using `methods` property (by default `null` to cover all the different methods).
You can also choose to enable or disable rate limiting by using the `enabled` property.

```php
Expand Down Expand Up @@ -128,7 +129,8 @@ use Indragunawan\ApiRateLimitBundle\Annotation\ApiRateLimit;
* "period"=10
* }
* }
* }
* },
* methods={"GET", "DELETE"}
* )
*/
class Foo
Expand Down
4 changes: 4 additions & 0 deletions Service/RateLimitHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public function handle(Request $request)

if (null !== $annotation) {
$this->enabled = $annotation->enabled;
if (!in_array($request->getMethod(), array_map('strtoupper', $annotation->methods), true) && !empty($annotation->methods)) {
// The annotation is ignored as the method is not corresponding
$annotation = new ApiRateLimit();
}
} else {
$annotation = new ApiRateLimit();
}
Expand Down

0 comments on commit 55ade1a

Please sign in to comment.