From 271a55f187cf862c66388b641cc313244c33db87 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Tue, 29 Aug 2023 14:04:40 +0200 Subject: [PATCH] client: remove option from inner RawApiResponse when using the client for an api call that does not return any data (it returns '{"data":null}'), we would always get an error 'api returned no data'. The message is technically correct, but it should not be an error when we expect no data (e.g. most of our CRUD PUT/POST calls) instead of having the Option in the RawApiResponse type itself, move it into to the 'nodata' function intended for api calls where we don't expect any data. Signed-off-by: Dominik Csapak --- proxmox-client/src/lib.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/proxmox-client/src/lib.rs b/proxmox-client/src/lib.rs index 6039ae5e..9aa71448 100644 --- a/proxmox-client/src/lib.rs +++ b/proxmox-client/src/lib.rs @@ -106,8 +106,8 @@ impl HttpApiResponse { /// Expect that the API call did *not* return any data in the `data` field. pub fn nodata(self) -> Result<(), Error> { - let response = serde_json::from_slice::>(&self.body) - .map_err(|err| Error::bad_api("failed to parse api response", err))?; + let response = serde_json::from_slice::>>(&self.body) + .map_err(|err| Error::bad_api("unexpected api response", err))?; if response.data.is_some() { Err(Error::UnexpectedData) @@ -131,7 +131,7 @@ struct RawApiResponse { message: Option, #[serde(default, deserialize_with = "proxmox_login::parse::deserialize_bool")] success: Option, - data: Option, + data: T, #[serde(default)] errors: HashMap, @@ -164,9 +164,7 @@ impl RawApiResponse { let this = self.check_success()?; Ok(ApiResponseData { - data: this - .data - .ok_or_else(|| Error::BadApi("api returned no data".to_string(), None))?, + data: this.data, attribs: this.attribs, }) }