feat: initial API and docs
This commit is contained in:
parent
79be4eb543
commit
5d709d658b
16 changed files with 3169 additions and 342 deletions
2
src/utils/mod.rs
Normal file
2
src/utils/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod open_library;
|
||||
pub mod serde;
|
||||
43
src/utils/open_library.rs
Normal file
43
src/utils/open_library.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use reqwest::StatusCode;
|
||||
use sea_orm::ActiveValue::{NotSet, Set};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::entities::book;
|
||||
|
||||
pub struct FetchedBook {
|
||||
pub ean: String,
|
||||
pub title: String,
|
||||
pub author: String
|
||||
}
|
||||
|
||||
impl FetchedBook {
|
||||
pub fn to_active_model(&self) -> book::ActiveModel {
|
||||
book::ActiveModel {
|
||||
id: NotSet,
|
||||
ean: Set(self.ean.clone()),
|
||||
title: Set(self.title.clone()),
|
||||
author: Set(self.author.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch_book_by_ean(web_client: &reqwest::Client, ean: &String) -> Option<FetchedBook> {
|
||||
let body = web_client.execute(
|
||||
web_client.get(format!("https://openlibrary.org/isbn/{ean}.json"))
|
||||
.build()
|
||||
.expect("get request creation failed")
|
||||
).await.unwrap();
|
||||
match body.status() {
|
||||
StatusCode::OK => {
|
||||
let res = body.text().await.unwrap();
|
||||
println!("Res: {res:#?}");
|
||||
let v: Value = serde_json::from_str(&res).unwrap();
|
||||
Some(FetchedBook {
|
||||
ean: ean.to_string(),
|
||||
title: v.get("title").unwrap().to_string(),
|
||||
author: "temp".to_owned()
|
||||
})
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
16
src/utils/serde.rs
Normal file
16
src/utils/serde.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use serde::{de, Deserialize, Deserializer};
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
|
||||
pub fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
T: FromStr,
|
||||
T::Err: fmt::Display,
|
||||
{
|
||||
let opt = Option::<String>::deserialize(de)?;
|
||||
match opt.as_deref() {
|
||||
None | Some("") => Ok(None),
|
||||
Some(s) => FromStr::from_str(s).map_err(de::Error::custom).map(Some),
|
||||
}
|
||||
}
|
||||
Reference in a new issue