Skip to main content

cratestack_client_rust/client/
crud.rs

1use reqwest::Method;
2use serde::Serialize;
3use serde::de::DeserializeOwned;
4
5use crate::client::core::CratestackClient;
6use crate::client::decode::decode_typed_response;
7use crate::codec::HttpClientCodec;
8use crate::error::{ClientError, HeaderPair, QueryPair};
9
10impl<C> CratestackClient<C>
11where
12    C: HttpClientCodec,
13{
14    pub async fn get<Output>(
15        &self,
16        path: &str,
17        query: &[QueryPair<'_>],
18        headers: &[HeaderPair<'_>],
19    ) -> Result<Output, ClientError>
20    where
21        Output: DeserializeOwned,
22    {
23        let response = self
24            .request_raw(Method::GET, path, None, query, headers)
25            .await?;
26        decode_typed_response(&self.codec, &response)
27    }
28
29    pub async fn post<Input, Output>(
30        &self,
31        path: &str,
32        input: &Input,
33        headers: &[HeaderPair<'_>],
34    ) -> Result<Output, ClientError>
35    where
36        Input: Serialize,
37        Output: DeserializeOwned,
38    {
39        let body = self.codec.encode(input)?;
40        let response = self
41            .request_raw(Method::POST, path, Some(body), &[], headers)
42            .await?;
43        decode_typed_response(&self.codec, &response)
44    }
45
46    pub async fn patch<Input, Output>(
47        &self,
48        path: &str,
49        input: &Input,
50        headers: &[HeaderPair<'_>],
51    ) -> Result<Output, ClientError>
52    where
53        Input: Serialize,
54        Output: DeserializeOwned,
55    {
56        let body = self.codec.encode(input)?;
57        let response = self
58            .request_raw(Method::PATCH, path, Some(body), &[], headers)
59            .await?;
60        decode_typed_response(&self.codec, &response)
61    }
62
63    pub async fn delete<Output>(
64        &self,
65        path: &str,
66        headers: &[HeaderPair<'_>],
67    ) -> Result<Output, ClientError>
68    where
69        Output: DeserializeOwned,
70    {
71        let response = self
72            .request_raw(Method::DELETE, path, None, &[], headers)
73            .await?;
74        decode_typed_response(&self.codec, &response)
75    }
76}