use anyhow::Result;
use crate::Client;
#[derive(Clone, Debug)]
pub struct Channels {
pub client: Client,
}
impl Channels {
#[doc(hidden)]
pub fn new(client: Client) -> Self {
Self { client }
}
#[doc = "List channels\n\nList the channels of the company.\n\n```rust,no_run\nasync fn \
example_channels_list() -> anyhow::Result<()> {\n let client = \
front_api::Client::new_from_env();\n let result: \
front_api::types::ListChannelsResponse = client.channels().list().await?;\n \
println!(\"{:?}\", result);\n Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn list<'a>(
&'a self,
) -> Result<crate::types::ListChannelsResponse, crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::GET,
&format!("{}/{}", self.client.base_url, "channels"),
);
req = req.bearer_auth(&self.client.token);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
let text = resp.text().await.unwrap_or_default();
serde_json::from_str(&text).map_err(|err| {
crate::types::error::Error::from_serde_error(
format_serde_error::SerdeError::new(text.to_string(), err),
status,
)
})
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
#[doc = "List team channels\n\nList the channels of a team.\n\n**Parameters:**\n\n- `team_id: \
&'astr`: The team ID (required)\n\n```rust,no_run\nasync fn \
example_channels_list_team() -> anyhow::Result<()> {\n let client = \
front_api::Client::new_from_env();\n let result: \
front_api::types::ListTeamChannelsResponse =\n \
client.channels().list_team(\"some-string\").await?;\n println!(\"{:?}\", \
result);\n Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn list_team<'a>(
&'a self,
team_id: &'a str,
) -> Result<crate::types::ListTeamChannelsResponse, crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::GET,
&format!(
"{}/{}",
self.client.base_url,
"teams/{team_id}/channels".replace("{team_id}", team_id)
),
);
req = req.bearer_auth(&self.client.token);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
let text = resp.text().await.unwrap_or_default();
serde_json::from_str(&text).map_err(|err| {
crate::types::error::Error::from_serde_error(
format_serde_error::SerdeError::new(text.to_string(), err),
status,
)
})
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
#[doc = "List teammate channels\n\nList the channels of a teammate.\n\n**Parameters:**\n\n- \
`teammate_id: &'astr`: The teammate ID (required)\n\n```rust,no_run\nasync fn \
example_channels_list_teammate() -> anyhow::Result<()> {\n let client = \
front_api::Client::new_from_env();\n let result: \
front_api::types::ListTeammateChannelsResponse =\n \
client.channels().list_teammate(\"some-string\").await?;\n println!(\"{:?}\", \
result);\n Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn list_teammate<'a>(
&'a self,
teammate_id: &'a str,
) -> Result<crate::types::ListTeammateChannelsResponse, crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::GET,
&format!(
"{}/{}",
self.client.base_url,
"teammates/{teammate_id}/channels".replace("{teammate_id}", teammate_id)
),
);
req = req.bearer_auth(&self.client.token);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
let text = resp.text().await.unwrap_or_default();
serde_json::from_str(&text).map_err(|err| {
crate::types::error::Error::from_serde_error(
format_serde_error::SerdeError::new(text.to_string(), err),
status,
)
})
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
#[doc = "Get channel\n\nFetch a channel.\n\n**Parameters:**\n\n- `channel_id: &'astr`: The \
Channel ID (required)\n\n```rust,no_run\nasync fn example_channels_get() -> \
anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n let \
result: front_api::types::ChannelResponse = \
client.channels().get(\"some-string\").await?;\n println!(\"{:?}\", result);\n \
Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn get<'a>(
&'a self,
channel_id: &'a str,
) -> Result<crate::types::ChannelResponse, crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::GET,
&format!(
"{}/{}",
self.client.base_url,
"channels/{channel_id}".replace("{channel_id}", channel_id)
),
);
req = req.bearer_auth(&self.client.token);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
let text = resp.text().await.unwrap_or_default();
serde_json::from_str(&text).map_err(|err| {
crate::types::error::Error::from_serde_error(
format_serde_error::SerdeError::new(text.to_string(), err),
status,
)
})
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
#[doc = "Update Channel\n\nUpdate a channel.\n\n**Parameters:**\n\n- `channel_id: &'astr`: The Channel ID (required)\n\n```rust,no_run\nasync fn example_channels_update() -> anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n client\n .channels()\n .update(\n \"some-string\",\n &serde_json::Value::String(\"some-string\".to_string()),\n )\n .await?;\n Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn update<'a>(
&'a self,
channel_id: &'a str,
body: &crate::types::UpdateChannel,
) -> Result<(), crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::PATCH,
&format!(
"{}/{}",
self.client.base_url,
"channels/{channel_id}".replace("{channel_id}", channel_id)
),
);
req = req.bearer_auth(&self.client.token);
req = req.json(body);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
Ok(())
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
#[doc = "Validate channel\n\nAsynchronously validate a channel\n\n**Parameters:**\n\n- \
`channel_id: &'astr`: The Channel ID (required)\n\n```rust,no_run\nasync fn \
example_channels_validate() -> anyhow::Result<()> {\n let client = \
front_api::Client::new_from_env();\n let result: \
front_api::types::ValidateChannelResponse =\n \
client.channels().validate(\"some-string\").await?;\n println!(\"{:?}\", \
result);\n Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn validate<'a>(
&'a self,
channel_id: &'a str,
) -> Result<crate::types::ValidateChannelResponse, crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::POST,
&format!(
"{}/{}",
self.client.base_url,
"channels/{channel_id}/validate".replace("{channel_id}", channel_id)
),
);
req = req.bearer_auth(&self.client.token);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
let text = resp.text().await.unwrap_or_default();
serde_json::from_str(&text).map_err(|err| {
crate::types::error::Error::from_serde_error(
format_serde_error::SerdeError::new(text.to_string(), err),
status,
)
})
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
#[doc = "Create a channel\n\nCreate a channel in an inbox.\n\n**Parameters:**\n\n- `inbox_id: \
&'astr`: The Inbox ID (required)\n\n```rust,no_run\nasync fn \
example_channels_create() -> anyhow::Result<()> {\n let client = \
front_api::Client::new_from_env();\n client\n .channels()\n \
.create(\n \"some-string\",\n \
&serde_json::Value::String(\"some-string\".to_string()),\n )\n \
.await?;\n Ok(())\n}\n```"]
#[tracing::instrument]
pub async fn create<'a>(
&'a self,
inbox_id: &'a str,
body: &crate::types::CreateChannel,
) -> Result<(), crate::types::error::Error> {
let mut req = self.client.client.request(
http::Method::POST,
&format!(
"{}/{}",
self.client.base_url,
"inboxes/{inbox_id}/channels".replace("{inbox_id}", inbox_id)
),
);
req = req.bearer_auth(&self.client.token);
req = req.json(body);
let resp = req.send().await?;
let status = resp.status();
if status.is_success() {
Ok(())
} else {
Err(crate::types::error::Error::UnexpectedResponse(resp))
}
}
}