diff --git a/Octokit.Tests/Clients/StatisticsClientTests.cs b/Octokit.Tests/Clients/StatisticsClientTests.cs index db3ea707..db6ab9a4 100644 --- a/Octokit.Tests/Clients/StatisticsClientTests.cs +++ b/Octokit.Tests/Clients/StatisticsClientTests.cs @@ -25,14 +25,12 @@ namespace Octokit.Tests.Clients { var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/contributors", UriKind.Relative); - var connection = Substitute.For(); var client = Substitute.For(); - client.Connection.Returns(connection); var statisticsClient = new StatisticsClient(client); statisticsClient.GetContributors("username","repositoryName"); - connection.Received().GetAsync>(expectedEndPoint); + client.Received().GetQueuedOperation>(expectedEndPoint); } [Fact] @@ -57,14 +55,12 @@ namespace Octokit.Tests.Clients { var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/commit_activity", UriKind.Relative); - var connection = Substitute.For(); var client = Substitute.For(); - client.Connection.Returns(connection); var statisticsClient = new StatisticsClient(client); statisticsClient.GetCommitActivityForTheLastYear("username", "repositoryName"); - connection.Received().GetAsync>(expectedEndPoint); + client.Received().GetQueuedOperation>(expectedEndPoint); } [Fact] @@ -89,14 +85,12 @@ namespace Octokit.Tests.Clients { var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/code_frequency", UriKind.Relative); - var connection = Substitute.For(); var client = Substitute.For(); - client.Connection.Returns(connection); var statisticsClient = new StatisticsClient(client); statisticsClient.GetAdditionsAndDeletionsPerWeek("username", "repositoryName"); - connection.Received().GetAsync>(expectedEndPoint); + client.Received().GetQueuedOperation>(expectedEndPoint); } [Fact] @@ -121,14 +115,12 @@ namespace Octokit.Tests.Clients { var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/participation", UriKind.Relative); - var connection = Substitute.For(); var client = Substitute.For(); - client.Connection.Returns(connection); var statisticsClient = new StatisticsClient(client); statisticsClient.GetCommitCountsPerWeek("username", "repositoryName"); - connection.Received().GetAsync(expectedEndPoint); + client.Received().GetQueuedOperation(expectedEndPoint); } [Fact] @@ -153,14 +145,12 @@ namespace Octokit.Tests.Clients { var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/punch_card", UriKind.Relative); - var connection = Substitute.For(); var client = Substitute.For(); - client.Connection.Returns(connection); var statisticsClient = new StatisticsClient(client); statisticsClient.GetCommitPerHour("username", "repositoryName"); - connection.Received().GetAsync>(expectedEndPoint); + client.Received().GetQueuedOperation>(expectedEndPoint); } [Fact] diff --git a/Octokit/Clients/StatisticsClient.cs b/Octokit/Clients/StatisticsClient.cs index e37c313b..de9009b8 100644 --- a/Octokit/Clients/StatisticsClient.cs +++ b/Octokit/Clients/StatisticsClient.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Net; +using System.Collections.Generic; using System.Threading.Tasks; namespace Octokit @@ -27,7 +25,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); var endpoint = "/repos/{0}/{1}/stats/contributors".FormatUri(owner, repositoryName); - return await WaitForResponse>(endpoint); + return await ApiConnection.GetQueuedOperation>(endpoint); } /// @@ -42,7 +40,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); var endpoint = "/repos/{0}/{1}/stats/commit_activity".FormatUri(owner, repositoryName); - return await WaitForResponse>(endpoint); + return await ApiConnection.GetQueuedOperation>(endpoint); } /// @@ -57,7 +55,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); var endpoint = "/repos/{0}/{1}/stats/code_frequency".FormatUri(owner, repositoryName); - return await WaitForResponse>(endpoint); + return await ApiConnection.GetQueuedOperation>(endpoint); } /// @@ -72,7 +70,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); var endpoint = "/repos/{0}/{1}/stats/participation".FormatUri(owner, repositoryName); - return await WaitForResponse(endpoint); + return await ApiConnection.GetQueuedOperation(endpoint); } /// @@ -87,18 +85,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); var endpoint = "/repos/{0}/{1}/stats/punch_card".FormatUri(owner, repositoryName); - return await WaitForResponse>(endpoint); - } - - async Task WaitForResponse(Uri endpoint) - { - var response = await Connection.GetAsync(endpoint); - - if (response.StatusCode == HttpStatusCode.Accepted) - { - return await WaitForResponse(endpoint); - } - return response.BodyAsObject; + return await ApiConnection.GetQueuedOperation>(endpoint); } } } \ No newline at end of file diff --git a/Octokit/Http/ApiConnection.cs b/Octokit/Http/ApiConnection.cs index c24baea4..86bac2e3 100644 --- a/Octokit/Http/ApiConnection.cs +++ b/Octokit/Http/ApiConnection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; using Octokit.Internal; @@ -246,6 +247,24 @@ namespace Octokit return Connection.DeleteAsync(uri); } + public async Task GetQueuedOperation(Uri uri) + { + Ensure.ArgumentNotNull(uri, "uri"); + + var response = await Connection.GetAsync(uri); + + if (response.StatusCode == HttpStatusCode.Accepted) + { + return await GetQueuedOperation(uri); + } + + if (response.StatusCode == HttpStatusCode.OK) + { + return response.BodyAsObject; + } + throw new ApiException("Queued Operations expect status codes of Accepted or OK.",response.StatusCode); + } + async Task> GetPage( Uri uri, IDictionary parameters, diff --git a/Octokit/Http/IApiConnection.cs b/Octokit/Http/IApiConnection.cs index 7b6647d9..ef2541c4 100644 --- a/Octokit/Http/IApiConnection.cs +++ b/Octokit/Http/IApiConnection.cs @@ -144,5 +144,7 @@ namespace Octokit /// URI of the API resource to delete /// A for the request's execution. Task Delete(Uri uri); + + Task GetQueuedOperation(Uri uri); } } \ No newline at end of file