Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reqwest = { version = "0.10.1", features = ["json", "blocking"] } #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
.idea
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
env_logger = "0.7"
Empty file removed src/errors.rs
Empty file.
24 changes: 13 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ extern crate serde_derive;
extern crate env_logger;


#[derive(Debug)]
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
pub struct Response {
url: String,
}
Expand All @@ -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<String, String> {
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()))
}
}