cod_client/general_requests/
put.rs

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