1use anyhow::Result;
2
3use crate::Client;
4#[derive(Clone, Debug)]
5pub struct Channels {
6 pub client: Client,
7}
8
9impl Channels {
10 #[doc(hidden)]
11 pub fn new(client: Client) -> Self {
12 Self { client }
13 }
14
15 #[doc = "List channels\n\nList the channels of the company.\n\n```rust,no_run\nasync fn \
16 example_channels_list() -> anyhow::Result<()> {\n let client = \
17 front_api::Client::new_from_env();\n let result: \
18 front_api::types::ListChannelsResponse = client.channels().list().await?;\n \
19 println!(\"{:?}\", result);\n Ok(())\n}\n```"]
20 #[tracing::instrument]
21 pub async fn list<'a>(
22 &'a self,
23 ) -> Result<crate::types::ListChannelsResponse, crate::types::error::Error> {
24 let mut req = self.client.client.request(
25 http::Method::GET,
26 &format!("{}/{}", self.client.base_url, "channels"),
27 );
28 req = req.bearer_auth(&self.client.token);
29 let resp = req.send().await?;
30 let status = resp.status();
31 if status.is_success() {
32 let text = resp.text().await.unwrap_or_default();
33 serde_json::from_str(&text).map_err(|err| {
34 crate::types::error::Error::from_serde_error(
35 format_serde_error::SerdeError::new(text.to_string(), err),
36 status,
37 )
38 })
39 } else {
40 Err(crate::types::error::Error::UnexpectedResponse(resp))
41 }
42 }
43
44 #[doc = "List team channels\n\nList the channels of a team.\n\n**Parameters:**\n\n- `team_id: \
45 &'astr`: The team ID (required)\n\n```rust,no_run\nasync fn \
46 example_channels_list_team() -> anyhow::Result<()> {\n let client = \
47 front_api::Client::new_from_env();\n let result: \
48 front_api::types::ListTeamChannelsResponse =\n \
49 client.channels().list_team(\"some-string\").await?;\n println!(\"{:?}\", \
50 result);\n Ok(())\n}\n```"]
51 #[tracing::instrument]
52 pub async fn list_team<'a>(
53 &'a self,
54 team_id: &'a str,
55 ) -> Result<crate::types::ListTeamChannelsResponse, crate::types::error::Error> {
56 let mut req = self.client.client.request(
57 http::Method::GET,
58 &format!(
59 "{}/{}",
60 self.client.base_url,
61 "teams/{team_id}/channels".replace("{team_id}", team_id)
62 ),
63 );
64 req = req.bearer_auth(&self.client.token);
65 let resp = req.send().await?;
66 let status = resp.status();
67 if status.is_success() {
68 let text = resp.text().await.unwrap_or_default();
69 serde_json::from_str(&text).map_err(|err| {
70 crate::types::error::Error::from_serde_error(
71 format_serde_error::SerdeError::new(text.to_string(), err),
72 status,
73 )
74 })
75 } else {
76 Err(crate::types::error::Error::UnexpectedResponse(resp))
77 }
78 }
79
80 #[doc = "List teammate channels\n\nList the channels of a teammate.\n\n**Parameters:**\n\n- \
81 `teammate_id: &'astr`: The teammate ID (required)\n\n```rust,no_run\nasync fn \
82 example_channels_list_teammate() -> anyhow::Result<()> {\n let client = \
83 front_api::Client::new_from_env();\n let result: \
84 front_api::types::ListTeammateChannelsResponse =\n \
85 client.channels().list_teammate(\"some-string\").await?;\n println!(\"{:?}\", \
86 result);\n Ok(())\n}\n```"]
87 #[tracing::instrument]
88 pub async fn list_teammate<'a>(
89 &'a self,
90 teammate_id: &'a str,
91 ) -> Result<crate::types::ListTeammateChannelsResponse, crate::types::error::Error> {
92 let mut req = self.client.client.request(
93 http::Method::GET,
94 &format!(
95 "{}/{}",
96 self.client.base_url,
97 "teammates/{teammate_id}/channels".replace("{teammate_id}", teammate_id)
98 ),
99 );
100 req = req.bearer_auth(&self.client.token);
101 let resp = req.send().await?;
102 let status = resp.status();
103 if status.is_success() {
104 let text = resp.text().await.unwrap_or_default();
105 serde_json::from_str(&text).map_err(|err| {
106 crate::types::error::Error::from_serde_error(
107 format_serde_error::SerdeError::new(text.to_string(), err),
108 status,
109 )
110 })
111 } else {
112 Err(crate::types::error::Error::UnexpectedResponse(resp))
113 }
114 }
115
116 #[doc = "Get channel\n\nFetch a channel.\n\n**Parameters:**\n\n- `channel_id: &'astr`: The \
117 Channel ID (required)\n\n```rust,no_run\nasync fn example_channels_get() -> \
118 anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n let \
119 result: front_api::types::ChannelResponse = \
120 client.channels().get(\"some-string\").await?;\n println!(\"{:?}\", result);\n \
121 Ok(())\n}\n```"]
122 #[tracing::instrument]
123 pub async fn get<'a>(
124 &'a self,
125 channel_id: &'a str,
126 ) -> Result<crate::types::ChannelResponse, crate::types::error::Error> {
127 let mut req = self.client.client.request(
128 http::Method::GET,
129 &format!(
130 "{}/{}",
131 self.client.base_url,
132 "channels/{channel_id}".replace("{channel_id}", channel_id)
133 ),
134 );
135 req = req.bearer_auth(&self.client.token);
136 let resp = req.send().await?;
137 let status = resp.status();
138 if status.is_success() {
139 let text = resp.text().await.unwrap_or_default();
140 serde_json::from_str(&text).map_err(|err| {
141 crate::types::error::Error::from_serde_error(
142 format_serde_error::SerdeError::new(text.to_string(), err),
143 status,
144 )
145 })
146 } else {
147 Err(crate::types::error::Error::UnexpectedResponse(resp))
148 }
149 }
150
151 #[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```"]
152 #[tracing::instrument]
153 pub async fn update<'a>(
154 &'a self,
155 channel_id: &'a str,
156 body: &crate::types::UpdateChannel,
157 ) -> Result<(), crate::types::error::Error> {
158 let mut req = self.client.client.request(
159 http::Method::PATCH,
160 &format!(
161 "{}/{}",
162 self.client.base_url,
163 "channels/{channel_id}".replace("{channel_id}", channel_id)
164 ),
165 );
166 req = req.bearer_auth(&self.client.token);
167 req = req.json(body);
168 let resp = req.send().await?;
169 let status = resp.status();
170 if status.is_success() {
171 Ok(())
172 } else {
173 Err(crate::types::error::Error::UnexpectedResponse(resp))
174 }
175 }
176
177 #[doc = "Validate channel\n\nAsynchronously validate a channel\n\n**Parameters:**\n\n- \
178 `channel_id: &'astr`: The Channel ID (required)\n\n```rust,no_run\nasync fn \
179 example_channels_validate() -> anyhow::Result<()> {\n let client = \
180 front_api::Client::new_from_env();\n let result: \
181 front_api::types::ValidateChannelResponse =\n \
182 client.channels().validate(\"some-string\").await?;\n println!(\"{:?}\", \
183 result);\n Ok(())\n}\n```"]
184 #[tracing::instrument]
185 pub async fn validate<'a>(
186 &'a self,
187 channel_id: &'a str,
188 ) -> Result<crate::types::ValidateChannelResponse, crate::types::error::Error> {
189 let mut req = self.client.client.request(
190 http::Method::POST,
191 &format!(
192 "{}/{}",
193 self.client.base_url,
194 "channels/{channel_id}/validate".replace("{channel_id}", channel_id)
195 ),
196 );
197 req = req.bearer_auth(&self.client.token);
198 let resp = req.send().await?;
199 let status = resp.status();
200 if status.is_success() {
201 let text = resp.text().await.unwrap_or_default();
202 serde_json::from_str(&text).map_err(|err| {
203 crate::types::error::Error::from_serde_error(
204 format_serde_error::SerdeError::new(text.to_string(), err),
205 status,
206 )
207 })
208 } else {
209 Err(crate::types::error::Error::UnexpectedResponse(resp))
210 }
211 }
212
213 #[doc = "Create a channel\n\nCreate a channel in an inbox.\n\n**Parameters:**\n\n- `inbox_id: \
214 &'astr`: The Inbox ID (required)\n\n```rust,no_run\nasync fn \
215 example_channels_create() -> anyhow::Result<()> {\n let client = \
216 front_api::Client::new_from_env();\n client\n .channels()\n \
217 .create(\n \"some-string\",\n \
218 &serde_json::Value::String(\"some-string\".to_string()),\n )\n \
219 .await?;\n Ok(())\n}\n```"]
220 #[tracing::instrument]
221 pub async fn create<'a>(
222 &'a self,
223 inbox_id: &'a str,
224 body: &crate::types::CreateChannel,
225 ) -> Result<(), crate::types::error::Error> {
226 let mut req = self.client.client.request(
227 http::Method::POST,
228 &format!(
229 "{}/{}",
230 self.client.base_url,
231 "inboxes/{inbox_id}/channels".replace("{inbox_id}", inbox_id)
232 ),
233 );
234 req = req.bearer_auth(&self.client.token);
235 req = req.json(body);
236 let resp = req.send().await?;
237 let status = resp.status();
238 if status.is_success() {
239 Ok(())
240 } else {
241 Err(crate::types::error::Error::UnexpectedResponse(resp))
242 }
243 }
244}