Split requests into their own module

This commit is contained in:
blankie 2023-10-28 11:08:10 +11:00
parent 4769baf875
commit bb80105b4a
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
6 changed files with 28 additions and 25 deletions

21
src/api.rs Normal file
View File

@ -0,0 +1,21 @@
use crate::structs;
extern crate serde_json;
pub async fn get_sauce_info(client: reqwest::Client, sauce: i32) -> Result<structs::GalleryInfo, reqwest::Error> {
let mut uri = String::from("https://nhentai.net/api/gallery/");
uri.push_str(&sauce.to_string());
let resp = client.get(&uri)
.send()
.await?;
Ok(serde_json::from_str(&resp.text().await?).unwrap())
}
pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Result<structs::SearchInfo, reqwest::Error> {
let uri = "https://nhentai.net/api/galleries/search";
let resp = client.get(uri)
.query(&[("query", search_query)])
.send()
.await?;
Ok(serde_json::from_str(&resp.text().await?).unwrap())
}

View File

@ -1,3 +1,4 @@
use crate::api;
use crate::utils; use crate::utils;
use crate::structs; use crate::structs;
@ -29,7 +30,7 @@ pub async fn run(args: env::Args) {
for sauce in sauces { for sauce in sauces {
let cloned_client = client.clone(); let cloned_client = client.clone();
handles.push(tokio::spawn(async move { handles.push(tokio::spawn(async move {
match utils::get_sauce_info(cloned_client, sauce).await.unwrap() { match api::get_sauce_info(cloned_client, sauce).await.unwrap() {
structs::GalleryInfo::Info(sauce_info) => sauce_info, structs::GalleryInfo::Info(sauce_info) => sauce_info,
structs::GalleryInfo::Error(sauce_error) => panic!("{} returned: {}", sauce, sauce_error.error) structs::GalleryInfo::Error(sauce_error) => panic!("{} returned: {}", sauce, sauce_error.error)
} }

View File

@ -1,4 +1,4 @@
use crate::utils; use crate::api;
use std::env; use std::env;
use std::process::exit; use std::process::exit;
@ -14,7 +14,7 @@ pub async fn run(args: env::Args) {
eprintln!("Missing search query"); eprintln!("Missing search query");
exit(1); exit(1);
} }
let search_info = utils::get_search_info(reqwest::Client::new(), &query).await.unwrap(); let search_info = api::get_search_info(reqwest::Client::new(), &query).await.unwrap();
if search_info.num_pages < 1 { if search_info.num_pages < 1 {
eprintln!("No results found"); eprintln!("No results found");
exit(1); exit(1);

View File

@ -1,3 +1,4 @@
use crate::api;
use crate::utils; use crate::utils;
use crate::structs; use crate::structs;
@ -18,7 +19,7 @@ pub async fn run(args: env::Args) {
for sauce in sauces { for sauce in sauces {
let cloned_client = client.clone(); let cloned_client = client.clone();
handles.push(tokio::spawn(async move { handles.push(tokio::spawn(async move {
(utils::get_sauce_info(cloned_client, sauce).await.unwrap(), sauce) (api::get_sauce_info(cloned_client, sauce).await.unwrap(), sauce)
})); }));
} }
let mut fail = false; let mut fail = false;

View File

@ -1,3 +1,4 @@
mod api;
mod utils; mod utils;
mod structs; mod structs;
mod commands; mod commands;

View File

@ -1,29 +1,8 @@
use crate::structs;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
extern crate serde_json;
extern crate reqwest; extern crate reqwest;
pub async fn get_sauce_info(client: reqwest::Client, sauce: i32) -> Result<structs::GalleryInfo, reqwest::Error> {
let mut uri = String::from("https://nhentai.net/api/gallery/");
uri.push_str(&sauce.to_string());
let resp = client.get(&uri)
.send()
.await?;
Ok(serde_json::from_str(&resp.text().await?).unwrap())
}
pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Result<structs::SearchInfo, reqwest::Error> {
let uri = "https://nhentai.net/api/galleries/search";
let resp = client.get(uri)
.query(&[("query", search_query)])
.send()
.await?;
Ok(serde_json::from_str(&resp.text().await?).unwrap())
}
pub async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<bool, reqwest::Error> { pub async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<bool, reqwest::Error> {
let resp = client.get(url) let resp = client.get(url)
.send() .send()