Chnage LastApiInfo to GetLastApiInfo with cloned version

This commit is contained in:
Mark Taylor
2015-08-16 21:27:26 +01:00
parent 5bd1f1d6c5
commit b2c7e1c2a7
13 changed files with 224 additions and 33 deletions
+20
View File
@@ -51,5 +51,25 @@ namespace Octokit
/// Information about the API rate limit
/// </summary>
public RateLimit RateLimit { get; private set; }
/// <summary>
/// Allows you to clone ApiInfo
/// </summary>
/// <returns>A clone of <seealso cref="ApiInfo"/></returns>
public ApiInfo Clone()
{
// Seem to have to do this to pass a whole bunch of tests (for example Octokit.Tests.Clients.EventsClientTests.DeserializesCommitCommentEventCorrectly)
// I believe this has something to do with the Mocking framework.
if (this == null || this.Links == null || this.OauthScopes == null || this.RateLimit == null || this.Etag == null)
return null;
return new ApiInfo(this.Links.Clone(),
this.OauthScopes.Clone(),
this.AcceptedOauthScopes.Clone(),
new String(this.Etag.ToCharArray()),
this.RateLimit.Clone());
}
}
}
+9 -18
View File
@@ -140,25 +140,15 @@ namespace Octokit
/// Gets the latest API Info - this will be null if no API calls have been made
/// </summary>
/// <returns><seealso cref="ApiInfo"/> representing the information returned as part of an Api call</returns>
public ApiInfo LastApiInfo
{
get
{
lock (LastApiInfoLocker)
{
return _lastApiInfo;
}
}
private set
{
lock (LastApiInfoLocker)
{
_lastApiInfo = value;
}
}
public ApiInfo GetLastApiInfo()
{
// We've choosen to not wrap the _lastApiInfo in a lock. Originally the code was returning a reference - so there was a danger of
// on thread writing to the object while another was reading. Now we are cloning the ApiInfo on request - thus removing the need (or overhead)
// of putting locks in place.
// See https://github.com/octokit/octokit.net/pull/855#discussion_r36774884
return _lastApiInfo == null ? null : _lastApiInfo.Clone();
}
private ApiInfo _lastApiInfo;
private readonly object LastApiInfoLocker = new object();
public Task<IApiResponse<T>> Get<T>(Uri uri, IDictionary<string, string> parameters, string accepts)
{
@@ -550,7 +540,8 @@ namespace Octokit
var response = await _httpClient.Send(request, cancellationToken).ConfigureAwait(false);
if (response != null)
{
LastApiInfo = response.ApiInfo;
// Use the clone method to avoid keeping hold of the original (just in case it effect the lifetime of the whole response
_lastApiInfo = response.ApiInfo.Clone();
}
HandleErrors(response);
return response;
+2 -1
View File
@@ -9,6 +9,7 @@
/// Gets the latest API Info - this will be null if no API calls have been made
/// </summary>
/// <returns><seealso cref="ApiInfo"/> representing the information returned as part of an Api call</returns>
ApiInfo LastApiInfo { get; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
ApiInfo GetLastApiInfo();
}
}
+14
View File
@@ -99,5 +99,19 @@ namespace Octokit
}
}
/// <summary>
/// Allows you to clone RateLimit
/// </summary>
/// <returns>A clone of <seealso cref="RateLimit"/></returns>
public RateLimit Clone()
{
var clone = new RateLimit();
clone.Limit = this.Limit;
clone.Remaining = this.Remaining;
clone.ResetAsUtcEpochSeconds = this.ResetAsUtcEpochSeconds;
return clone;
}
}
}