Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit b6c4df5

Browse files
Check RemainingRequests for both GraphQL API & REST API
1 parent 73d27be commit b6c4df5

5 files changed

Lines changed: 100 additions & 67 deletions

File tree

GitHubReadmeWebTrends.Common/Services/GitHubApiService.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Net;
3+
using System.Threading.Tasks;
4+
using Refit;
5+
6+
namespace GitHubReadmeWebTrends.Common
7+
{
8+
public class GitHubApiStatusService : GitHubApiStatus.GitHubApiStatusService
9+
{
10+
readonly GitHubRestApiService _gitHubRestApiService;
11+
readonly GitHubGraphQLApiService _gitHubGraphQLApiService;
12+
13+
public GitHubApiStatusService(GitHubRestApiService gitHubRestApiService, GitHubGraphQLApiService gitHubGraphQLApiService) =>
14+
(_gitHubRestApiService, _gitHubGraphQLApiService) = (gitHubRestApiService, gitHubGraphQLApiService);
15+
16+
public bool HasReachedMaximimApiCallLimit(in Exception exception) => exception switch
17+
{
18+
ApiException apiException when apiException.StatusCode is HttpStatusCode.Forbidden => HasReachedMaximimApiCallLimit(apiException.Headers),
19+
GraphQLException graphQLException => HasReachedMaximimApiCallLimit(graphQLException.ResponseHeaders),
20+
_ => false
21+
};
22+
23+
public async Task<(int gitHubRestApiRequestsRemaining, int gitHubGraphQLApiRequestsRemaining)> GetRemaininRequestCount()
24+
{
25+
int gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining;
26+
try
27+
{
28+
var restApiResponse = await _gitHubRestApiService.GetResponseMessage().ConfigureAwait(false);
29+
var graphQLApiResponse = await _gitHubGraphQLApiService.GetViewerInformationResponse().ConfigureAwait(false);
30+
31+
gitHubRestApiRequestsRemaining = GetRemainingRequestCount(restApiResponse.Headers);
32+
gitHubGraphQLApiRequestsRemaining = GetRemainingRequestCount(graphQLApiResponse.Headers);
33+
}
34+
catch (Exception e) when (HasReachedMaximimApiCallLimit(e))
35+
{
36+
gitHubRestApiRequestsRemaining = gitHubGraphQLApiRequestsRemaining = 0;
37+
}
38+
39+
return (gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining);
40+
}
41+
}
42+
}

GitHubReadmeWebTrends.Common/Services/GitHubGraphQLApiService.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ public class GitHubGraphQLApiService
1212

1313
public GitHubGraphQLApiService(IGitHubGraphQLApiClient gitHubGraphQLApiClient) => _gitHubGraphQLApiClient = gitHubGraphQLApiClient;
1414

15-
public Task<ApiResponse<GraphQLResponse<CreateBranchResponseModel>>> CreateBranch(string repositoryId, string repositoryName, string branchOid, Guid guid) =>
16-
ExecuteGraphQLRequest(_gitHubGraphQLApiClient.CreateBranchMutation(new CreateBranchMutationContent(repositoryId, repositoryName, branchOid, guid)));
1715

18-
public Task<ApiResponse<GraphQLResponse<RepositoryConnectionResponse>>> GetRepository(string repositoryOwner, string repositoryName) =>
19-
ExecuteGraphQLRequest(_gitHubGraphQLApiClient.RepositoryConnectionQuery(new RepositoryConnectionQueryContent(repositoryOwner, repositoryName)));
16+
public Task<CreateBranchResponseModel> CreateBranch(string repositoryId, string repositoryName, string branchOid, Guid guid) =>
17+
GetGraphQLResponseData(CreateBranchResponse(repositoryId, repositoryName, branchOid, guid));
2018

21-
public Task<ApiResponse<GraphQLResponse<CreatePullRequestResponseModel>>> CreatePullRequest(in string repositoryId, in string baseRefName, in string headRefName, in string title, in string body, in Guid clientMutationId, in bool maintainerCanModify = true, in bool draft = false) =>
19+
public Task<RepositoryConnectionResponse> GetRepository(string repositoryOwner, string repositoryName) =>
20+
GetGraphQLResponseData(GetRepositoryResponse(repositoryOwner, repositoryName));
21+
22+
public Task<CreatePullRequestResponseModel> CreatePullRequest(in string repositoryId, in string baseRefName, in string headRefName, in string title, in string body, in Guid clientMutationId, in bool maintainerCanModify = true, in bool draft = false) =>
23+
GetGraphQLResponseData(CreatePullRequestResponse(repositoryId, baseRefName, headRefName, title, body, clientMutationId, maintainerCanModify, draft));
24+
25+
public Task<GitHubViewerResponse> GetViewerInformation() =>
26+
GetGraphQLResponseData(GetViewerInformationResponse());
27+
28+
public Task<ApiResponse<GraphQLResponse<CreateBranchResponseModel>>> CreateBranchResponse(string repositoryId, string repositoryName, string branchOid, Guid guid) =>
29+
ExecuteGraphQLRequest(_gitHubGraphQLApiClient.CreateBranchMutation(new CreateBranchMutationContent(repositoryId, repositoryName, branchOid, guid)));
30+
31+
public Task<ApiResponse<GraphQLResponse<RepositoryConnectionResponse>>> GetRepositoryResponse(string repositoryOwner, string repositoryName) =>
32+
ExecuteGraphQLRequest(_gitHubGraphQLApiClient.RepositoryConnectionQuery(new RepositoryConnectionQueryContent(repositoryOwner, repositoryName)));
33+
34+
public Task<ApiResponse<GraphQLResponse<CreatePullRequestResponseModel>>> CreatePullRequestResponse(in string repositoryId, in string baseRefName, in string headRefName, in string title, in string body, in Guid clientMutationId, in bool maintainerCanModify = true, in bool draft = false) =>
2235
ExecuteGraphQLRequest(_gitHubGraphQLApiClient.CreatePullRequestMutation(new CreatePullRequestMutationContent(repositoryId, baseRefName, headRefName, title, body, clientMutationId, maintainerCanModify, draft)));
2336

24-
public Task<ApiResponse<GraphQLResponse<GitHubViewerResponse>>> GetViewerInformation() => ExecuteGraphQLRequest(_gitHubGraphQLApiClient.ViewerLoginQuery(new ViewerLoginQueryContent()));
37+
public Task<ApiResponse<GraphQLResponse<GitHubViewerResponse>>> GetViewerInformationResponse() =>
38+
ExecuteGraphQLRequest(_gitHubGraphQLApiClient.ViewerLoginQuery(new ViewerLoginQueryContent()));
2539

2640
public async IAsyncEnumerable<IEnumerable<Repository>> GetRepositories(string repositoryOwner, int numberOfRepositoriesPerRequest = 100)
2741
{
@@ -36,6 +50,12 @@ public async IAsyncEnumerable<IEnumerable<Repository>> GetRepositories(string re
3650
while (repositoryConnection?.PageInfo?.HasNextPage is true);
3751
}
3852

53+
async Task<T> GetGraphQLResponseData<T>(Task<ApiResponse<GraphQLResponse<T>>> graphQLRequestTask)
54+
{
55+
var response = await ExecuteGraphQLRequest(graphQLRequestTask).ConfigureAwait(false);
56+
return response.Content.Data;
57+
}
58+
3959
async Task<ApiResponse<GraphQLResponse<T>>> ExecuteGraphQLRequest<T>(Task<ApiResponse<GraphQLResponse<T>>> graphQLRequestTask)
4060
{
4161
var response = await graphQLRequestTask.ConfigureAwait(false);

GitHubReadmeWebTrends.Common/Services/StartupService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void ConfigureServices(in IServiceCollection services)
3939
services.AddSingleton<GitHubRestApiService>();
4040
services.AddSingleton<GitHubGraphQLApiService>();
4141
services.AddSingleton<CloudAdvocateService>();
42-
services.AddSingleton<GitHubApiExceptionService>();
42+
services.AddSingleton<GitHubApiStatusService>();
4343

4444
static TimeSpan sleepDurationProvider(int attemptNumber) => TimeSpan.FromSeconds(Math.Pow(2, attemptNumber));
4545

GitHubReadmeWebTrends.Functions/Functions/GetReadmeFunction.cs

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ public class GetReadmeFunction
1818
readonly CloudQueueClient _cloudQueueClient;
1919
readonly GitHubRestApiService _gitHubRestApiService;
2020
readonly GitHubGraphQLApiService _gitHubGraphQLApiService;
21-
readonly GitHubApiExceptionService _gitHubApiExceptionService;
21+
readonly GitHubApiStatusService _gitHubApiStatusService;
2222

2323
public GetReadmeFunction(CloudQueueClient cloudQueueClient,
2424
IHttpClientFactory httpClientFactory,
2525
GitHubRestApiService gitHubApiService,
26-
GitHubGraphQLApiService gitHubGraphQLApiService,
27-
GitHubApiExceptionService gitHubApiExceptionService)
26+
GitHubApiStatusService gitHubApiStatusService,
27+
GitHubGraphQLApiService gitHubGraphQLApiService)
2828
{
2929
_httpClient = httpClientFactory.CreateClient();
3030

3131
_cloudQueueClient = cloudQueueClient;
3232
_gitHubRestApiService = gitHubApiService;
33+
_gitHubApiStatusService = gitHubApiStatusService;
3334
_gitHubGraphQLApiService = gitHubGraphQLApiService;
34-
_gitHubApiExceptionService = gitHubApiExceptionService;
3535
}
3636

3737
[FunctionName(nameof(GetReadmeQueueTriggerFunction))]
@@ -41,52 +41,39 @@ public async Task GetReadmeQueueTriggerFunction([QueueTrigger(QueueConstants.Rep
4141
{
4242
const int minimumApiRequests = 2000;
4343

44-
int gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining;
45-
4644
log.LogInformation($"{nameof(GetReadmeFunction)} Stared");
4745

4846
var (repository, gitHubUser) = data;
4947

50-
try
51-
{
52-
var restApiResponse = await _gitHubRestApiService.GetResponseMessage().ConfigureAwait(false);
53-
var graphQLApiResponse = await _gitHubGraphQLApiService.GetViewerInformation().ConfigureAwait(false);
48+
var (gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining) = await _gitHubApiStatusService.GetRemaininRequestCount().ConfigureAwait(false);
5449

55-
gitHubRestApiRequestsRemaining = _gitHubApiExceptionService.GetRemainingRequestCount(restApiResponse.Headers);
56-
gitHubGraphQLApiRequestsRemaining = _gitHubApiExceptionService.GetRemainingRequestCount(graphQLApiResponse.Headers);
57-
}
58-
catch (Exception e) when (_gitHubApiExceptionService.HasReachedMaximimApiCallLimit(e))
59-
{
60-
gitHubRestApiRequestsRemaining = gitHubGraphQLApiRequestsRemaining = 0;
61-
}
62-
6350
log.LogInformation($"{nameof(gitHubRestApiRequestsRemaining)}: {gitHubRestApiRequestsRemaining}");
64-
log.LogInformation($"{nameof(gitHubGraphQLApiRequestsRemaining)}: {gitHubGraphQLApiRequestsRemaining}");
65-
66-
try
51+
log.LogInformation($"{nameof(gitHubGraphQLApiRequestsRemaining)}: {gitHubGraphQLApiRequestsRemaining}");
52+
53+
54+
// The GitHub API Limits requests to 5,0000 per hour https://docs.github.com/en/free-pro-team@latest/rest#rate-limiting
55+
// If the API Limit is approaching, output to RemainingRepositoriesQueue, where it will be handled by GetReadmeTimerTriggerFunction which runs once an hour
56+
// Otherwise, process the data and place it on VerifyWebTrendsQueue
57+
if (gitHubRestApiRequestsRemaining < minimumApiRequests
58+
|| gitHubGraphQLApiRequestsRemaining < minimumApiRequests)
6759
{
68-
69-
// The GitHub API Limits requests to 5,0000 per hour https://docs.github.com/en/free-pro-team@latest/rest#rate-limiting
70-
// If the API Limit is approaching, output to RemainingRepositoriesQueue, where it will be handled by GetReadmeTimerTriggerFunction which runs once an hour
71-
// Otherwise, process the data and place it on VerifyWebTrendsQueue
72-
if (gitHubRestApiRequestsRemaining < minimumApiRequests
73-
|| gitHubGraphQLApiRequestsRemaining < minimumApiRequests)
74-
{
75-
log.LogInformation($"Maximum API Requests Reached");
76-
77-
remainingRepositoriesData.Add(data);
78-
79-
log.LogInformation($"Added Data to RemainingRepositoriesQueue");
80-
}
81-
else
82-
{
83-
await RetrieveReadme(repository, gitHubUser, log, completedRepositoriesData).ConfigureAwait(false);
84-
}
60+
log.LogInformation($"Maximum API Requests Reached");
61+
62+
remainingRepositoriesData.Add(data);
63+
64+
log.LogInformation($"Added Data to RemainingRepositoriesQueue");
8565
}
86-
catch (Refit.ApiException e) when (e.StatusCode is System.Net.HttpStatusCode.NotFound)
66+
else
8767
{
88-
//If a Readme doesn't exist, GitHubApiService.GetReadme will return a 404 Not Found response
89-
}
68+
try
69+
{
70+
await RetrieveReadme(repository, gitHubUser, log, completedRepositoriesData).ConfigureAwait(false);
71+
}
72+
catch (Refit.ApiException e) when (e.StatusCode is System.Net.HttpStatusCode.NotFound)
73+
{
74+
//If a Readme doesn't exist, GitHubApiService.GetReadme will return a 404 Not Found response
75+
}
76+
}
9077

9178
log.LogInformation($"{nameof(GetReadmeFunction)} Completed");
9279
}
@@ -116,9 +103,10 @@ public async Task GetReadmeTimerTriggerFunction([TimerTrigger(_runEveryHour, Run
116103

117104
try
118105
{
119-
var response = await _gitHubRestApiService.GetResponseMessage().ConfigureAwait(false);
106+
var (gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining) = await _gitHubApiStatusService.GetRemaininRequestCount().ConfigureAwait(false);
120107

121-
if (GitHubApiExceptionService.GetNumberOfApiRequestsRemaining(response.Headers) >= 2000)
108+
if (gitHubRestApiRequestsRemaining > 2000
109+
&& gitHubGraphQLApiRequestsRemaining > 2000)
122110
{
123111
await RetrieveReadme(repository, gitHubUser, log, completedRepositoriesData).ConfigureAwait(false);
124112
await remainingRepositoriesQueue.DeleteMessageAsync(queueMessage).ConfigureAwait(false);

0 commit comments

Comments
 (0)