Skip to content

Commit

Permalink
feat: implement integrations methods
Browse files Browse the repository at this point in the history
Signed-off-by: Lakshya Singh <[email protected]>
  • Loading branch information
king-11 committed Oct 29, 2023
1 parent 427b4bc commit 845679b
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,129 @@ pub struct ChannelTypeLimit {
pub struct Integrations {
client: Client,
}

impl Integrations {
pub fn new(client: Client) -> Self {
Self { client }
}

pub async fn get_integrations(&self) -> Result<Vec<Integration>, NovuError> {
let result: Response<Vec<Integration>> = self
.client
.get("/integrations")
.await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
400 => {
println!("{:?}", err);
todo!()
}
401 => Err(NovuError::UnauthorizedError("/integrations".to_string())),
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}

pub async fn create(&self, data: CreateIntegrationRequest) -> Result<Integration, NovuError> {
let result: Response<Integration> = self.client.post("/integrations", Some(&data)).await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError("/integrations".to_string())),
409 => {
println!("Integration already exists");
todo!()
}
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}

pub async fn active_integrations(&self) -> Result<Vec<Integration>, NovuError> {
let result: Response<Vec<Integration>> = self.client.get("/integrations/active").await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError("/integrations/active".to_string())),
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}

pub async fn webhook_support_status(&self, provider_id: u32) -> Result<bool, NovuError> {
let result: Response<bool> = self.client.get(format!("/integrations/webhook/provider/{}/status", provider_id)).await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError(format!("/integrations/provider/{}/webhook-support", provider_id))),
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}

pub async fn update_integration(&self, integration_id: u32, update_integration: UpdateIntegrationRequest) -> Result<Integration, NovuError> {
let result: Response<Integration> = self.client.put(format!("/integrations/{}", integration_id), &Some(update_integration)).await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError("/integrations".to_string())),
409 => {
println!("Integration already exists");
todo!()
}
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}

pub async fn delete_integration(&self, integration_id: u32) -> Result<Integration, NovuError> {
let result: Response<Integration> = self.client.delete(format!("/integrations/{}", integration_id)).await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError("/integrations".to_string())),
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}

pub async fn set_primary_integration(&self, integration_id: u32) -> Result<Integration, NovuError> {
let result: Response<Integration> = self.client.post(format!("/integrations/{}/set-primary", integration_id), None).await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError("/integrations".to_string())),
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}


pub async fn get_channel_limit(&self, channel_type: ChannelTypeEnum) -> Result<ChannelTypeLimit, NovuError> {
let result: Response<ChannelTypeLimit> = self.client.get(format!("/integrations/{}/limit", channel_type)).await?;

match result {
Response::Success(data) => Ok(data.data),
Response::Error(err) => match err.status_code {
401 => Err(NovuError::UnauthorizedError(format!("/integrations/channel/{}/limit", channel_type))),
code => todo!("{}", code),
},
Response::Messages(err) => todo!("{:?}", err),
}
}
}

0 comments on commit 845679b

Please sign in to comment.