Merge pull request #848 from Red-Folder/master

Added GetRateLimits to MiscellaneousClient
This commit is contained in:
Phil Haack
2015-07-27 14:00:16 -07:00
16 changed files with 271 additions and 0 deletions
@@ -50,5 +50,14 @@ namespace Octokit.Reactive
/// <param name="key"></param>
/// <returns>A <see cref="License" /> that includes the license key, text, and attributes of the license.</returns>
IObservable<License> GetLicense(string key);
/// <summary>
/// Gets API Rate Limits (API service rather than header info).
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>An <see cref="MiscellaneousRateLimit"/> of Rate Limits.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<MiscellaneousRateLimit> GetRateLimits();
}
}
@@ -74,5 +74,16 @@ namespace Octokit.Reactive
{
return _client.GetLicense(key).ToObservable();
}
/// <summary>
/// Gets API Rate Limits (API service rather than header info).
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>An <see cref="MiscellaneousRateLimit"/> of Rate Limits.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
public IObservable<MiscellaneousRateLimit> GetRateLimits()
{
return _client.GetRateLimits().ToObservable();
}
}
}
@@ -70,4 +70,37 @@ public class MiscellaneousClientTests
Assert.Equal("MIT License", result.Name);
}
}
public class TheGetResourceRateLimitsMethod
{
[IntegrationTest]
public async Task CanRetrieveResourceRateLimits()
{
var github = Helper.GetAuthenticatedClient();
var result = await github.Miscellaneous.GetRateLimits();
// Test the core limits
Assert.True(result.Resources.Core.Limit > 0);
Assert.True(result.Resources.Core.Remaining > -1);
Assert.True(result.Resources.Core.Remaining <= result.Resources.Core.Limit);
Assert.True(result.Resources.Core.ResetAsUtcEpochSeconds > 0);
Assert.NotNull(result.Resources.Core.Reset);
// Test the search limits
Assert.True(result.Resources.Search.Limit > 0);
Assert.True(result.Resources.Search.Remaining > -1);
Assert.True(result.Resources.Search.Remaining <= result.Resources.Search.Limit);
Assert.True(result.Resources.Search.ResetAsUtcEpochSeconds > 0);
Assert.NotNull(result.Resources.Search.Reset);
// Test the depreciated rate limits
Assert.True(result.Rate.Limit > 0);
Assert.True(result.Rate.Remaining > -1);
Assert.True(result.Rate.Remaining <= result.Rate.Limit);
Assert.True(result.Resources.Search.ResetAsUtcEpochSeconds > 0);
Assert.NotNull(result.Resources.Search.Reset);
}
}
}
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
using Xunit;
using System.Globalization;
namespace Octokit.Tests.Clients
{
@@ -58,6 +59,63 @@ namespace Octokit.Tests.Clients
}
}
public class TheGetResourceRateLimitsMethod
{
[Fact]
public async Task RequestsTheRecourceRateLimitEndpoint()
{
IApiResponse<MiscellaneousRateLimit> response = new ApiResponse<MiscellaneousRateLimit>
(
new Response(),
new MiscellaneousRateLimit(
new ResourceRateLimit(
new RateLimit(5000, 4999, 1372700873),
new RateLimit(30, 18, 1372700873)
),
new RateLimit(100, 75, 1372700873)
)
);
var connection = Substitute.For<IConnection>();
connection.Get<MiscellaneousRateLimit>(Args.Uri, null, null).Returns(Task.FromResult(response));
var client = new MiscellaneousClient(connection);
var result = await client.GetRateLimits();
// Test the core limits
Assert.Equal(5000, result.Resources.Core.Limit);
Assert.Equal(4999, result.Resources.Core.Remaining);
Assert.Equal(1372700873, result.Resources.Core.ResetAsUtcEpochSeconds);
var expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, result.Resources.Core.Reset);
// Test the search limits
Assert.Equal(30, result.Resources.Search.Limit);
Assert.Equal(18, result.Resources.Search.Remaining);
Assert.Equal(1372700873, result.Resources.Search.ResetAsUtcEpochSeconds);
expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, result.Resources.Search.Reset);
// Test the depreciated rate limits
Assert.Equal(100, result.Rate.Limit);
Assert.Equal(75, result.Rate.Remaining);
Assert.Equal(1372700873, result.Rate.ResetAsUtcEpochSeconds);
expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, result.Rate.Reset);
connection.Received()
.Get<MiscellaneousRateLimit>(Arg.Is<Uri>(u => u.ToString() == "rate_limit"), null, null);
}
}
public class TheCtor
{
[Fact]
+8
View File
@@ -59,5 +59,13 @@ namespace Octokit
/// <param name="key"></param>
/// <returns>A <see cref="License" /> that includes the license key, text, and attributes of the license.</returns>
Task<License> GetLicense(string key);
/// <summary>
/// Gets API Rate Limits (API service rather than header info).
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>An <see cref="MiscellaneousRateLimit"/> of Rate Limits.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<MiscellaneousRateLimit> GetRateLimits();
}
}
+13
View File
@@ -116,5 +116,18 @@ namespace Octokit
.ConfigureAwait(false);
return response.Body;
}
/// <summary>
/// Gets API Rate Limits (API service rather than header info).
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>An <see cref="MiscellaneousRateLimit"/> of Rate Limits.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
public async Task<MiscellaneousRateLimit> GetRateLimits()
{
var endpoint = new Uri("rate_limit", UriKind.Relative);
var response = await _connection.Get<MiscellaneousRateLimit>(endpoint, null, null).ConfigureAwait(false);
return response.Body;
}
}
}
@@ -18,5 +18,14 @@ namespace Octokit.Helpers
{
return new DateTimeOffset(unixTime * TimeSpan.TicksPerSecond + unixEpochTicks, TimeSpan.Zero);
}
/// <summary>
/// Convert <see cref="DateTimeOffset"/> with UTC offset to a Unix tick
/// </summary>
/// <param name="date">Date Time with UTC offset</param>
public static long ToUnixTime(this DateTimeOffset date)
{
return (date.Ticks - unixEpochTicks) / TimeSpan.TicksPerSecond;
}
}
}
+33
View File
@@ -2,17 +2,22 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using Octokit.Helpers;
using System.Diagnostics;
using System.Globalization;
using Octokit.Internal;
namespace Octokit
{
#if !NETFX_CORE
[Serializable]
#endif
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RateLimit
#if !NETFX_CORE
: ISerializable
#endif
{
public RateLimit() {}
public RateLimit(IDictionary<string, string> responseHeaders)
{
@@ -23,6 +28,17 @@ namespace Octokit
Reset = GetHeaderValueAsInt32Safe(responseHeaders, "X-RateLimit-Reset").FromUnixTime();
}
public RateLimit(int limit, int remaining, long reset)
{
Ensure.ArgumentNotNull(limit, "limit");
Ensure.ArgumentNotNull(remaining, "remaining");
Ensure.ArgumentNotNull(reset, "reset");
Limit = limit;
Remaining = remaining;
Reset = reset.FromUnixTime();
}
/// <summary>
/// The maximum number of requests that the consumer is permitted to make per hour.
/// </summary>
@@ -36,8 +52,16 @@ namespace Octokit
/// <summary>
/// The date and time at which the current rate limit window resets
/// </summary>
[ParameterAttribute(Key = "ignoreThisField")]
public DateTimeOffset Reset { get; private set; }
/// <summary>
/// The date and time at which the current rate limit window resets - in UTC epoch seconds
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[ParameterAttribute(Key = "reset")]
public long ResetAsUtcEpochSeconds { get { return Reset.ToUnixTime(); } private set { Reset = value.FromUnixTime(); } }
static long GetHeaderValueAsInt32Safe(IDictionary<string, string> responseHeaders, string key)
{
string value;
@@ -66,5 +90,14 @@ namespace Octokit
info.AddValue("Reset", Reset.Ticks);
}
#endif
internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture, "Limit {0}, Remaining {1}, Reset {2} ", Limit, Remaining, Reset);
}
}
}
}
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class MiscellaneousRateLimit
{
public MiscellaneousRateLimit() {}
public MiscellaneousRateLimit(ResourceRateLimit resources, RateLimit rate)
{
Ensure.ArgumentNotNull(resources, "resource");
Ensure.ArgumentNotNull(rate, "rate");
Resources = resources;
Rate = rate;
}
/// <summary>
/// Object of resources rate limits
/// </summary>
public ResourceRateLimit Resources { get; private set; }
/// <summary>
/// Legacy rate limit - to be depreciated - https://developer.github.com/v3/rate_limit/#deprecation-notice
/// </summary>
public RateLimit Rate { get; private set; }
internal string DebuggerDisplay
{
get
{
return Resources == null ? "No rates found" : String.Format(CultureInfo.InvariantCulture, Resources.DebuggerDisplay);
}
}
}
}
@@ -0,0 +1,42 @@
using System;
using System.Diagnostics;
using System.Globalization;
using Octokit.Helpers;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class ResourceRateLimit
{
public ResourceRateLimit() {}
public ResourceRateLimit(RateLimit core, RateLimit search)
{
Ensure.ArgumentNotNull(core, "core");
Ensure.ArgumentNotNull(search, "search");
Core = core;
Search = search;
}
/// <summary>
/// Rate limits for core API (rate limit for everything except Search API)
/// </summary>
public RateLimit Core { get; private set; }
/// <summary>
/// Rate Limits for Search API
/// </summary>
public RateLimit Search { get; private set; }
internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture, "Core: {0}; Search: {1} ", Core.DebuggerDisplay, Search.DebuggerDisplay);
}
}
}
}
+2
View File
@@ -396,6 +396,8 @@
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+2
View File
@@ -412,6 +412,8 @@
<Compile Include="Models\Request\RepositoryHookTestRequest.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
+2
View File
@@ -405,6 +405,8 @@
<Compile Include="Models\Request\RepositoryHookTestRequest.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
+2
View File
@@ -395,6 +395,8 @@
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
</ItemGroup>
<ItemGroup>
+2
View File
@@ -399,6 +399,8 @@
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
</ItemGroup>
<ItemGroup>
+2
View File
@@ -117,8 +117,10 @@
<Compile Include="Models\Response\ContentType.cs" />
<Compile Include="Models\Response\ApplicationAuthorization.cs" />
<Compile Include="Models\Response\GitHubCommitFile.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Models\Response\PublicKey.cs" />
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\RepositoryContent.cs" />
<Compile Include="Models\Response\RepositoryContentChangeSet.cs" />
<Compile Include="Models\Response\RepositoryContributor.cs" />