diff --git a/WebApiThrottle/ThrottlingFilter.cs b/WebApiThrottle/ThrottlingFilter.cs index a0e4d11..5020270 100644 --- a/WebApiThrottle/ThrottlingFilter.cs +++ b/WebApiThrottle/ThrottlingFilter.cs @@ -103,6 +103,13 @@ public ThrottlePolicy Policy /// public string QuotaExceededMessage { get; set; } + /// + /// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message. + /// If none specified the default will be: + /// API calls quota exceeded! maximum admitted {0} per {1} + /// + public Func QuotaExceededContent { get; set; } + /// /// Gets or sets the value to return as the HTTP status /// code when a request is rejected because of the @@ -184,6 +191,10 @@ public override void OnActionExecuting(HttpActionContext actionContext) ? this.QuotaExceededMessage : "API calls quota exceeded! maximum admitted {0} per {1}."; + var content = this.QuotaExceededContent != null + ? this.QuotaExceededContent(rateLimit, rateLimitPeriod) + : string.Format(message, rateLimit, rateLimitPeriod); + // add status code and retry after x seconds to response actionContext.Response = QuotaExceededResponse( actionContext.Request, @@ -219,9 +230,9 @@ protected IPAddress GetClientIp(HttpRequestMessage request) return core.GetClientIp(request); } - protected virtual HttpResponseMessage QuotaExceededResponse(HttpRequestMessage request, string message, HttpStatusCode responseCode, string retryAfter) + protected virtual HttpResponseMessage QuotaExceededResponse(HttpRequestMessage request, object content, HttpStatusCode responseCode, string retryAfter) { - var response = request.CreateResponse(responseCode, message); + var response = request.CreateResponse(responseCode, content); response.Headers.Add("Retry-After", new string[] { retryAfter }); return response; } diff --git a/WebApiThrottle/ThrottlingHandler.cs b/WebApiThrottle/ThrottlingHandler.cs index df5189f..c02170d 100644 --- a/WebApiThrottle/ThrottlingHandler.cs +++ b/WebApiThrottle/ThrottlingHandler.cs @@ -102,6 +102,13 @@ public ThrottlePolicy Policy /// public string QuotaExceededMessage { get; set; } + /// + /// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message. + /// If none specified the default will be: + /// API calls quota exceeded! maximum admitted {0} per {1} + /// + public Func QuotaExceededContent { get; set; } + /// /// Gets or sets the value to return as the HTTP status /// code when a request is rejected because of the @@ -179,10 +186,14 @@ protected override Task SendAsync(HttpRequestMessage reques ? this.QuotaExceededMessage : "API calls quota exceeded! maximum admitted {0} per {1}."; + var content = this.QuotaExceededContent != null + ? this.QuotaExceededContent(rateLimit, rateLimitPeriod) + : string.Format(message, rateLimit, rateLimitPeriod); + // break execution return QuotaExceededResponse( request, - string.Format(message, rateLimit, rateLimitPeriod), + content, QuotaExceededResponseCode, core.RetryAfterFrom(throttleCounter.Timestamp, rateLimitPeriod)); } @@ -215,9 +226,9 @@ protected virtual string ComputeThrottleKey(RequestIdentity requestIdentity, Rat return core.ComputeThrottleKey(requestIdentity, period); } - protected virtual Task QuotaExceededResponse(HttpRequestMessage request, string message, HttpStatusCode responseCode, string retryAfter) + protected virtual Task QuotaExceededResponse(HttpRequestMessage request, object content, HttpStatusCode responseCode, string retryAfter) { - var response = request.CreateResponse(responseCode, message); + var response = request.CreateResponse(responseCode, content); response.Headers.Add("Retry-After", new string[] { retryAfter }); return Task.FromResult(response); }