cod_client/general_requests/
patch.rs1use reqwest::{header, Url};
2use serde::de::DeserializeOwned;
3use std::fmt::Debug;
4
5use crate::CodebergClient;
6
7impl CodebergClient {
8 pub async fn patch_body<B, T>(&self, api_endpoint: Url, body: B) -> anyhow::Result<T>
9 where
10 B: serde::Serialize,
11 T: DeserializeOwned + Debug,
12 {
13 tracing::debug!(
14 "Making PATCH call. API endpoint: {:?}",
15 api_endpoint.as_str()
16 );
17 let body = serde_json::to_string(&body)?;
18 tracing::debug!("PATCH Body: {body}");
19 let response = self
20 .patch(api_endpoint.clone())
21 .header(
22 header::CONTENT_TYPE,
23 "application/json".parse::<header::HeaderValue>()?,
24 )
25 .body(body.clone())
26 .send()
27 .await?;
28 tracing::debug!("Response Status: {:?}", response.status());
29 if !response.status().is_success() {
30 let response = self
31 .patch(api_endpoint.clone())
32 .header(
33 header::CONTENT_TYPE,
34 "application/json".parse::<header::HeaderValue>()?,
35 )
36 .body(body)
37 .send()
38 .await?;
39 let text = response.text().await?;
40 tracing::debug!("{text}");
41 }
42 let json_response = response.json().await?;
43 tracing::debug!("Response: {:?}", json_response);
44 Ok(json_response)
45 }
46}