Refactor: Make sure 'static Future is returned

To make it easier to create generic function

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2024-05-27 23:37:01 +10:00
parent c36145f2b4
commit cb418fda11
No known key found for this signature in database
GPG key ID: 76D1E687CA3C4928
3 changed files with 51 additions and 32 deletions

View file

@ -61,15 +61,30 @@ fn check_http_status_and_header(status: StatusCode, headers: &HeaderMap) -> Resu
}
}
fn get_api_endpoint() -> &'static Url {
static API_ENDPOINT: OnceLock<Url> = OnceLock::new();
API_ENDPOINT.get_or_init(|| {
Url::parse("https://api.github.com/").expect("Literal provided must be a valid url")
})
}
pub(super) fn issue_restful_api<T>(
client: &remote::Client,
path: String,
path: &[&str],
auth_token: Option<&str>,
) -> impl Future<Output = Result<T, GhApiError>> + Send + Sync + 'static
where
T: DeserializeOwned,
{
let res = Url::parse(&format!("https://api.github.com/{path}")).map(|url| {
let mut url = get_api_endpoint().clone();
url.path_segments_mut()
.expect("get_api_endpoint() should return a https url")
.extend(path);
debug!("Getting restful API: {url}");
let mut request_builder = client
.get(url)
.header("Accept", "application/vnd.github+json")
@ -79,11 +94,10 @@ where
request_builder = request_builder.bearer_auth(&auth_token);
}
request_builder.send(false)
});
let future = request_builder.send(false);
async move {
let response = res?.await?;
let response = future.await?;
check_http_status_and_header(response.status(), response.headers())?;
@ -105,12 +119,15 @@ struct GraphQLQuery {
query: String,
}
fn get_graphql_endpoint() -> &'static Url {
static GRAPHQL_ENDPOINT: OnceLock<Url> = OnceLock::new();
fn get_graphql_endpoint() -> Url {
let mut graphql_endpoint = get_api_endpoint().clone();
GRAPHQL_ENDPOINT.get_or_init(|| {
Url::parse("https://api.github.com/graphql").expect("Literal provided must be a valid url")
})
graphql_endpoint
.path_segments_mut()
.expect("get_api_endpoint() should return a https url")
.push("graphql");
graphql_endpoint
}
pub(super) fn issue_graphql_query<T>(
@ -121,15 +138,15 @@ pub(super) fn issue_graphql_query<T>(
where
T: DeserializeOwned,
{
let graphql_endpoint = get_graphql_endpoint();
let res = to_json_string(&GraphQLQuery { query })
.map_err(remote::Error::from)
.map(|graphql_query| {
let graphql_endpoint = get_graphql_endpoint();
debug!("Sending graphql query to {graphql_endpoint}: '{graphql_query}'");
let request_builder = client
.post(graphql_endpoint.clone(), graphql_query)
.post(graphql_endpoint, graphql_query)
.header("Accept", "application/vnd.github+json")
.bearer_auth(&auth_token);

View file

@ -76,12 +76,14 @@ fn fetch_release_artifacts_restful_api(
) -> impl Future<Output = Result<Artifacts, GhApiError>> + Send + Sync + 'static {
issue_restful_api(
client,
format!(
"repos/{owner}/{repo}/releases/tags/{tag}",
owner = percent_encode_http_url_path(owner),
repo = percent_encode_http_url_path(repo),
tag = percent_encode_http_url_path(tag),
),
&[
"repos",
&percent_encode_http_url_path(owner).to_compact_string(),
&percent_encode_http_url_path(repo).to_compact_string(),
"releases",
"tags",
&percent_encode_http_url_path(tag).to_compact_string(),
],
auth_token,
)
}

View file

@ -1,4 +1,4 @@
use compact_str::CompactString;
use compact_str::{CompactString, ToCompactString};
use serde::Deserialize;
use super::{
@ -38,11 +38,11 @@ async fn fetch_repo_info_restful_api(
) -> Result<RepoInfo, GhApiError> {
issue_restful_api(
client,
format!(
"repos/{owner}/{repo}",
owner = percent_encode_http_url_path(owner),
repo = percent_encode_http_url_path(repo),
),
&[
"repos",
&percent_encode_http_url_path(owner).to_compact_string(),
&percent_encode_http_url_path(repo).to_compact_string(),
],
auth_token,
)
.await