reqwest and tokio block on

281 阅读1分钟
[package]
name = "iciba"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = "0.11.13"
tokio = { version = "1.22.0", features = ["rt-multi-thread"] }
# ureq = "2.5.0"
fn main() {
    run().unwrap();
}
fn run() -> Result<String, Box<dyn std::error::Error>> {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let text = runtime.block_on(async { get().await.unwrap() });
    println!("{}", text);
    Ok(text)
}

#[allow(warnings)]
async fn get() -> Result<String, Box<dyn std::error::Error>> {
    let mut client = reqwest::Client::builder().user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36").build().unwrap();
    let text = client
        .get("http://open.iciba.com/dsapi/?date=2022-05-03")
        .timeout(std::time::Duration::from_secs(10))
        .send()
        .await
        .unwrap()
        .text()
        .await
        .unwrap();
    Ok(text)
}