use serde::{Deserialize, Serialize};
use std::{collections::HashMap, error::Error};
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Integration {
pub id: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
pub scope: Option<String>,
pub channel_ids: Option<Vec<String>>,
pub headers: Option<Vec<Header>>,
}
impl Integration {
pub fn new() -> Self {
Integration {
id: None,
name: None,
description: None,
scope: None,
channel_ids: None,
headers: None,
}
}
pub fn template() -> Self {
Integration {
id: Some(String::from("UUID")),
name: Some(String::from("Integration Name")),
description: Some(String::from("Integration Description")),
scope: Some(String::from(
"PUBLIC_CHANNELS | OWNER_ACCESS | CHANNEL_LIST",
)),
channel_ids: Some(vec![
String::from("CHANNEL-A ID for CHANNEL_LIST scope"),
String::from("CHANNEL-B ID for CHANNEL_LIST scope"),
String::from("CHANNEL-C ID for CHANNEL_LIST scope"),
]),
headers: Some(vec![
Header {
name: Some(String::from("HeaderName")),
value: Some(String::from("HeaderValue")),
},
Header {
name: Some(String::from("x-my-api-key")),
value: Some(String::from("ABC123")),
},
]),
}
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Header {
pub name: Option<String>,
pub value: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Subscription {
pub id: Option<String>,
pub event_type: Option<String>,
pub url: Option<String>,
pub slash_command: Option<String>,
}
impl Subscription {
pub fn new() -> Self {
Subscription {
id: None,
event_type: None,
url: None,
slash_command: None,
}
}
pub fn template() -> Self {
Subscription{
id: Some(String::from("UUID")),
event_type: Some(String::from("MESSAGE_POSTED | SLASH_COMMAND | THREAD_CREATED | USERS_JOINED_CHANNEL | USERS_LEFT_CHANNEL")),
url: Some(String::from("The integration will post to this URL when an event occurs")),
slash_command: Some(String::from("Required if and only if eventType is SLASH_COMMAND")),
}
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Event {
pub author: Option<User>,
pub thread: Option<Channel>,
pub message: Option<Message>,
pub users: Option<Vec<User>>,
pub event: Option<EventT>,
pub owner: Option<User>,
pub organization: Option<Organization>,
pub channel: Option<Channel>,
pub callback: Option<Callback>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct EventT {
#[serde(rename = "type")]
pub event_type: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Organization {
pub domain: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct User {
pub id: Option<u64>,
pub display_name: Option<String>,
pub email: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Message {
pub id: Option<String>,
pub text: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Channel {
pub id: Option<String>,
pub parent_id: Option<String>,
pub title: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Callback {
pub url: Option<String>,
pub headers: HashMap<String, String>,
}
impl super::Client {
pub async fn get_integrations(
&self,
) -> Result<Vec<Integration>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
struct Ret {
integrations: Vec<Integration>,
}
let mut response = surf::get(&format!("{}{}", self.host, "/v1/buzz/integrations"))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
let ret: Ret = response.body_json().await?;
Ok(ret.integrations)
}
pub async fn post_integration(
&self,
integration: Integration,
) -> Result<Integration, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
let mut response = surf::post(&format!("{}{}", self.host, "/v1/buzz/integrations"))
.header("Authorization", at)
.body(surf::Body::from_json(&integration)?)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn get_integration(
&self,
id: &str,
) -> Result<Integration, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
let mut response = surf::get(&format!("{}{}{}", self.host, "/v1/buzz/integrations/", id))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn delete_integration(
&self,
id: &str,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
let mut response =
surf::delete(&format!("{}{}{}", self.host, "/v1/buzz/integrations/", id))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn get_integration_subscriptions(
&self,
id: &str,
) -> Result<Vec<Subscription>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
struct Ret {
subscriptions: Vec<Subscription>,
}
let mut response = surf::get(&format!(
"{}{}{}{}",
self.host, "/v1/buzz/integrations/", id, "/subscriptions"
))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
let ret: Ret = response.body_json().await?;
Ok(ret.subscriptions)
}
pub async fn post_integration_subscription(
&self,
id: &str,
subscription: Subscription,
) -> Result<Subscription, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
let mut response = surf::post(&format!(
"{}{}{}{}",
self.host, "/v1/buzz/integrations/", id, "/subscriptions"
))
.header("Authorization", at)
.body(surf::Body::from_json(&subscription)?)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn delete_integration_subscription(
&self,
id: &str,
subscription_id: &str,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("buzz").await?;
let mut response = surf::delete(&format!(
"{}{}{}{}{}",
self.host, "/v1/buzz/integrations/", id, "/subscriptions/", subscription_id
))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
}