Skip to main content

openapp_sdk_core/resources/
zones.rs

1//! `Zones` resource group.
2
3use std::sync::Arc;
4
5use openapp_sdk_common::NameMatch;
6use reqwest::Method;
7
8use super::JsonValue;
9use crate::{
10    error::SdkError,
11    resolve::{items_from_list_response, resolve_unique_sdk},
12    transport::{RequestSpec, Transport},
13};
14
15#[derive(Debug, Clone)]
16pub struct ZonesClient {
17    transport: Arc<Transport>,
18}
19
20impl ZonesClient {
21    pub(crate) fn new(transport: Arc<Transport>) -> Self {
22        Self { transport }
23    }
24
25    /// Resolve a single zone under an integration by localized display name.
26    pub async fn get_by_name(
27        &self,
28        integration_id: &str,
29        name: &str,
30        mode: NameMatch,
31    ) -> Result<JsonValue, SdkError> {
32        let items = self.by_integration(integration_id).await?;
33        let zones = items_from_list_response(&items, None);
34        resolve_unique_sdk(&zones, "zone", "name", name, mode)
35    }
36
37    pub async fn create(&self, body: &JsonValue) -> Result<JsonValue, SdkError> {
38        self.transport
39            .request_json::<JsonValue, JsonValue>(RequestSpec {
40                method: Method::POST,
41                path: "/zones",
42                body: Some(body),
43                ..Default::default()
44            })
45            .await
46    }
47
48    pub async fn get(&self, id: &str) -> Result<JsonValue, SdkError> {
49        let path = format!("/zones/{id}");
50        self.transport
51            .request_json::<(), JsonValue>(RequestSpec {
52                method: Method::GET,
53                path: &path,
54                ..Default::default()
55            })
56            .await
57    }
58
59    pub async fn update(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
60        let path = format!("/zones/{id}");
61        self.transport
62            .request_json::<JsonValue, JsonValue>(RequestSpec {
63                method: Method::PUT,
64                path: &path,
65                body: Some(body),
66                ..Default::default()
67            })
68            .await
69    }
70
71    pub async fn delete(&self, id: &str) -> Result<(), SdkError> {
72        let path = format!("/zones/{id}");
73        self.transport
74            .request_json::<(), ()>(RequestSpec {
75                method: Method::DELETE,
76                path: &path,
77                ..Default::default()
78            })
79            .await
80    }
81
82    pub async fn purge(&self, id: &str) -> Result<(), SdkError> {
83        let path = format!("/zones/{id}/purge");
84        self.transport
85            .request_json::<(), ()>(RequestSpec {
86                method: Method::DELETE,
87                path: &path,
88                ..Default::default()
89            })
90            .await
91    }
92
93    pub async fn by_integration(&self, integration_id: &str) -> Result<JsonValue, SdkError> {
94        let path = format!("/integrations/{integration_id}/zones");
95        self.transport
96            .request_json::<(), JsonValue>(RequestSpec {
97                method: Method::GET,
98                path: &path,
99                ..Default::default()
100            })
101            .await
102    }
103}