Refactor: Return 'static future

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2024-05-15 23:43:45 +10:00
parent f9008d7c93
commit f90eea4235
No known key found for this signature in database
GPG key ID: 76D1E687CA3C4928
2 changed files with 91 additions and 70 deletions

View file

@ -1,4 +1,4 @@
use std::{sync::OnceLock, time::Duration}; use std::{future::Future, sync::OnceLock, time::Duration};
use binstalk_downloader::remote::{self, header::HeaderMap, StatusCode, Url}; use binstalk_downloader::remote::{self, header::HeaderMap, StatusCode, Url};
use compact_str::CompactString; use compact_str::CompactString;
@ -61,16 +61,17 @@ fn check_http_status_and_header(status: StatusCode, headers: &HeaderMap) -> Resu
} }
} }
pub(super) async fn issue_restful_api<T>( pub(super) fn issue_restful_api<T>(
client: &remote::Client, client: &remote::Client,
path: String, path: String,
auth_token: Option<&str>, auth_token: Option<&str>,
) -> Result<T, GhApiError> ) -> impl Future<Output = Result<T, GhApiError>> + Send + Sync + 'static
where where
T: DeserializeOwned, T: DeserializeOwned,
{ {
let res = Url::parse(&format!("https://api.github.com/{path}")).map(|url| {
let mut request_builder = client let mut request_builder = client
.get(Url::parse(&format!("https://api.github.com/{path}"))?) .get(url)
.header("Accept", "application/vnd.github+json") .header("Accept", "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"); .header("X-GitHub-Api-Version", "2022-11-28");
@ -78,12 +79,17 @@ where
request_builder = request_builder.bearer_auth(&auth_token); request_builder = request_builder.bearer_auth(&auth_token);
} }
let response = request_builder.send(false).await?; request_builder.send(false)
});
async move {
let response = res?.await?;
check_http_status_and_header(response.status(), response.headers())?; check_http_status_and_header(response.status(), response.headers())?;
Ok(response.json().await?) Ok(response.json().await?)
} }
}
#[derive(Deserialize)] #[derive(Deserialize)]
enum GraphQLResponse<T> { enum GraphQLResponse<T> {
@ -107,18 +113,19 @@ fn get_graphql_endpoint() -> &'static Url {
}) })
} }
pub(super) async fn issue_graphql_query<T>( pub(super) fn issue_graphql_query<T>(
client: &remote::Client, client: &remote::Client,
query: String, query: String,
auth_token: &str, auth_token: &str,
) -> Result<T, GhApiError> ) -> impl Future<Output = Result<T, GhApiError>> + Send + Sync + 'static
where where
T: DeserializeOwned, T: DeserializeOwned,
{ {
let graphql_endpoint = get_graphql_endpoint(); let graphql_endpoint = get_graphql_endpoint();
let graphql_query = to_json_string(&GraphQLQuery { query }).map_err(remote::Error::from)?; let res = to_json_string(&GraphQLQuery { query })
.map_err(remote::Error::from)
.map(|graphql_query| {
debug!("Sending graphql query to {graphql_endpoint}: '{graphql_query}'"); debug!("Sending graphql query to {graphql_endpoint}: '{graphql_query}'");
let request_builder = client let request_builder = client
@ -126,7 +133,11 @@ where
.header("Accept", "application/vnd.github+json") .header("Accept", "application/vnd.github+json")
.bearer_auth(&auth_token); .bearer_auth(&auth_token);
let response = request_builder.send(false).await?; request_builder.send(false)
});
async move {
let response = res?.await?;
check_http_status_and_header(response.status(), response.headers())?; check_http_status_and_header(response.status(), response.headers())?;
let response: GraphQLResponse<T> = response.json().await?; let response: GraphQLResponse<T> = response.json().await?;
@ -139,3 +150,4 @@ where
GraphQLResponse::Errors(errors) => Err(errors.into()), GraphQLResponse::Errors(errors) => Err(errors.into()),
} }
} }
}

View file

@ -2,11 +2,12 @@ use std::{
borrow::Borrow, borrow::Borrow,
collections::HashSet, collections::HashSet,
fmt, fmt,
future::Future,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
}; };
use binstalk_downloader::remote::{self}; use binstalk_downloader::remote::{self};
use compact_str::CompactString; use compact_str::{CompactString, ToCompactString};
use serde::Deserialize; use serde::Deserialize;
use super::{ use super::{
@ -65,14 +66,14 @@ impl Artifacts {
} }
} }
async fn fetch_release_artifacts_restful_api( fn fetch_release_artifacts_restful_api(
client: &remote::Client, client: &remote::Client,
GhRelease { GhRelease {
repo: GhRepo { owner, repo }, repo: GhRepo { owner, repo },
tag, tag,
}: &GhRelease, }: &GhRelease,
auth_token: Option<&str>, auth_token: Option<&str>,
) -> Result<Artifacts, GhApiError> { ) -> impl Future<Output = Result<Artifacts, GhApiError>> + Send + Sync + 'static {
issue_restful_api( issue_restful_api(
client, client,
format!( format!(
@ -83,7 +84,6 @@ async fn fetch_release_artifacts_restful_api(
), ),
auth_token, auth_token,
) )
.await
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -132,36 +132,44 @@ impl fmt::Display for FilterCondition {
} }
} }
async fn fetch_release_artifacts_graphql_api( fn fetch_release_artifacts_graphql_api(
client: &remote::Client, client: &remote::Client,
GhRelease { GhRelease {
repo: GhRepo { owner, repo }, repo: GhRepo { owner, repo },
tag, tag,
}: &GhRelease, }: &GhRelease,
auth_token: &str, auth_token: &str,
) -> Result<Artifacts, GhApiError> { ) -> impl Future<Output = Result<Artifacts, GhApiError>> + Send + Sync + 'static {
let client = client.clone();
let auth_token = auth_token.to_compact_string();
let base_query_prefix = format!(
r#"
query {{
repository(owner:"{owner}",name:"{repo}") {{
release(tagName:"{tag}") {{"#
);
let base_query_suffix = r#"
nodes { name url }
pageInfo { endCursor hasNextPage }
}}}}"#
.trim();
async move {
let mut artifacts = Artifacts::default(); let mut artifacts = Artifacts::default();
let mut cond = FilterCondition::Init; let mut cond = FilterCondition::Init;
let base_query_prefix = base_query_prefix.trim();
loop { loop {
let query = format!( let query = format!(
r#" r#"
query {{ {base_query_prefix}
repository(owner:"{owner}",name:"{repo}") {{
release(tagName:"{tag}") {{
releaseAssets({cond}) {{ releaseAssets({cond}) {{
nodes {{ {base_query_suffix}"#
name
url
}}
pageInfo {{ endCursor hasNextPage }}
}}
}}
}}
}}"#
); );
let data: GraphQLData = issue_graphql_query(client, query, auth_token).await?; let data: GraphQLData = issue_graphql_query(&client, query, &auth_token).await?;
let assets = data let assets = data
.repository .repository
@ -185,6 +193,7 @@ query {{
} }
} }
} }
}
pub(super) async fn fetch_release_artifacts( pub(super) async fn fetch_release_artifacts(
client: &remote::Client, client: &remote::Client,