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 { 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(); log::trace!(target: "api", "OpenLibrary book fetch result: {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 } }