mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 14:28:42 +00:00
Fix rate limit checking in GhApiClient
(#1725)
* Fix rate limit checking in `GhApiClient` - Mv logic into `binstalk_downloader` - Check for `RETRY_AFTER` and `x-ratelimit-remaining` on any status code Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix clippy lint Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> --------- Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
3aae883467
commit
238e0f6318
3 changed files with 40 additions and 43 deletions
|
@ -299,14 +299,13 @@ impl GhApiClient {
|
|||
.send(false)
|
||||
.await?;
|
||||
|
||||
match check_http_status_and_header(&response) {
|
||||
match check_http_status_and_header(response) {
|
||||
Err(GhApiError::Unauthorized) => {
|
||||
self.0.is_auth_token_valid.store(false, Relaxed);
|
||||
Err(GhApiError::Unauthorized)
|
||||
}
|
||||
res => res?,
|
||||
res => res.map(Download::from_response),
|
||||
}
|
||||
|
||||
Ok(Download::from_response(response))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{fmt::Debug, future::Future, sync::OnceLock, time::Duration};
|
||||
use std::{fmt::Debug, future::Future, sync::OnceLock};
|
||||
|
||||
use binstalk_downloader::remote::{self, Response, Url};
|
||||
use compact_str::CompactString;
|
||||
|
@ -18,28 +18,12 @@ pub(super) fn percent_decode_http_url_path(input: &str) -> CompactString {
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn check_http_status_and_header(response: &Response) -> Result<(), GhApiError> {
|
||||
let headers = response.headers();
|
||||
|
||||
pub(super) fn check_http_status_and_header(response: Response) -> Result<Response, GhApiError> {
|
||||
match response.status() {
|
||||
remote::StatusCode::FORBIDDEN
|
||||
if headers
|
||||
.get("x-ratelimit-remaining")
|
||||
.map(|val| val == "0")
|
||||
.unwrap_or(false) =>
|
||||
{
|
||||
Err(GhApiError::RateLimit {
|
||||
retry_after: headers.get("x-ratelimit-reset").and_then(|value| {
|
||||
let secs = value.to_str().ok()?.parse().ok()?;
|
||||
Some(Duration::from_secs(secs))
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
remote::StatusCode::UNAUTHORIZED => Err(GhApiError::Unauthorized),
|
||||
remote::StatusCode::NOT_FOUND => Err(GhApiError::NotFound),
|
||||
|
||||
_ => Ok(()),
|
||||
_ => Ok(response.error_for_status()?),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,9 +57,7 @@ where
|
|||
.send(false);
|
||||
|
||||
async move {
|
||||
let response = future.await?;
|
||||
|
||||
check_http_status_and_header(&response)?;
|
||||
let response = check_http_status_and_header(future.await?)?;
|
||||
|
||||
Ok(response.json().await?)
|
||||
}
|
||||
|
@ -127,8 +109,7 @@ where
|
|||
});
|
||||
|
||||
async move {
|
||||
let response = res?.await?;
|
||||
check_http_status_and_header(&response)?;
|
||||
let response = check_http_status_and_header(res?.await?)?;
|
||||
|
||||
let mut response: GraphQLResponse<T> = response.json().await?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue