update reqwest

This commit is contained in:
2020-05-03 13:12:30 +08:00
parent 609d89ee4e
commit 6776ea7b5a
3 changed files with 42 additions and 10 deletions

12
reqwest/Cargo.lock generated
View File

@@ -6,6 +6,17 @@ version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62" checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62"
[[package]]
name = "async-trait"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da71fef07bc806586090247e971229289f64c210a278ee5ae419314eb386b31d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.0" version = "1.0.0"
@@ -624,6 +635,7 @@ dependencies = [
name = "reqwest" name = "reqwest"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-trait",
"reqwest 0.10.4", "reqwest 0.10.4",
"tokio", "tokio",
] ]

View File

@@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
async-trait = "0.1.30"
tokio = { version = "0.2.6", features = ["full"] } tokio = { version = "0.2.6", features = ["full"] }
reqwest = "0.10.4" reqwest = "0.10.4"

View File

@@ -9,24 +9,43 @@ pub async fn main() {
return; return;
}, },
}; };
let mut map = HashMap::new(); let mut map: HashMap<_, Box<dyn Call>> = HashMap::new();
map.insert("1", test001); map.insert("1", Box::new(T001{}) );
map.insert("2", Box::new(T002{}) );
let f = match map.get(a.as_str()) { let c = match map.get(a.as_str()) {
Some(f) => f, None => { Some(c) => c, None => {
println!("{}", &format!("[ERROR] Cannot find {}", a)); println!("[ERROR] Cannot find {}", a);
return; return;
}, },
}; };
match f().await { match c.call().await {
Ok(_) => println!("[OK] Call fn ok: {}", a), Ok(_) => println!("[OK] Call fn ok: {}", a),
Err(err) => println!("[ERROR] Call fn error: {}, message: {}", a, err), Err(err) => println!("[ERROR] Call fn error: {}, message: {}", a, err),
} }
} }
async fn test001() -> Result<(), Box<dyn std::error::Error>> { #[async_trait::async_trait]
let ip = reqwest::get("https://hatter.ink/ip/ip.jsonp").await?.text().await?; trait Call {
println!("{}", ip); async fn call(&self) -> Result<(), Box<dyn std::error::Error>>;
Ok(()) }
struct T001();
#[async_trait::async_trait]
impl Call for T001 {
async fn call(&self) -> Result<(), Box<dyn std::error::Error>> {
let ip = reqwest::get("https://hatter.ink/ip/ip.jsonp").await?.text().await?;
println!("{}", ip);
Ok(())
}
}
struct T002();
#[async_trait::async_trait]
impl Call for T002 {
async fn call(&self) -> Result<(), Box<dyn std::error::Error>> {
println!("Hello World 2");
Ok(())
}
} }