-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #558 from Sharktheone/net/fetcher
Add `Fetcher` which automatically expands urls
- Loading branch information
Showing
12 changed files
with
202 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub use ureq; | ||
|
||
pub mod fetcher; | ||
pub mod headers; | ||
pub mod request; | ||
pub mod response; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use super::response::Response; | ||
use crate::http::request::Request; | ||
use anyhow::bail; | ||
use gosub_shared::types::Result; | ||
use url::{ParseError, Url}; | ||
|
||
pub struct Fetcher { | ||
base_url: Url, | ||
client: ureq::Agent, | ||
} | ||
|
||
impl Fetcher { | ||
pub fn new(base: Url) -> Self { | ||
Self { | ||
base_url: base, | ||
client: ureq::Agent::new(), | ||
} | ||
} | ||
|
||
pub fn get_url(&self, url: &Url) -> Result<Response> { | ||
let scheme = url.scheme(); | ||
|
||
let resp = if scheme == "http" || scheme == "https" { | ||
let response = self.client.get(url.as_str()).call()?; | ||
|
||
response.try_into()? | ||
} else if scheme == "file" { | ||
let path = url.path(); | ||
let body = std::fs::read(path)?; | ||
|
||
Response::from(body) | ||
} else { | ||
bail!("Unsupported scheme") | ||
}; | ||
|
||
Ok(resp) | ||
} | ||
|
||
pub fn get(&self, url: &str) -> Result<Response> { | ||
let url = self.parse_url(url)?; | ||
|
||
self.get_url(&url) | ||
} | ||
|
||
pub fn get_req(&self, _url: &Request) { | ||
todo!() | ||
} | ||
|
||
fn parse_url(&self, url: &str) -> Result<Url> { | ||
let mut parsed_url = Url::parse(url); | ||
|
||
if parsed_url == Err(ParseError::RelativeUrlWithoutBase) { | ||
parsed_url = self.base_url.join(url); | ||
} | ||
|
||
Ok(parsed_url?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.