This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check RemainingRequests for both GraphQL API & REST API
- Loading branch information
Showing
5 changed files
with
100 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
GitHubReadmeWebTrends.Common/Services/GitHubApiStatusService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Refit; | ||
|
||
namespace GitHubReadmeWebTrends.Common | ||
{ | ||
public class GitHubApiStatusService : GitHubApiStatus.GitHubApiStatusService | ||
{ | ||
readonly GitHubRestApiService _gitHubRestApiService; | ||
readonly GitHubGraphQLApiService _gitHubGraphQLApiService; | ||
|
||
public GitHubApiStatusService(GitHubRestApiService gitHubRestApiService, GitHubGraphQLApiService gitHubGraphQLApiService) => | ||
(_gitHubRestApiService, _gitHubGraphQLApiService) = (gitHubRestApiService, gitHubGraphQLApiService); | ||
|
||
public bool HasReachedMaximimApiCallLimit(in Exception exception) => exception switch | ||
{ | ||
ApiException apiException when apiException.StatusCode is HttpStatusCode.Forbidden => HasReachedMaximimApiCallLimit(apiException.Headers), | ||
GraphQLException graphQLException => HasReachedMaximimApiCallLimit(graphQLException.ResponseHeaders), | ||
_ => false | ||
}; | ||
|
||
public async Task<(int gitHubRestApiRequestsRemaining, int gitHubGraphQLApiRequestsRemaining)> GetRemaininRequestCount() | ||
{ | ||
int gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining; | ||
try | ||
{ | ||
var restApiResponse = await _gitHubRestApiService.GetResponseMessage().ConfigureAwait(false); | ||
var graphQLApiResponse = await _gitHubGraphQLApiService.GetViewerInformationResponse().ConfigureAwait(false); | ||
|
||
gitHubRestApiRequestsRemaining = GetRemainingRequestCount(restApiResponse.Headers); | ||
gitHubGraphQLApiRequestsRemaining = GetRemainingRequestCount(graphQLApiResponse.Headers); | ||
} | ||
catch (Exception e) when (HasReachedMaximimApiCallLimit(e)) | ||
{ | ||
gitHubRestApiRequestsRemaining = gitHubGraphQLApiRequestsRemaining = 0; | ||
} | ||
|
||
return (gitHubRestApiRequestsRemaining, gitHubGraphQLApiRequestsRemaining); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters