mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-05 03:30:34 +00:00
Chnage LastApiInfo to GetLastApiInfo with cloned version
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user