mirror of
https://github.com/rustdesk/rustdesk
synced 2026-04-21 13:27:19 +00:00
refactor: replace unused TryFrom<Response> with HbbHttpResponse::parse method
Remove TryFrom<Response> impl that was never called and replace the private parse_hbb_http_response helper in account.rs with a public parse() method on HbbHttpResponse, eliminating code duplication. Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
parent
28cca601b5
commit
8965c60424
2 changed files with 6 additions and 26 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use reqwest::blocking::Response;
|
||||
use hbb_common::ResultType;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
|
|
@ -21,11 +21,9 @@ pub enum HbbHttpResponse<T> {
|
|||
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>>()?;
|
||||
impl<T: DeserializeOwned> HbbHttpResponse<T> {
|
||||
pub fn parse(body: &str) -> ResultType<Self> {
|
||||
let map = serde_json::from_str::<Map<String, Value>>(body)?;
|
||||
if let Some(error) = map.get("error") {
|
||||
if let Some(err) = error.as_str() {
|
||||
Ok(Self::Error(err.to_owned()))
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
use super::HbbHttpResponse;
|
||||
use crate::hbbs_http::create_http_client_with_url;
|
||||
use hbb_common::{config::LocalConfig, log, ResultType};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||
use serde_json::{Map, Value};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, RwLock},
|
||||
|
|
@ -159,22 +157,6 @@ impl OidcSession {
|
|||
write_guard.warmed_api_server = Some(api_server.to_owned());
|
||||
}
|
||||
|
||||
fn parse_hbb_http_response<T: DeserializeOwned>(body: &str) -> ResultType<HbbHttpResponse<T>> {
|
||||
let map = serde_json::from_str::<Map<String, Value>>(body)?;
|
||||
if let Some(error) = map.get("error") {
|
||||
if let Some(err) = error.as_str() {
|
||||
Ok(HbbHttpResponse::Error(err.to_owned()))
|
||||
} else {
|
||||
Ok(HbbHttpResponse::ErrorFormat)
|
||||
}
|
||||
} else {
|
||||
match serde_json::from_value(Value::Object(map)) {
|
||||
Ok(v) => Ok(HbbHttpResponse::Data(v)),
|
||||
Err(_) => Ok(HbbHttpResponse::DataTypeFormat),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn auth(
|
||||
api_server: &str,
|
||||
op: &str,
|
||||
|
|
@ -190,7 +172,7 @@ impl OidcSession {
|
|||
})
|
||||
.to_string();
|
||||
let resp = crate::post_request_sync(format!("{}/api/oidc/auth", api_server), body, "")?;
|
||||
Self::parse_hbb_http_response(&resp)
|
||||
HbbHttpResponse::parse(&resp)
|
||||
}
|
||||
|
||||
fn query(
|
||||
|
|
@ -216,7 +198,7 @@ impl OidcSession {
|
|||
"{}".to_owned(),
|
||||
)?;
|
||||
let resp = serde_json::from_str::<HttpResponseBody>(&resp)?;
|
||||
Self::parse_hbb_http_response(&resp.body)
|
||||
HbbHttpResponse::parse(&resp.body)
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue