mirror of
https://github.com/rustdesk/rustdesk.git
synced 2024-12-14 19:49:04 +08:00
fae8a94891
Signed-off-by: 21pages <pages21@163.com>
37 lines
967 B
Rust
37 lines
967 B
Rust
use reqwest::blocking::Response;
|
|
use serde::de::DeserializeOwned;
|
|
use serde_json::{Map, Value};
|
|
|
|
#[cfg(feature = "flutter")]
|
|
pub mod account;
|
|
pub mod record_upload;
|
|
pub mod sync;
|
|
|
|
#[derive(Debug)]
|
|
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> {
|
|
let map = resp.json::<Map<String, Value>>()?;
|
|
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),
|
|
}
|
|
}
|
|
}
|
|
}
|