diff --git a/README.md b/README.md index 2aa5858..0a73c65 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ psvm -v "1.4.0" -c psvm -v "1.6.0" -O ``` -> Listing all available Polkadot SDK versions requires querying the GitHub API, so your IP may be rate-limited. If a rate limit is reached, the tool will fallback to the GitHub CLI to list the versions. Ensure you have the GitHub CLI installed and authenticated to avoid any issue. +> Listing all available Polkadot SDK versions requires querying the GitHub API, so your IP may be rate-limited. If a rate limit is reached, the tool will fallback to the GitHub CLI to list the versions. Ensure you have the GitHub CLI installed and authenticated to avoid any issue, or you can alternatively set the `GITHUB_TOKEN` environment variable to your GitHub API token. ## Using as a Library diff --git a/src/versions.rs b/src/versions.rs index d75a426..d4108e0 100644 --- a/src/versions.rs +++ b/src/versions.rs @@ -119,6 +119,19 @@ pub async fn get_polkadot_sdk_versions() -> Result, Box Result { + let mut builder = reqwest::Client::new() + .get(url) + .header("User-Agent", "reqwest") + .header("Accept", "application/vnd.github.v3+json"); + + if let Ok(token) = std::env::var("GITHUB_TOKEN") { + builder = builder.header("Authorization", format!("Bearer {}", token)) + }; + + builder.send().await +} + /// Fetches a list of stable tag versions for the Polkadot SDK from GitHub. /// /// This function queries GitHub's API to retrieve tags for the Polkadot SDK, @@ -138,13 +151,7 @@ pub async fn get_stable_tag_versions() -> Result, Box(&content) @@ -332,12 +333,7 @@ pub async fn get_version_mapping( source: &str, ) -> Result, Box> { let url = version_to_url(base_url, version, source); - let response = reqwest::Client::new() - .get(&url) - .header("User-Agent", "reqwest") - .header("Accept", "application/vnd.github.v3+json") - .send() - .await?; + let response = github_query(&url).await?; let content = match response.error_for_status() { Ok(response) => response.text().await?, @@ -495,13 +491,7 @@ pub async fn get_release_branches_versions( for page in 1..100 { // currently there's 5 pages, so 100 should be enough - let response = reqwest::Client::new() - .get(format!("{}{}", repository_info.branches_url, page)) - .header("User-Agent", "reqwest") - .header("Accept", "application/vnd.github.v3+json") - .send() - .await?; - + let response = github_query(&format!("{}{}", repository_info.branches_url, page)).await?; let output = if response.status().is_success() { response.text().await? } else {