pub mod activations; pub mod folders; pub mod groups; pub mod hosts; pub mod tags; use std::fmt::{self, Display}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use crate::Result; use crate::client::InnerClient; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum DomainType { Link, HostConfig, FolderConfig, ContactGroupConfig, HostGroupConfig, ServiceGroupConfig, HostTagGroup, ActivationRun, } impl fmt::Display for DomainType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { DomainType::HostConfig => write!(f, "host_config"), DomainType::FolderConfig => write!(f, "folder_config"), DomainType::Link => write!(f, "link"), DomainType::ContactGroupConfig => write!(f, "contact_group_config"), DomainType::HostGroupConfig => write!(f, "host_group_config"), DomainType::ServiceGroupConfig => write!(f, "service_group_config"), DomainType::HostTagGroup => write!(f, "host_tag_group"), DomainType::ActivationRun => write!(f, "activation_run"), } } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "UPPERCASE")] pub enum HttpMethod { Get, Put, Post, Delete, } // #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] // pub(crate) struct RelationLink { // #[serde(rename = "domainType")] // pub domain_type: DomainType, // pub rel: String, // pub href: String, // pub method: HttpMethod, // pub r#type: String, // #[serde(default)] // pub title: Option, // // body_params: HashMap // i will deal with this later // } #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub(crate) struct DomainObject { pub domain_type: DomainType, pub id: String, #[serde(default)] pub title: Option, pub extensions: E, // not used in this library // #[serde(default)] // pub links: Vec, // deprecated in favour of links // pub members: M } #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub(crate) struct DomainCollection { pub domain_type: DomainType, pub id: String, #[serde(default)] pub title: Option, pub value: Vec>, // not used in this library // #[serde(default)] // pub links: Vec, // pub extensions: Option, } impl From> for Vec { fn from(value: DomainCollection) -> Self { value.value.into_iter().map(|obj| obj.extensions).collect() } } pub trait DomainExtension: DeserializeOwned + Serialize { const DOMAIN_TYPE: DomainType; } impl InnerClient { pub(crate) async fn create_domain_object(&self, object: &O, query: &Q) -> Result<()> where E: DomainExtension, O: Serialize, Q: Serialize, { self.create(E::DOMAIN_TYPE, object, query).await } pub(crate) async fn read_domain_object( &self, id: impl Display, query: &Q, ) -> Result> where E: DomainExtension, Q: Serialize, { self.read(E::DOMAIN_TYPE, id, query).await } pub(crate) async fn update_domain_object( &self, id: impl Display, object: &O, ) -> Result<()> where E: DomainExtension, O: Serialize, { self.update(E::DOMAIN_TYPE, id, object).await } pub(crate) async fn delete_domain_object(&self, id: impl Display, query: &Q) -> Result<()> where E: DomainExtension, Q: Serialize, { self.delete(E::DOMAIN_TYPE, id, query).await } pub(crate) async fn bulk_create_domain_objects( &self, entries: &[O], query: &Q, ) -> Result<()> where E: DomainExtension, O: Serialize, Q: Serialize, { self.bulk_create(E::DOMAIN_TYPE, entries, query).await } pub(crate) async fn bulk_read_domain_objects( &self, query: &Q, ) -> Result> where E: DomainExtension, Q: Serialize, { self.bulk_read(E::DOMAIN_TYPE, query).await } pub(crate) async fn bulk_update_domain_objects(&self, entries: &[O]) -> Result<()> where E: DomainExtension, O: Serialize, { self.bulk_update(E::DOMAIN_TYPE, entries).await } pub(crate) async fn bulk_delete_domain_objects(&self, entries: &[String]) -> Result<()> where E: DomainExtension, { self.bulk_delete(E::DOMAIN_TYPE, entries).await } } macro_rules! domain_client { ($id:ident, $func:ident) => { impl Client { pub fn $func(&self) -> ApiClient<$id> { self.into() } } domain_client!($id); }; ($id:ident) => { impl From<&Client> for ApiClient<$id> { fn from(value: &Client) -> Self { Self::new(value) } } }; } pub(super) use domain_client; macro_rules! domain_create { ($id:ident, $object:ident) => { impl ApiClient<$id> { pub async fn create(&self, object: &$object) -> Result<()> { self.inner .create_domain_object::<$id, _, _>(object, &()) .await } } }; ($id:ident, $object:ident, $qry:ident) => { impl ApiClient<$id> { pub async fn create(&self, object: &$object, query: &$qry) -> Result<()> { self.inner .create_domain_object::<$id, _, _>(object, query) .await } } }; } pub(super) use domain_create; macro_rules! domain_read { ($id:ident) => { impl ApiClient<$id> { pub async fn read(&self, id: impl Display) -> Result<$id> { self.inner .read_domain_object::<$id, _>(id, &()) .await .map(|obj| obj.extensions) } } }; ($id:ident, $qry:ident) => { impl ApiClient<$id> { pub async fn read(&self, id: impl Display, query: &$qry) -> Result<$id> { self.inner .read_domain_object::<$id, _>(id, &query) .await .map(|obj| obj.extensions) } } }; } pub(super) use domain_read; macro_rules! domain_update { ($id:ident, $object:ident) => { impl ApiClient<$id> { pub async fn update(&self, id: impl Display, object: &$object) -> Result<()> { self.inner.update_domain_object::<$id, _>(id, object).await } } }; } pub(super) use domain_update; macro_rules! domain_delete { ($id:ident) => { impl ApiClient<$id> { pub async fn delete(&self, id: impl Display) -> Result<()> { self.inner.delete_domain_object::<$id, _>(id, &()).await } } }; ($id:ident, $qry:ident) => { impl ApiClient<$id> { pub async fn delete(&self, id: impl Display, query: &$qry) -> Result<()> { self.inner.delete_domain_object::<$id, _>(id, &query).await } } }; } pub(super) use domain_delete; macro_rules! domain_bulk_create { ($id:ident, $object:ident) => { impl ApiClient<$id> { pub async fn bulk_create(&self, entries: &[$object]) -> Result<()> { self.inner .bulk_create_domain_objects::<$id, _, _>(entries, &()) .await } } }; ($id:ident, $object:ident, $qry:ident) => { impl ApiClient<$id> { pub async fn bulk_create(&self, entries: &[$object], query: &$qry) -> Result<()> { self.inner .bulk_create_domain_objects::<$id, _, _>(entries, query) .await } } }; } pub(super) use domain_bulk_create; macro_rules! domain_bulk_read { ($id:ident) => { impl ApiClient<$id> { pub async fn bulk_read(&self) -> Result> { let collection = self.inner.bulk_read_domain_objects::<$id, _>(&()).await?; Ok(collection.into()) } } }; ($id:ident, $qry:ident) => { impl ApiClient<$id> { pub async fn bulk_read(&self, query: &$qry) -> Result> { let collection = self .inner .bulk_read_domain_objects::<$id, _>(&query) .await?; Ok(collection.into()) } } }; } pub(super) use domain_bulk_read; macro_rules! domain_bulk_update { ($id:ident, $object:ident) => { impl ApiClient<$id> { pub async fn bulk_update(&self, entries: &[$object]) -> Result<()> { self.inner .bulk_update_domain_objects::<$id, _>(entries) .await } } }; } pub(super) use domain_bulk_update; macro_rules! domain_bulk_delete { ($id:ident) => { impl ApiClient<$id> { pub async fn bulk_delete(&self, entries: &[String]) -> Result<()> { self.inner.bulk_delete_domain_objects::<$id>(entries).await } } }; } pub(super) use domain_bulk_delete;