This commit is contained in:
2022-10-03 21:12:35 +05:30
parent f47102cc7d
commit 1ed2df0977

View File

@@ -16,19 +16,50 @@ struct Docs {
number_of_pages_median: Option<u32>, number_of_pages_median: Option<u32>,
isbn: Option<Vec<String>>, isbn: Option<Vec<String>>,
cover_i: Option<u32>, cover_i: Option<u32>,
cover_url: Option<String>,
author_name: Option<Vec<String>>, author_name: Option<Vec<String>>,
person: Option<Vec<String>>, person: Option<Vec<String>>,
place: Option<Vec<String>>, place: Option<Vec<String>>,
subject: Option<Vec<String>>, subject: Option<Vec<String>>,
time: Option<Vec<String>>, time: Option<Vec<String>>,
} }
/*
impl Docs {
fn set_cover_url(&mut self) {
match self.cover_i {
Some(cover_i) => {
self.cover_url = Some(format!(
"https://covers.openlibrary.org/b/id/{}-L.jpg",
(cover_i.unwrap())
))
}
None => (),
}
}
}
*/
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct Books { struct Books {
num_found: u32, num_found: u32,
docs: Vec<Docs>, docs: Vec<Docs>,
} }
impl Books {
fn set_all_cover_urls(&mut self) {
for book in self.docs.iter_mut() {
match book.cover_i {
Some(cover_i) => {
book.cover_url = Some(format!(
"https://covers.openlibrary.org/b/id/{}-L.jpg",
cover_i
))
}
None => (),
}
}
}
}
// Setup the command line interface with clap. // Setup the command line interface with clap.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(name = "server", about = "A server for our wasm project!")] #[clap(name = "server", about = "A server for our wasm project!")]
@@ -101,8 +132,9 @@ async fn hello(
//}; //};
let query = format!("https://openlibrary.org/search.json?q={}", search); let query = format!("https://openlibrary.org/search.json?q={}", search);
let res = reqwest::get(query).await.expect("Unable to request"); let res = reqwest::get(query).await.expect("Unable to request");
let resjson = res.json::<Books>().await.expect("Unable to return value"); let mut resjson = res.json::<Books>().await.expect("Unable to return value");
resjson.set_all_cover_urls();
resjson.docs.truncate(10);
print!("Search token {:?}\n", search); print!("Search token {:?}\n", search);
return Json(resjson); return Json(resjson);
//return "Hello"; //return "Hello";