golem_gateway_client/api/
api_deployment.rs

1use async_trait::async_trait;
2use crate::Context;
3use crate::Error;
4use crate::model::ApiDeployment;
5use crate::model::ErrorBody;
6use crate::model::ErrorsBody;
7use crate::model::MessageBody;
8use uuid::Uuid;
9
10pub enum ApiDeploymentError {
11    Error400(ErrorsBody),
12    Error401(ErrorBody),
13    Error403(ErrorBody),
14    Error404(MessageBody),
15    Error409(String),
16    Error500(ErrorBody),
17}
18
19#[async_trait]
20pub trait ApiDeploymentClient {
21    async fn get(&self, project_id: &Uuid, api_definition_id: &str) -> Result<Vec<ApiDeployment>, Error<ApiDeploymentError>>;
22    async fn put(&self, value: &ApiDeployment) -> Result<ApiDeployment, Error<ApiDeploymentError>>;
23    async fn delete(&self, project_id: &Uuid, api_definition_id: &str, site: &str) -> Result<String, Error<ApiDeploymentError>>;
24}
25
26pub struct ApiDeploymentClientLive {
27    pub context: Context,
28}
29
30#[async_trait]
31impl ApiDeploymentClient for ApiDeploymentClientLive {
32    async fn get(&self, project_id: &Uuid, api_definition_id: &str) -> Result<Vec<ApiDeployment>, Error<ApiDeploymentError>> {
33        let mut url = self.context.base_url.clone();
34        url.path_segments_mut().unwrap()
35            .push("v1")
36            .push("api")
37            .push("deployments");
38
39        url.query_pairs_mut().append_pair("project-id", &project_id.to_string());
40
41        url.query_pairs_mut().append_pair("api-definition-id", &api_definition_id);
42
43        let mut request = self
44            .context
45            .client
46            .get(url.clone());
47
48        {
49            tracing::info!(method="get", endpoint="/v1/api/deployments", url=url.to_string(), "get");
50        }
51
52        if let Some(token) = self.context.bearer_token() {
53            request = request.bearer_auth(token);
54        }
55
56        let response = request.send().await?;
57
58        let status = response.status().as_u16();
59        match status {
60            200 => {
61                Ok(response.json::<Vec<ApiDeployment>>().await?)
62            }
63            400 => {
64                let body = response.json::<ErrorsBody>().await?;
65                Err(Error::Item(ApiDeploymentError::Error400(body)))
66            }
67            401 => {
68                let body = response.json::<ErrorBody>().await?;
69                Err(Error::Item(ApiDeploymentError::Error401(body)))
70            }
71            403 => {
72                let body = response.json::<ErrorBody>().await?;
73                Err(Error::Item(ApiDeploymentError::Error403(body)))
74            }
75            404 => {
76                let body = response.json::<MessageBody>().await?;
77                Err(Error::Item(ApiDeploymentError::Error404(body)))
78            }
79            409 => {
80                let body = response.json::<String>().await?;
81                Err(Error::Item(ApiDeploymentError::Error409(body)))
82            }
83            500 => {
84                let body = response.json::<ErrorBody>().await?;
85                Err(Error::Item(ApiDeploymentError::Error500(body)))
86            }
87            _ => Err(Error::unexpected(status, response.bytes().await?)),
88        }
89    }
90
91    async fn put(&self, value: &ApiDeployment) -> Result<ApiDeployment, Error<ApiDeploymentError>> {
92        let mut url = self.context.base_url.clone();
93        url.path_segments_mut().unwrap()
94            .push("v1")
95            .push("api")
96            .push("deployments");
97
98        let mut request = self
99            .context
100            .client
101            .put(url.clone());
102
103        {
104            tracing::info!(method="put", endpoint="/v1/api/deployments", url=url.to_string(), body=serde_json::to_string(value)?, "put");
105        }
106
107        if let Some(token) = self.context.bearer_token() {
108            request = request.bearer_auth(token);
109        }
110
111        request = request.json(value);
112
113        let response = request.send().await?;
114
115        let status = response.status().as_u16();
116        match status {
117            200 => {
118                Ok(response.json::<ApiDeployment>().await?)
119            }
120            400 => {
121                let body = response.json::<ErrorsBody>().await?;
122                Err(Error::Item(ApiDeploymentError::Error400(body)))
123            }
124            401 => {
125                let body = response.json::<ErrorBody>().await?;
126                Err(Error::Item(ApiDeploymentError::Error401(body)))
127            }
128            403 => {
129                let body = response.json::<ErrorBody>().await?;
130                Err(Error::Item(ApiDeploymentError::Error403(body)))
131            }
132            404 => {
133                let body = response.json::<MessageBody>().await?;
134                Err(Error::Item(ApiDeploymentError::Error404(body)))
135            }
136            409 => {
137                let body = response.json::<String>().await?;
138                Err(Error::Item(ApiDeploymentError::Error409(body)))
139            }
140            500 => {
141                let body = response.json::<ErrorBody>().await?;
142                Err(Error::Item(ApiDeploymentError::Error500(body)))
143            }
144            _ => Err(Error::unexpected(status, response.bytes().await?)),
145        }
146    }
147
148    async fn delete(&self, project_id: &Uuid, api_definition_id: &str, site: &str) -> Result<String, Error<ApiDeploymentError>> {
149        let mut url = self.context.base_url.clone();
150        url.path_segments_mut().unwrap()
151            .push("v1")
152            .push("api")
153            .push("deployments");
154
155        url.query_pairs_mut().append_pair("project-id", &project_id.to_string());
156
157        url.query_pairs_mut().append_pair("api-definition-id", &api_definition_id);
158
159        url.query_pairs_mut().append_pair("site", &site);
160
161        let mut request = self
162            .context
163            .client
164            .delete(url.clone());
165
166        {
167            tracing::info!(method="delete", endpoint="/v1/api/deployments", url=url.to_string(), "delete");
168        }
169
170        if let Some(token) = self.context.bearer_token() {
171            request = request.bearer_auth(token);
172        }
173
174        let response = request.send().await?;
175
176        let status = response.status().as_u16();
177        match status {
178            200 => {
179                Ok(response.json::<String>().await?)
180            }
181            400 => {
182                let body = response.json::<ErrorsBody>().await?;
183                Err(Error::Item(ApiDeploymentError::Error400(body)))
184            }
185            401 => {
186                let body = response.json::<ErrorBody>().await?;
187                Err(Error::Item(ApiDeploymentError::Error401(body)))
188            }
189            403 => {
190                let body = response.json::<ErrorBody>().await?;
191                Err(Error::Item(ApiDeploymentError::Error403(body)))
192            }
193            404 => {
194                let body = response.json::<MessageBody>().await?;
195                Err(Error::Item(ApiDeploymentError::Error404(body)))
196            }
197            409 => {
198                let body = response.json::<String>().await?;
199                Err(Error::Item(ApiDeploymentError::Error409(body)))
200            }
201            500 => {
202                let body = response.json::<ErrorBody>().await?;
203                Err(Error::Item(ApiDeploymentError::Error500(body)))
204            }
205            _ => Err(Error::unexpected(status, response.bytes().await?)),
206        }
207    }
208}