Skip to content

Commit

Permalink
Fixing some issues with RateLimiting handling in 1.1 API
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkOSIndustries committed Apr 7, 2014
1 parent 77257a0 commit f7c5c9c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Twitterizer2/Core/TwitterCommand.cs
Expand Up @@ -43,6 +43,7 @@ namespace Twitterizer.Core
using System.Web;
#endif
using Twitterizer;
using Newtonsoft.Json;

/// <summary>
/// The base command class.
Expand Down Expand Up @@ -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.

Copy link
@meebey

meebey Jan 12, 2015

Member

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.

This comment has been minimized.

Copy link
@DigitallyBorn

DigitallyBorn Jan 12, 2015

Member

Might have to be a bit of refactoring to make it happen, but I don't have a problem with the code changing.

This comment has been minimized.

Copy link
@meebey

meebey Jan 12, 2015

Member

This code is the only regression, it always was .NET 3.5 compatible before :) (Smuxi needs that)

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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit f7c5c9c

Please sign in to comment.