User-Agent для reqwest теперь устанавливается с помощью переменной окружения.

This commit is contained in:
2025-04-17 22:41:42 +04:00
parent 65376e75f7
commit 9a517519db
5 changed files with 40 additions and 29 deletions

View File

@@ -4,7 +4,7 @@ use crate::app_state::Schedule;
use crate::parser::parse_xls;
use crate::routes::schedule::schema::CacheStatus;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::xls_downloader::interface::XLSDownloader;
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
use actix_web::web::Json;
use actix_web::{patch, web};
use chrono::Utc;
@@ -60,16 +60,18 @@ pub async fn update_download_url(
}
},
Err(error) => {
eprintln!("Unknown url provided {}", data.url);
eprintln!("{:?}", error);
if let FetchError::Unknown(error) = error {
sentry::capture_error(&error);
}
ErrorCode::DownloadFailed.into_response()
}
}
}
Err(error) => {
eprintln!("Unknown url provided {}", data.url);
eprintln!("{:?}", error);
if let FetchError::Unknown(error) = error {
sentry::capture_error(&error);
}
ErrorCode::FetchFailed.into_response()
}

View File

@@ -1,11 +1,13 @@
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
use chrono::{DateTime, Utc};
use std::env;
pub struct BasicXlsDownloader {
pub url: Option<String>,
user_agent: String,
}
async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchResult {
async fn fetch_specified(url: &String, user_agent: &String, head: bool) -> FetchResult {
let client = reqwest::Client::new();
let response = if head {
@@ -13,7 +15,7 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
} else {
client.get(url)
}
.header("User-Agent", user_agent)
.header("User-Agent", user_agent.clone())
.send()
.await;
@@ -49,13 +51,16 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
})
}
}
Err(_) => Err(FetchError::Unknown),
Err(e) => Err(FetchError::Unknown(e)),
}
}
impl BasicXlsDownloader {
pub fn new() -> Self {
BasicXlsDownloader { url: None }
BasicXlsDownloader {
url: None,
user_agent: env::var("REQWEST_USER_AGENT").expect("USER_AGENT must be set"),
}
}
}
@@ -64,17 +69,12 @@ impl XLSDownloader for BasicXlsDownloader {
if self.url.is_none() {
Err(FetchError::NoUrlProvided)
} else {
fetch_specified(
self.url.as_ref().unwrap(),
"t.me/polytechnic_next".to_string(),
head,
)
.await
fetch_specified(self.url.as_ref().unwrap(), &self.user_agent, head).await
}
}
async fn set_url(&mut self, url: String) -> FetchResult {
let result = fetch_specified(&url, "t.me/polytechnic_next".to_string(), true).await;
let result = fetch_specified(&url, &self.user_agent, true).await;
if let Ok(_) = result {
self.url = Some(url);
@@ -95,8 +95,8 @@ mod tests {
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, &user_agent, false).await,
];
assert!(results[0].is_err());
@@ -109,8 +109,8 @@ mod tests {
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, &user_agent, false).await,
];
assert!(results[0].is_err());
@@ -132,8 +132,8 @@ mod tests {
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, &user_agent, false).await,
];
assert!(results[0].is_err());
@@ -149,8 +149,8 @@ mod tests {
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, &user_agent, false).await,
];
assert!(results[0].is_err());
@@ -172,8 +172,8 @@ mod tests {
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
fetch_specified(&url, &user_agent, true).await,
fetch_specified(&url, &user_agent, false).await,
];
assert!(results[0].is_ok());

View File

@@ -1,13 +1,14 @@
use chrono::{DateTime, Utc};
use std::mem::discriminant;
/// XLS data retrieval errors.
#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub enum FetchError {
/// File url is not set.
NoUrlProvided,
/// Unknown error.
Unknown,
Unknown(reqwest::Error),
/// Server returned a status code different from 200.
BadStatusCode,
@@ -19,6 +20,12 @@ pub enum FetchError {
BadHeaders,
}
impl PartialEq for FetchError {
fn eq(&self, other: &Self) -> bool {
discriminant(self) == discriminant(other)
}
}
/// Result of XLS data retrieval.
pub struct FetchOk {
/// ETag object.