Split requests into their own module
This commit is contained in:
parent
4769baf875
commit
bb80105b4a
|
@ -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())
|
||||||
|
}
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod api;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod structs;
|
mod structs;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
|
21
src/utils.rs
21
src/utils.rs
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue