Skip to content

Commit

Permalink
Add get_text helper to blitz-net
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Dec 23, 2024
1 parent 23fa191 commit dd0af26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
12 changes: 12 additions & 0 deletions packages/blitz-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ use tokio::{

const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0";

pub async fn get_text(url: &str) -> String {
Client::new()
.get(url)
.header("User-Agent", USER_AGENT)
.send()
.await
.unwrap()
.text()
.await
.unwrap()
}

pub struct Provider<D> {
rt: Handle,
client: Client,
Expand Down
3 changes: 1 addition & 2 deletions packages/blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[features]
default = ["net", "accessibility", "menu", "tracing", "svg"]
svg = ["blitz-renderer-vello/svg"]
net = ["dep:tokio", "dep:reqwest", "dep:url", "dep:blitz-net"]
net = ["dep:tokio", "dep:url", "dep:blitz-net"]
accessibility = ["blitz-shell/accessibility"]
menu = ["blitz-shell/menu"]
tracing = ["blitz-shell/tracing"]
Expand All @@ -22,7 +22,6 @@ blitz-net = { path = "../blitz-net", optional = true }
# IO & Networking
url = { workspace = true, features = ["serde"], optional = true }
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
reqwest = { workspace = true, optional = true }

[package.metadata.docs.rs]
all-features = true
Expand Down
22 changes: 3 additions & 19 deletions packages/blitz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@ use blitz_shell::{

#[cfg(feature = "net")]
pub fn launch_url(url: &str) {
use reqwest::Client;
use url::Url;

const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0";
println!("{}", url);

// Assert that url is valid
println!("{}", url);
let url = url.to_owned();
Url::parse(&url).expect("Invalid url");
url::Url::parse(&url).expect("Invalid url");

// Turn on the runtime and enter it
let rt = tokio::runtime::Builder::new_multi_thread()
Expand All @@ -35,18 +30,7 @@ pub fn launch_url(url: &str) {
.unwrap();
let _guard = rt.enter();

let client = Client::new();
let html = rt.block_on(async {
client
.get(&url)
.header("User-Agent", USER_AGENT)
.send()
.await
.unwrap()
.text()
.await
.unwrap()
});
let html = rt.block_on(blitz_net::get_text(&url));

launch_internal(
&html,
Expand Down

0 comments on commit dd0af26

Please sign in to comment.