public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] client: remove option from inner RawApiResponse
Date: Tue, 29 Aug 2023 14:04:40 +0200	[thread overview]
Message-ID: <20230829120440.478630-1-d.csapak@proxmox.com> (raw)

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<T> 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 <d.csapak@proxmox.com>
---
 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 6039ae5..9aa7144 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::<RawApiResponse<()>>(&self.body)
-            .map_err(|err| Error::bad_api("failed to parse api response", err))?;
+        let response = serde_json::from_slice::<RawApiResponse<Option<()>>>(&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<T> {
     message: Option<String>,
     #[serde(default, deserialize_with = "proxmox_login::parse::deserialize_bool")]
     success: Option<bool>,
-    data: Option<T>,
+    data: T,
 
     #[serde(default)]
     errors: HashMap<String, String>,
@@ -164,9 +164,7 @@ impl<T> RawApiResponse<T> {
         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,
         })
     }
-- 
2.39.2




             reply	other threads:[~2023-08-29 12:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-29 12:04 Dominik Csapak [this message]
2023-08-29 12:18 ` [pbs-devel] applied: " Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230829120440.478630-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal