feat: initial API and docs

This commit is contained in:
Ninjdai 2025-07-30 16:55:07 +02:00
parent 79be4eb543
commit 5d709d658b
16 changed files with 3169 additions and 342 deletions

43
src/utils/open_library.rs Normal file
View 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
}
}