From cb418fda1169fd63f1b9e498787a0c22aed1e678 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Mon, 27 May 2024 23:37:01 +1000 Subject: [PATCH] Refactor: Make sure `'static` Future is returned To make it easier to create generic function Signed-off-by: Jiahao XU --- .../src/gh_api_client/common.rs | 57 ++++++++++++------- .../src/gh_api_client/release_artifacts.rs | 14 +++-- .../src/gh_api_client/repo_info.rs | 12 ++-- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/crates/binstalk-git-repo-api/src/gh_api_client/common.rs b/crates/binstalk-git-repo-api/src/gh_api_client/common.rs index 75442c24..29d56d5a 100644 --- a/crates/binstalk-git-repo-api/src/gh_api_client/common.rs +++ b/crates/binstalk-git-repo-api/src/gh_api_client/common.rs @@ -61,29 +61,43 @@ fn check_http_status_and_header(status: StatusCode, headers: &HeaderMap) -> Resu } } +fn get_api_endpoint() -> &'static Url { + static API_ENDPOINT: OnceLock = 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( client: &remote::Client, - path: String, + path: &[&str], auth_token: Option<&str>, ) -> impl Future> + Send + Sync + 'static where T: DeserializeOwned, { - let res = Url::parse(&format!("https://api.github.com/{path}")).map(|url| { - let mut request_builder = client - .get(url) - .header("Accept", "application/vnd.github+json") - .header("X-GitHub-Api-Version", "2022-11-28"); + let mut url = get_api_endpoint().clone(); - if let Some(auth_token) = auth_token { - request_builder = request_builder.bearer_auth(&auth_token); - } + url.path_segments_mut() + .expect("get_api_endpoint() should return a https url") + .extend(path); - request_builder.send(false) - }); + debug!("Getting restful API: {url}"); + + let mut request_builder = client + .get(url) + .header("Accept", "application/vnd.github+json") + .header("X-GitHub-Api-Version", "2022-11-28"); + + if let Some(auth_token) = auth_token { + request_builder = request_builder.bearer_auth(&auth_token); + } + + 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 = 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( @@ -121,15 +138,15 @@ pub(super) fn issue_graphql_query( 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); diff --git a/crates/binstalk-git-repo-api/src/gh_api_client/release_artifacts.rs b/crates/binstalk-git-repo-api/src/gh_api_client/release_artifacts.rs index 81ae0ec3..723349e6 100644 --- a/crates/binstalk-git-repo-api/src/gh_api_client/release_artifacts.rs +++ b/crates/binstalk-git-repo-api/src/gh_api_client/release_artifacts.rs @@ -76,12 +76,14 @@ fn fetch_release_artifacts_restful_api( ) -> impl Future> + 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, ) } diff --git a/crates/binstalk-git-repo-api/src/gh_api_client/repo_info.rs b/crates/binstalk-git-repo-api/src/gh_api_client/repo_info.rs index 2bb7e2ec..496d453c 100644 --- a/crates/binstalk-git-repo-api/src/gh_api_client/repo_info.rs +++ b/crates/binstalk-git-repo-api/src/gh_api_client/repo_info.rs @@ -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 { 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