Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing some issues with RateLimiting handling in 1.1 API
- Loading branch information
1 parent
77257a0
commit f7c5c9c
Showing
1 changed file
with
19 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ namespace Twitterizer.Core | |
using System.Web; | ||
#endif | ||
using Twitterizer; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// The base command class. | ||
|
@@ -246,6 +247,20 @@ public TwitterResponse<T> ExecuteCommand() | |
responseData = ConversionUtility.ReadStream(exceptionResponse.GetResponseStream()); | ||
twitterResponse.Content = Encoding.UTF8.GetString(responseData, 0, responseData.Length); | ||
|
||
if (!String.IsNullOrEmpty(twitterResponse.Content)) | ||
{ | ||
var responseContent = JsonConvert.DeserializeObject<dynamic>(twitterResponse.Content); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
DigitallyBorn
Member
|
||
if (responseContent != null && responseContent.errors != null) | ||
{ | ||
var errors = responseContent.errors; | ||
if (errors != null && ((IEnumerable<dynamic>)errors).Any()) | ||
{ | ||
foreach (var error in errors) | ||
twitterResponse.ErrorMessage += error.code + ":" + error.message + ";"; | ||
} | ||
} | ||
} | ||
|
||
#if !SILVERLIGHT | ||
rateLimiting = ParseRateLimitHeaders(exceptionResponse.Headers); | ||
|
||
|
@@ -318,10 +333,11 @@ private static void SetStatusCode(TwitterResponse<T> twitterResponse, HttpStatus | |
break; | ||
|
||
case HttpStatusCode.BadRequest: | ||
twitterResponse.Result = (rateLimiting != null && rateLimiting.Remaining == 0) ? RequestResult.RateLimited : RequestResult.BadRequest; | ||
twitterResponse.Result = RequestResult.BadRequest; | ||
break; | ||
|
||
case (HttpStatusCode)420: //Rate Limited from Search/Trends API | ||
case (HttpStatusCode)429: | ||
twitterResponse.Result = RequestResult.RateLimited; | ||
break; | ||
|
||
|
@@ -372,12 +388,12 @@ private static RateLimiting ParseRateLimitHeaders(WebHeaderCollection responseHe | |
{ | ||
RateLimiting rateLimiting = new RateLimiting(); | ||
|
||
if (responseHeaders.AllKeys.Contains("X-RateLimit-Limit")) | ||
if (responseHeaders.AllKeys.Any(x => x.Equals("X-RateLimit-Limit", StringComparison.InvariantCultureIgnoreCase))) | ||
{ | ||
rateLimiting.Total = int.Parse(responseHeaders["X-RateLimit-Limit"], CultureInfo.InvariantCulture); | ||
} | ||
|
||
if (responseHeaders.AllKeys.Contains("X-RateLimit-Remaining")) | ||
if (responseHeaders.AllKeys.Any(x => x.Equals("X-RateLimit-Remaining", StringComparison.InvariantCultureIgnoreCase))) | ||
{ | ||
rateLimiting.Remaining = int.Parse(responseHeaders["X-RateLimit-Remaining"], CultureInfo.InvariantCulture); | ||
} | ||
|
This line breaks .NET 3.5 compatibility. @DigitallyBorn do you mind if we replace this with a version that works with .NET 3.5? Else I would do it Smuxi's Twitterizer fork.