Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Added QuotaExceededContent for object/json responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Hwang committed Nov 2, 2015
1 parent a950726 commit 4025396
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 13 additions & 2 deletions WebApiThrottle/ThrottlingFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public ThrottlePolicy Policy
/// </summary>
public string QuotaExceededMessage { get; set; }

/// <summary>
/// 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}
/// </summary>
public Func<long, RateLimitPeriod, object> QuotaExceededContent { get; set; }

/// <summary>
/// Gets or sets the value to return as the HTTP status
/// code when a request is rejected because of the
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 14 additions & 3 deletions WebApiThrottle/ThrottlingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public ThrottlePolicy Policy
/// </summary>
public string QuotaExceededMessage { get; set; }

/// <summary>
/// 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}
/// </summary>
public Func<long, RateLimitPeriod, object> QuotaExceededContent { get; set; }

/// <summary>
/// Gets or sets the value to return as the HTTP status
/// code when a request is rejected because of the
Expand Down Expand Up @@ -179,10 +186,14 @@ protected override Task<HttpResponseMessage> 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));
}
Expand Down Expand Up @@ -215,9 +226,9 @@ protected virtual string ComputeThrottleKey(RequestIdentity requestIdentity, Rat
return core.ComputeThrottleKey(requestIdentity, period);
}

protected virtual Task<HttpResponseMessage> QuotaExceededResponse(HttpRequestMessage request, string message, HttpStatusCode responseCode, string retryAfter)
protected virtual Task<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 Task.FromResult(response);
}
Expand Down

0 comments on commit 4025396

Please sign in to comment.