diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index f84892243b..c3acbb6bb8 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -721,10 +721,6 @@ "text": "Distributed Locking", "path": "framework/infrastructure/distributed-locking.md" }, - { - "text": "Operation Rate Limiting", - "path": "framework/infrastructure/operation-rate-limiting.md" - }, { "text": "Email Sending", "items": [ @@ -811,6 +807,10 @@ "text": "Object to Object Mapping", "path": "framework/infrastructure/object-to-object-mapping.md" }, + { + "text": "Operation Rate Limiting", + "path": "framework/infrastructure/operation-rate-limiting.md" + }, { "text": "Settings", "path": "framework/infrastructure/settings.md" diff --git a/docs/en/framework/infrastructure/operation-rate-limiting.md b/docs/en/framework/infrastructure/operation-rate-limiting.md index 736152444f..5208c2c959 100644 --- a/docs/en/framework/infrastructure/operation-rate-limiting.md +++ b/docs/en/framework/infrastructure/operation-rate-limiting.md @@ -64,8 +64,7 @@ public class SmsAppService : ApplicationService public async Task SendCodeAsync(string phoneNumber) { - await _rateLimitChecker.CheckAsync("SendSmsCode", - new OperationRateLimitingContext { Parameter = phoneNumber }); + await _rateLimitChecker.CheckAsync("SendSmsCode", phoneNumber); // If we reach here, the limit was not exceeded. // Send the SMS code... @@ -75,6 +74,7 @@ public class SmsAppService : ApplicationService * `CheckAsync` increments the counter and throws `AbpOperationRateLimitingException` (HTTP 429) if the limit is exceeded. * Each phone number has its own counter because we used `PartitionByParameter()`. +* Passing `phoneNumber` directly is a shortcut for `new OperationRateLimitingContext { Parameter = phoneNumber }`. Extension methods are provided for all four methods (`CheckAsync`, `IsAllowedAsync`, `GetStatusAsync`, `ResetAsync`) when you only need to pass a `parameter` string. That's the basic usage. The following sections explain each concept in detail. diff --git a/framework/src/Volo.Abp.OperationRateLimiting/Volo/Abp/OperationRateLimiting/Checker/OperationRateLimitingCheckerExtensions.cs b/framework/src/Volo.Abp.OperationRateLimiting/Volo/Abp/OperationRateLimiting/Checker/OperationRateLimitingCheckerExtensions.cs new file mode 100644 index 0000000000..df8d195aab --- /dev/null +++ b/framework/src/Volo.Abp.OperationRateLimiting/Volo/Abp/OperationRateLimiting/Checker/OperationRateLimitingCheckerExtensions.cs @@ -0,0 +1,38 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.OperationRateLimiting; + +public static class OperationRateLimitingCheckerExtensions +{ + public static Task CheckAsync( + this IOperationRateLimitingChecker checker, + string policyName, + string parameter) + { + return checker.CheckAsync(policyName, new OperationRateLimitingContext { Parameter = parameter }); + } + + public static Task IsAllowedAsync( + this IOperationRateLimitingChecker checker, + string policyName, + string parameter) + { + return checker.IsAllowedAsync(policyName, new OperationRateLimitingContext { Parameter = parameter }); + } + + public static Task GetStatusAsync( + this IOperationRateLimitingChecker checker, + string policyName, + string parameter) + { + return checker.GetStatusAsync(policyName, new OperationRateLimitingContext { Parameter = parameter }); + } + + public static Task ResetAsync( + this IOperationRateLimitingChecker checker, + string policyName, + string parameter) + { + return checker.ResetAsync(policyName, new OperationRateLimitingContext { Parameter = parameter }); + } +}