diff --git a/.gitignore b/.gitignore index 6936990..293dd90 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target **/*.rs.bk Cargo.lock +.idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index c13c9b2..c608096 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["boobbot", "API"] edition = "2018" [dependencies] -reqwest = "0.9.16" +reqwest = { version = "0.10.1", features = ["json", "blocking"] } serde = "1.0.90" serde_derive = "1.0.90" -env_logger = "0.6.1" \ No newline at end of file +env_logger = "0.7" \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.rs b/src/lib.rs index e067073..8ad973a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,7 @@ extern crate serde_derive; extern crate env_logger; -#[derive(Debug)] -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] pub struct Response { url: String, } @@ -15,16 +14,19 @@ pub struct Response { * Get's a image url for the specified type */ pub fn get_img(img_type: String, api_key: String) -> Result { - let client = reqwest::Client::new(); - let img_url = "https://boob.bot/api/v2/img/"; - let url_to_praise = reqwest::Url::parse(&img_url).ok().expect("Expected a valid URL"); - let img_url = url_to_praise.join(&img_type).ok().expect("Failed to join url"); - let builder = client.get(img_url).header("key", api_key); - let mut req = builder.send().ok().expect("Failed to send request"); - if req.status().is_success() { - let img: Response = req.json().unwrap(); + let img_url = { + let img_url = "https://boob.bot/api/v2/img/"; + let url_to_praise = reqwest::Url::parse(&img_url).map_err(|_| "Expected a valid URL".to_string())?; + url_to_praise.join(&img_type).map_err(|_| "Failed to join url".to_string())? + }; + + let resp = reqwest::blocking::Client::new().get(img_url).header("key", api_key) + .send().map_err(|_| "Failed to send request".to_string())?; + + if resp.status().is_success() { + let img: Response = resp.json().unwrap(); Ok(img.url) } else { - Err(format!("Something went wrong. Status code: {:?}", req.status())) + Err(format!("Something went wrong. Status code: {}", resp.status())) } }