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(())
|
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>(
|
async fn do_fetch<T, U, GraphQLFn, RestfulFn, GraphQLFut, RestfulFut>(
|
||||||
&self,
|
&self,
|
||||||
graphql_func: GraphQLFn,
|
graphql_func: GraphQLFn,
|
||||||
|
@ -164,25 +172,18 @@ impl GhApiClient {
|
||||||
{
|
{
|
||||||
self.check_retry_after()?;
|
self.check_retry_after()?;
|
||||||
|
|
||||||
if self.0.is_auth_token_valid.load(Relaxed) {
|
if let Some(auth_token) = self.get_auth_token() {
|
||||||
if let Some(auth_token) = self.0.auth_token.as_deref() {
|
match graphql_func(&self.0.client, data, auth_token).await {
|
||||||
match graphql_func(&self.0.client, data, auth_token).await {
|
Err(GhApiError::Unauthorized) => {
|
||||||
Err(GhApiError::Unauthorized) => {
|
self.0.is_auth_token_valid.store(false, Relaxed);
|
||||||
self.0.is_auth_token_valid.store(false, Relaxed);
|
|
||||||
}
|
|
||||||
res => return res.map_err(|err| err.context("GraphQL API")),
|
|
||||||
}
|
}
|
||||||
|
res => return res.map_err(|err| err.context("GraphQL API")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match restful_func(&self.0.client, data, self.0.auth_token.as_deref()).await {
|
restful_func(&self.0.client, data, None)
|
||||||
Err(GhApiError::Unauthorized) => {
|
.await
|
||||||
self.0.is_auth_token_valid.store(false, Relaxed);
|
.map_err(|err| err.context("Restful API"))
|
||||||
restful_func(&self.0.client, data, None).await
|
|
||||||
}
|
|
||||||
res => res,
|
|
||||||
}
|
|
||||||
.map_err(|err| err.context("Restful API"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `Ok(Some(api_artifact_url))` if exists.
|
/// Return `Ok(Some(api_artifact_url))` if exists.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue