Skip to content

Commit

Permalink
api builders
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunindia committed Oct 8, 2023
1 parent f999d73 commit 5881307
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
72 changes: 72 additions & 0 deletions src/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,28 @@ pub struct EnvironmentPayload {
pub name: String,
pub parent_id: Option<String>,
}
pub struct EnvironmentPayloadBuilder {
payload: EnvironmentPayload,
}
impl EnvironmentPayloadBuilder {
pub fn new(name: impl ToString) -> EnvironmentPayloadBuilder {
Self {
payload: EnvironmentPayload {
name: name.to_string(),
parent_id: None,
},
}
}

pub fn parent_id(mut self, parent_id: impl ToString) -> EnvironmentPayloadBuilder {
self.payload.parent_id = Some(parent_id.to_string());
self
}

pub fn build(self) -> EnvironmentPayload {
self.payload
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateEnvironmentPayload {
Expand All @@ -39,3 +60,54 @@ pub struct CreateEnvironmentPayload {
pub struct Dns {
pub inbound_parse_domain: Option<String>,
}

pub struct CreateEnvironmentPayloadBuilder {
payload: CreateEnvironmentPayload,
}

impl CreateEnvironmentPayloadBuilder {
pub fn new() -> CreateEnvironmentPayloadBuilder {
Self {
payload: CreateEnvironmentPayload {
name: None,
identifier: None,
parent_id: None,
dns: None,
},
}
}

pub fn name(mut self, name: impl ToString) -> CreateEnvironmentPayloadBuilder {
self.payload.name = Some(name.to_string());
self
}

pub fn identifier(mut self, identifier: impl ToString) -> CreateEnvironmentPayloadBuilder {
self.payload.identifier = Some(identifier.to_string());
self
}

pub fn parent_id(mut self, parent_id: impl ToString) -> CreateEnvironmentPayloadBuilder {
self.payload.parent_id = Some(parent_id.to_string());
self
}

pub fn inbound_parse_domain(
mut self,
inbound_parse_domain: impl ToString,
) -> CreateEnvironmentPayloadBuilder {
self.payload.dns = Some(Dns {
inbound_parse_domain: Some(inbound_parse_domain.to_string()),
});
self
}

pub fn build(self) -> CreateEnvironmentPayload {
self.payload
}
}
impl Default for CreateEnvironmentPayloadBuilder {
fn default() -> Self {
Self::new()
}
}
12 changes: 2 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ async fn test_get_environments() {
async fn test_create_environment() {
let novu = Novu::new("", None).unwrap();
let create_result = novu
.create_environment(EnvironmentPayload {
name: "test".to_string(),
parent_id: None,
})
.create_environment(environments::EnvironmentPayloadBuilder::new("test").build())
.await;
assert!(create_result.is_err());
}
Expand All @@ -233,12 +230,7 @@ async fn test_update_environment() {
let update_result = novu
.update_environment(
"test".to_string(),
CreateEnvironmentPayload {
name: None,
identifier: None,
parent_id: None,
dns: None,
},
environments::CreateEnvironmentPayloadBuilder::new().build(),
)
.await;
assert!(update_result.is_err());
Expand Down

0 comments on commit 5881307

Please sign in to comment.