Skip to content

Commit

Permalink
feat: add user-agent into requests
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed Mar 4, 2024
1 parent 791a3e7 commit 9e6cc33
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::time::Instant;

use crate::user_agent::APP_USER_AGENT;

pub fn download_file(url: &str, file_path: &Path, redirect_path: &Path) -> Result<()> {
if let Some(dir) = file_path.parent() {
fs::create_dir_all(dir)?;
Expand All @@ -20,6 +22,7 @@ pub fn download_file(url: &str, file_path: &Path, redirect_path: &Path) -> Resul
let file_size = file.metadata()?.len();

let client = Client::builder()
.user_agent(APP_USER_AGENT)
.timeout(std::time::Duration::from_secs(30))
.build()?;
let mut response = client
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod reader_with_bytes;
mod reader_with_progress;
mod sql;
mod unpack;
mod user_agent;
mod utils;

use checksum::*;
Expand Down
5 changes: 5 additions & 0 deletions src/user_agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub static APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
);
3 changes: 3 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use reqwest::{blocking::Client, redirect};
use std::{env, path::PathBuf};
use url::Url;

use crate::user_agent::APP_USER_AGENT;

pub fn strip_trailing_newline(input: &str) -> &str {
input.trim_end()
}
Expand Down Expand Up @@ -73,6 +75,7 @@ fn extract_number_from_url(url: &Url) -> Result<u64> {

pub fn fetch_latest_available_layer(download_url: &Url, go_version: &str) -> Result<u64> {
let client = Client::builder()
.user_agent(APP_USER_AGENT)
.redirect(redirect::Policy::none())
.timeout(std::time::Duration::from_secs(30))
.build()?;
Expand Down

0 comments on commit 9e6cc33

Please sign in to comment.