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

View File

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