retrieve API usage limits; rename Statistics() to GetUserStatistics();

This commit is contained in:
2013-10-25 00:52:29 +02:00
parent 8bce874504
commit a1f6637e9d
5 changed files with 136 additions and 2 deletions
+9
View File
@@ -151,5 +151,14 @@ namespace PocketSharp.Tests
Assert.True(statistics.CountAll > 0);
}
[Fact]
public async Task AreLimitsRetrieved()
{
PocketLimits limits = await client.GetUsageLimits();
Assert.True(limits.RateLimitForConsumerKey > 9999);
}
}
}
+44 -2
View File
@@ -1,4 +1,5 @@
using PocketSharp.Models;
using Newtonsoft.Json;
using PocketSharp.Models;
using System;
using System.Threading.Tasks;
@@ -14,9 +15,50 @@ namespace PocketSharp
/// </summary>
/// <returns></returns>
/// <exception cref="PocketException"></exception>
public async Task<PocketStatistics> Statistics()
public async Task<PocketStatistics> GetUserStatistics()
{
return await Request<PocketStatistics>("stats");
}
/// <summary>
/// Statistics from the user account.
/// </summary>
/// <returns></returns>
/// <exception cref="PocketException"></exception>
[Obsolete("Please use GetUserStatistics")]
public async Task<PocketStatistics> Statistics()
{
return await GetUserStatistics();
}
/// <summary>
/// Returns API usage statistics.
/// If a request was made before, the data is returned synchronously from the cache.
/// Note: This method only works for authenticated users with a given AccessCode.
/// </summary>
/// <returns></returns>
/// <exception cref="PocketException"></exception>
public async Task<PocketLimits> GetUsageLimits()
{
string rateLimitForConsumerKey = TryGetHeaderValue(lastHeaders, "X-Limit-Key-Limit");
if (rateLimitForConsumerKey == null)
{
// this is the fastest way to do a non-failing request to receive the correct headers
await Get(count: 1);
}
return new PocketLimits()
{
RateLimitForConsumerKey = Convert.ToInt32(TryGetHeaderValue(lastHeaders, "X-Limit-Key-Limit")),
RemainingCallsForConsumerKey = Convert.ToInt32(TryGetHeaderValue(lastHeaders, "X-Limit-Key-Remaining")),
SecondsUntilLimitResetsForConsumerKey = Convert.ToInt32(TryGetHeaderValue(lastHeaders, "X-Limit-Key-Reset")),
RateLimitForUser = Convert.ToInt32(TryGetHeaderValue(lastHeaders, "X-Limit-User-Limit")),
RemainingCallsForUser = Convert.ToInt32(TryGetHeaderValue(lastHeaders, "X-Limit-User-Remaining")),
SecondsUntilLimitResetsForUser = Convert.ToInt32(TryGetHeaderValue(lastHeaders, "X-Limit-User-Reset"))
};
}
}
}
+67
View File
@@ -0,0 +1,67 @@
using Newtonsoft.Json;
using PropertyChanged;
namespace PocketSharp.Models
{
/// <summary>
/// API Limitation Statistics
/// </summary>
[JsonObject]
[ImplementPropertyChanged]
public class PocketLimits
{
/// <summary>
/// Gets or sets the rate limit.
/// </summary>
/// <value>
/// Rate limit for current consumer key.
/// </value>
[JsonProperty("X-Limit-Key-Limit")]
public int RateLimitForConsumerKey { get; set; }
/// <summary>
/// Gets or sets the remaining calls.
/// </summary>
/// <value>
/// Remaining calls for current consumer key.
/// </value>
[JsonProperty("X-Limit-Key-Remaining")]
public int RemainingCallsForConsumerKey { get; set; }
/// <summary>
/// Gets or sets the reset seconds.
/// </summary>
/// <value>
/// Seconds until limit resets for current consumer key.
/// </value>
[JsonProperty("X-Limit-Key-Reset")]
public int SecondsUntilLimitResetsForConsumerKey { get; set; }
/// <summary>
/// Gets or sets the rate limit.
/// </summary>
/// <value>
/// Rate limit for current user.
/// </value>
[JsonProperty("X-Limit-User-Limit")]
public int RateLimitForUser { get; set; }
/// <summary>
/// Gets or sets the remaining calls.
/// </summary>
/// <value>
/// Remaining calls for current user.
/// </value>
[JsonProperty("X-Limit-User-Remaining")]
public int RemainingCallsForUser { get; set; }
/// <summary>
/// Gets or sets the reset seconds.
/// </summary>
/// <value>
/// Seconds until limit resets for current user.
/// </value>
[JsonProperty("X-Limit-User-Reset")]
public int SecondsUntilLimitResetsForUser { get; set; }
}
}
+15
View File
@@ -22,6 +22,11 @@ namespace PocketSharp
/// </summary>
protected readonly HttpClient _restClient;
/// <summary>
/// Caches HTTP headers from last response
/// </summary>
private HttpResponseHeaders lastHeaders;
/// <summary>
/// The base URL for the Pocket API
/// </summary>
@@ -54,6 +59,7 @@ namespace PocketSharp
public string AccessCode { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="PocketClient"/> class.
/// </summary>
@@ -144,6 +150,9 @@ namespace PocketSharp
// validate HTTP response
ValidateResponse(response);
// cache headers
lastHeaders = response.Headers;
// read response
var responseString = await response.Content.ReadAsStringAsync();
@@ -267,6 +276,11 @@ namespace PocketSharp
{
string result = null;
if (headers == null || String.IsNullOrEmpty(key))
{
return null;
}
foreach (var header in headers)
{
if (header.Key == key)
@@ -275,6 +289,7 @@ namespace PocketSharp
headerEnumerator.MoveNext();
result = headerEnumerator.Current;
break;
}
}
+1
View File
@@ -44,6 +44,7 @@
<Compile Include="Components\Statistics.cs" />
<Compile Include="Models\Parameters\RegisterParameters.cs" />
<Compile Include="Models\PocketArticle.cs" />
<Compile Include="Models\PocketLimits.cs" />
<Compile Include="Models\PocketStatistics.cs" />
<Compile Include="Utilities\PocketException.cs" />
<Compile Include="Components\Add.cs" />