Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support request methods selection #12

Merged
merged 3 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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