Skip to content

Commit

Permalink
feat(github): accept GITHUB_TOKEN for github downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Nov 14, 2024
1 parent d006985 commit d6c2b57
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/misc/download.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use std::{fs::Permissions, os::unix::fs::PermissionsExt, path::Path};
use std::{env, fs::Permissions, os::unix::fs::PermissionsExt, path::Path};

use anyhow::{Context, Result};
use chrono::Utc;
use futures::StreamExt;
use indicatif::ProgressBar;
use regex::Regex;
use reqwest::Url;
use reqwest::{
header::{HeaderMap, AUTHORIZATION, USER_AGENT},
Url,
};
use serde::{Deserialize, Serialize};
use tokio::{
fs::{self, File},
io::{AsyncReadExt, AsyncWriteExt, BufReader},
};
use tracing::{error, info};
use tracing::{error, info, trace};

use crate::{
core::{
Expand Down Expand Up @@ -145,9 +148,15 @@ async fn download(url: &str, output: Option<String>) -> Result<()> {
async fn fetch_github_releases(user_repo: &str) -> Result<Vec<GithubRelease>> {
let client = reqwest::Client::new();
let url = format!("https://api.github.com/repos/{}/releases", user_repo);
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, "pkgforge/soar".parse()?);
if let Ok(token) = env::var("GITHUB_TOKEN") {
trace!("Using Github token: {}", token);
headers.insert(AUTHORIZATION, format!("Bearer {}", token).parse()?);
};
let response = client
.get(&url)
.header("User-Agent", "rust-client") // GitHub API requires a user-agent header
.headers(headers)
.send()
.await
.context("Failed to fetch GitHub releases")?;
Expand Down

0 comments on commit d6c2b57

Please sign in to comment.