client: fixup checks for api calls not returning data

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2023-08-25 09:02:58 +02:00
parent f20f9bb9f7
commit 8617442560

View File

@ -107,7 +107,7 @@ impl HttpApiResponse {
if response.data.is_some() { if response.data.is_some() {
Err(Error::UnexpectedData) Err(Error::UnexpectedData)
} else { } else {
response.check()?; response.check_nodata()?;
Ok(()) Ok(())
} }
} }
@ -136,8 +136,11 @@ struct RawApiResponse<T> {
} }
impl<T> RawApiResponse<T> { impl<T> RawApiResponse<T> {
pub fn check(mut self) -> Result<ApiResponseData<T>, Error> { fn check_success(mut self) -> Result<Self, Error> {
if !self.success.unwrap_or(false) { if self.success == Some(true) {
return Ok(self);
}
let status = http::StatusCode::from_u16(self.status.unwrap_or(400)) let status = http::StatusCode::from_u16(self.status.unwrap_or(400))
.unwrap_or(http::StatusCode::BAD_REQUEST); .unwrap_or(http::StatusCode::BAD_REQUEST);
let mut message = self let mut message = self
@ -149,14 +152,26 @@ impl<T> RawApiResponse<T> {
let _ = write!(message, "\n{param}: {error}"); let _ = write!(message, "\n{param}: {error}");
} }
return Err(Error::api(status, message)); Err(Error::api(status, message))
} }
fn check(self) -> Result<ApiResponseData<T>, Error> {
let this = self.check_success()?;
Ok(ApiResponseData { Ok(ApiResponseData {
data: self data: this
.data .data
.ok_or_else(|| Error::BadApi("api returned no data".to_string(), None))?, .ok_or_else(|| Error::BadApi("api returned no data".to_string(), None))?,
attribs: self.attribs, attribs: this.attribs,
})
}
fn check_nodata(self) -> Result<ApiResponseData<()>, Error> {
let this = self.check_success()?;
Ok(ApiResponseData {
data: (),
attribs: this.attribs,
}) })
} }
} }