From d6a550b71c4aa71bfce96b0deadd9d8e2ee0c2a7 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 1 Aug 2023 15:16:07 +0200 Subject: [PATCH] login: improve response handling we have use cases where we have bytes, and serde_json has a from_slice method, doing the utf-8 check unnecessarily is pointless, while going from &str to &[u8] is free... Signed-off-by: Wolfgang Bumiller --- proxmox-login/src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/proxmox-login/src/lib.rs b/proxmox-login/src/lib.rs index 6fbccd9f..f5f95933 100644 --- a/proxmox-login/src/lib.rs +++ b/proxmox-login/src/lib.rs @@ -128,10 +128,17 @@ impl Login { /// /// On success, this will either yield an [`Authentication`] or a [`SecondFactorChallenge`] if /// Two-Factor-Authentication is required. - pub fn response(&self, body: &str) -> Result { + pub fn response>( + &self, + body: &T, + ) -> Result { + self.response_bytes(body.as_ref()) + } + + fn response_bytes(&self, body: &[u8]) -> Result { use ticket::TicketResponse; - let response: api::ApiResponse = serde_json::from_str(body)?; + let response: api::ApiResponse = serde_json::from_slice(body)?; let response = response.data.ok_or("missing response data")?; if response.username != self.userid { @@ -271,8 +278,15 @@ impl SecondFactorChallenge { } /// Deal with the API's response object to extract the ticket. - pub fn response(&self, body: &str) -> Result { - let response: api::ApiResponse = serde_json::from_str(body)?; + pub fn response>( + &self, + body: &T, + ) -> Result { + self.response_bytes(body.as_ref()) + } + + fn response_bytes(&self, body: &[u8]) -> Result { + let response: api::ApiResponse = serde_json::from_slice(body)?; let response = response.data.ok_or("missing response data")?; if response.username != self.userid {