2022-10-22 22:19:14 +08:00
|
|
|
use reqwest::blocking::Response;
|
|
|
|
use serde::de::DeserializeOwned;
|
2022-10-19 22:48:51 +08:00
|
|
|
use serde_json::{Map, Value};
|
|
|
|
|
2022-11-09 15:04:24 +08:00
|
|
|
#[cfg(feature = "flutter")]
|
2022-10-19 22:48:51 +08:00
|
|
|
pub mod account;
|
2024-04-25 11:46:21 +08:00
|
|
|
mod http_client;
|
2022-12-07 16:30:44 +08:00
|
|
|
pub mod record_upload;
|
2022-12-28 11:01:09 +08:00
|
|
|
pub mod sync;
|
2024-04-25 11:46:21 +08:00
|
|
|
pub use http_client::create_http_client;
|
|
|
|
pub use http_client::create_http_client_async;
|
2022-10-19 22:48:51 +08:00
|
|
|
|
2022-10-22 22:19:14 +08:00
|
|
|
#[derive(Debug)]
|
2022-10-19 22:48:51 +08:00
|
|
|
pub enum HbbHttpResponse<T> {
|
|
|
|
ErrorFormat,
|
|
|
|
Error(String),
|
|
|
|
DataTypeFormat,
|
|
|
|
Data(T),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: DeserializeOwned> TryFrom<Response> for HbbHttpResponse<T> {
|
|
|
|
type Error = reqwest::Error;
|
|
|
|
|
|
|
|
fn try_from(resp: Response) -> Result<Self, <Self as TryFrom<Response>>::Error> {
|
2022-10-22 22:19:14 +08:00
|
|
|
let map = resp.json::<Map<String, Value>>()?;
|
2022-10-19 22:48:51 +08:00
|
|
|
if let Some(error) = map.get("error") {
|
|
|
|
if let Some(err) = error.as_str() {
|
|
|
|
Ok(Self::Error(err.to_owned()))
|
|
|
|
} else {
|
|
|
|
Ok(Self::ErrorFormat)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match serde_json::from_value(Value::Object(map)) {
|
|
|
|
Ok(v) => Ok(Self::Data(v)),
|
|
|
|
Err(_) => Ok(Self::DataTypeFormat),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|