Skip to content

Commit

Permalink
Use an request method provided by Handler instead of GET
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Sep 5, 2024
1 parent eb74859 commit a88f239
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ tokio = { workspace = true, optional = true }
reqwest = { version = "0.12.7", optional = true }
winit = { version = "0.30.5", optional = true }
futures-util = { version = "0.3.30", optional = true }
ureq = { version = "2.10.1", optional = true}
ureq = { version = "2.10.1", optional = true }

http = "1.1.0"
url = "2.5.2"
data-url = "0.3.1"
tracing = "0.1.40"
Expand Down
5 changes: 5 additions & 0 deletions packages/net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod provider;
use std::ops::Deref;
use url::Url;

pub use http::Method;

pub use provider::*;

const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0";
Expand All @@ -28,6 +30,9 @@ where

pub trait RequestHandler<T>: Send + Sync + 'static {
fn bytes(self, bytes: &[u8]) -> T;
fn method(&self) -> Method {
Method::GET
}
}
impl<F: Fn(&[u8]) -> T + Sync + Send + 'static, T> RequestHandler<T> for F {
fn bytes(self, bytes: &[u8]) -> T {
Expand Down
10 changes: 7 additions & 3 deletions packages/net/src/provider/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ impl<T> SyncProvider<T> {
pub fn new() -> Self {
Self(RefCell::new(Vec::new()))
}
fn fetch_inner(&self, url: Url) -> Result<Vec<u8>, SyncProviderError> {
fn fetch_inner<H: RequestHandler<T>>(
&self,
url: Url,
handler: &H,
) -> Result<Vec<u8>, SyncProviderError> {
Ok(match url.scheme() {
"data" => {
let data_url = data_url::DataUrl::process(url.as_str())?;
Expand All @@ -23,7 +27,7 @@ impl<T> SyncProvider<T> {
file_content
}
_ => {
let response = ureq::get(url.as_str())
let response = ureq::request(handler.method().as_str(), url.as_str())
.set("User-Agent", USER_AGENT)
.call()?;

Expand All @@ -46,7 +50,7 @@ impl<T> NetProvider<T> for SyncProvider<T> {
where
H: RequestHandler<T>,
{
let res = match self.fetch_inner(url) {
let res = match self.fetch_inner(url, &handler) {
Ok(v) => v,
Err(e) => {
tracing::error!("{e}");
Expand Down
3 changes: 2 additions & 1 deletion packages/net/src/provider/non_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ impl<T: Send + 'static> AsyncProvider<T> {
}
_ => {
let start = tokio::time::Instant::now();

let response = client
.get(url.clone())
.request(handler.method(), url.clone())
.header("User-Agent", USER_AGENT)
.send()
.await?;
Expand Down

0 comments on commit a88f239

Please sign in to comment.