mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-17 08:06:38 +00:00
Optimize GhApiClient::do_fetch
: Avoid unnecessary restful API call
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
962a7d057c
commit
fc0222881c
1 changed files with 16 additions and 15 deletions
|
@ -150,6 +150,14 @@ impl GhApiClient {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn get_auth_token(&self) -> Option<&str> {
|
||||
if self.0.is_auth_token_valid.load(Relaxed) {
|
||||
self.0.auth_token.as_deref()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
async fn do_fetch<T, U, GraphQLFn, RestfulFn, GraphQLFut, RestfulFut>(
|
||||
&self,
|
||||
graphql_func: GraphQLFn,
|
||||
|
@ -164,8 +172,7 @@ impl GhApiClient {
|
|||
{
|
||||
self.check_retry_after()?;
|
||||
|
||||
if self.0.is_auth_token_valid.load(Relaxed) {
|
||||
if let Some(auth_token) = self.0.auth_token.as_deref() {
|
||||
if let Some(auth_token) = self.get_auth_token() {
|
||||
match graphql_func(&self.0.client, data, auth_token).await {
|
||||
Err(GhApiError::Unauthorized) => {
|
||||
self.0.is_auth_token_valid.store(false, Relaxed);
|
||||
|
@ -173,15 +180,9 @@ impl GhApiClient {
|
|||
res => return res.map_err(|err| err.context("GraphQL API")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match restful_func(&self.0.client, data, self.0.auth_token.as_deref()).await {
|
||||
Err(GhApiError::Unauthorized) => {
|
||||
self.0.is_auth_token_valid.store(false, Relaxed);
|
||||
restful_func(&self.0.client, data, None).await
|
||||
}
|
||||
res => res,
|
||||
}
|
||||
restful_func(&self.0.client, data, None)
|
||||
.await
|
||||
.map_err(|err| err.context("Restful API"))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue