using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.Serialization; using Octokit.Helpers; 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 responseHeaders) { Ensure.ArgumentNotNull(responseHeaders, "responseHeaders"); Limit = (int)GetHeaderValueAsInt32Safe(responseHeaders, "X-RateLimit-Limit"); Remaining = (int)GetHeaderValueAsInt32Safe(responseHeaders, "X-RateLimit-Remaining"); ResetAsUtcEpochSeconds = GetHeaderValueAsInt32Safe(responseHeaders, "X-RateLimit-Reset"); } public RateLimit(int limit, int remaining, long reset) { Ensure.ArgumentNotNull(limit, "limit"); Ensure.ArgumentNotNull(remaining, "remaining"); Ensure.ArgumentNotNull(reset, "reset"); Limit = limit; Remaining = remaining; ResetAsUtcEpochSeconds = reset; } /// /// The maximum number of requests that the consumer is permitted to make per hour. /// public int Limit { get; private set; } /// /// The number of requests remaining in the current rate limit window. /// public int Remaining { get; private set; } /// /// The date and time at which the current rate limit window resets /// [Parameter(Key = "ignoreThisField")] public DateTimeOffset Reset { get { return ResetAsUtcEpochSeconds.FromUnixTime(); } } /// /// The date and time at which the current rate limit window resets - in UTC epoch seconds /// [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [Parameter(Key = "reset")] public long ResetAsUtcEpochSeconds { get; private set; } static long GetHeaderValueAsInt32Safe(IDictionary responseHeaders, string key) { string value; long result; return !responseHeaders.TryGetValue(key, out value) || value == null || !long.TryParse(value, out result) ? 0 : result; } #if !NETFX_CORE protected RateLimit(SerializationInfo info, StreamingContext context) { Ensure.ArgumentNotNull(info, "info"); Limit = info.GetInt32("Limit"); Remaining = info.GetInt32("Remaining"); ResetAsUtcEpochSeconds = info.GetInt64("ResetAsUtcEpochSeconds"); } public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { Ensure.ArgumentNotNull(info, "info"); info.AddValue("Limit", Limit); info.AddValue("Remaining", Remaining); info.AddValue("ResetAsUtcEpochSeconds", ResetAsUtcEpochSeconds); } #endif internal string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "Limit {0}, Remaining {1}, Reset {2} ", Limit, Remaining, Reset); } } /// /// Allows you to clone RateLimit /// /// A clone of public RateLimit Clone() { return new RateLimit { Limit = Limit, Remaining = Remaining, ResetAsUtcEpochSeconds = ResetAsUtcEpochSeconds }; } } }