nhentairs/src/api.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2023-10-28 00:08:10 +00:00
use crate::structs;
2023-10-28 00:25:53 +00:00
use std::env;
use std::process::exit;
2023-10-28 00:08:10 +00:00
extern crate serde_json;
2023-10-28 00:25:53 +00:00
pub fn get_client() -> reqwest::Client {
let mut builder = reqwest::Client::builder();
match env::var("NHENTAIRS_INSECURE") {
Ok(val) => {
if val == "true" || val == "yes" || val == "1" {
builder = builder.danger_accept_invalid_certs(true);
}
}
Err(env::VarError::NotPresent) => {}
Err(err) => eprintln!("failed to parse NHENTAIRS_INSECURE: {err}"),
};
match builder.build() {
Ok(client) => client,
Err(err) => {
eprintln!("Failed to create reqwest client: {err}");
exit(1);
}
}
}
2023-10-28 00:11:16 +00:00
pub async fn get_sauce_info(
client: reqwest::Client,
sauce: i32,
2023-10-28 00:49:35 +00:00
) -> Result<structs::GalleryInfo, structs::Error> {
2023-10-28 00:08:10 +00:00
let mut uri = String::from("https://nhentai.net/api/gallery/");
uri.push_str(&sauce.to_string());
2023-10-28 00:11:16 +00:00
let resp = client.get(&uri).send().await?;
2023-10-28 00:49:35 +00:00
Ok(serde_json::from_str(&resp.text().await?)?)
2023-10-28 00:08:10 +00:00
}
2023-10-28 00:11:16 +00:00
pub async fn get_search_info(
client: reqwest::Client,
search_query: &str,
2023-10-28 00:49:35 +00:00
) -> Result<structs::SearchInfo, structs::Error> {
2023-10-28 00:08:10 +00:00
let uri = "https://nhentai.net/api/galleries/search";
2023-10-28 00:11:16 +00:00
let resp = client
.get(uri)
2023-10-28 00:08:10 +00:00
.query(&[("query", search_query)])
.send()
.await?;
2023-10-28 00:49:35 +00:00
Ok(serde_json::from_str(&resp.text().await?)?)
2023-10-28 00:08:10 +00:00
}