pipedrive_rs/apis/
channels_api.rs

1/*
2 * Pipedrive API v1
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: 1.0.0
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use reqwest;
12
13use super::{configuration, Error};
14use crate::apis::ResponseContent;
15
16/// struct for typed errors of method [`add_channel`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum AddChannelError {
20    Status400(crate::models::AddChannel400Response),
21    Status403(crate::models::AddChannel403Response),
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`delete_channel`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum DeleteChannelError {
29    Status400(crate::models::AddChannel400Response),
30    UnknownValue(serde_json::Value),
31}
32
33/// struct for typed errors of method [`delete_conversation`]
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum DeleteConversationError {
37    Status403(crate::models::DeleteConversation403Response),
38    Status404(crate::models::DeleteConversation404Response),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method [`receive_message`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum ReceiveMessageError {
46    Status400(crate::models::ReceiveMessage400Response),
47    UnknownValue(serde_json::Value),
48}
49
50/// Adds a new messaging channel, only admins are able to register new channels. It will use the getConversations endpoint to fetch conversations, participants and messages afterward. To use the endpoint, you need to have **Messengers integration** OAuth scope enabled and the Messaging manifest ready for the [Messaging app extension](https://pipedrive.readme.io/docs/messaging-app-extension).
51pub async fn add_channel(
52    configuration: &configuration::Configuration,
53    add_channel_request: Option<crate::models::AddChannelRequest>,
54) -> Result<crate::models::AddChannel200Response, Error<AddChannelError>> {
55    let local_var_configuration = configuration;
56
57    let local_var_client = &local_var_configuration.client;
58
59    let local_var_uri_str = format!("{}/channels", local_var_configuration.base_path);
60    let mut local_var_req_builder =
61        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
62
63    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
64        let local_var_key = local_var_apikey.key.clone();
65        let local_var_value = match local_var_apikey.prefix {
66            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
67            None => local_var_key,
68        };
69        local_var_req_builder = local_var_req_builder.query(&[("api_token", local_var_value)]);
70    }
71    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
72        local_var_req_builder =
73            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
74    }
75    if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
76        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
77    };
78    local_var_req_builder = local_var_req_builder.json(&add_channel_request);
79
80    let local_var_req = local_var_req_builder.build()?;
81    let local_var_resp = local_var_client.execute(local_var_req).await?;
82
83    let local_var_status = local_var_resp.status();
84    let local_var_content = local_var_resp.text().await?;
85
86    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
87        serde_json::from_str(&local_var_content).map_err(Error::from)
88    } else {
89        let local_var_entity: Option<AddChannelError> =
90            serde_json::from_str(&local_var_content).ok();
91        let local_var_error = ResponseContent {
92            status: local_var_status,
93            content: local_var_content,
94            entity: local_var_entity,
95        };
96        Err(Error::ResponseError(local_var_error))
97    }
98}
99
100/// Deletes an existing messenger’s channel and all related entities (conversations and messages). To use the endpoint, you need to have **Messengers integration** OAuth scope enabled and the Messaging manifest ready for the [Messaging app extension](https://pipedrive.readme.io/docs/messaging-app-extension).
101pub async fn delete_channel(
102    configuration: &configuration::Configuration,
103    id: &str,
104) -> Result<crate::models::DeleteChannel200Response, Error<DeleteChannelError>> {
105    let local_var_configuration = configuration;
106
107    let local_var_client = &local_var_configuration.client;
108
109    let local_var_uri_str = format!(
110        "{}/channels/{id}",
111        local_var_configuration.base_path,
112        id = crate::apis::urlencode(id)
113    );
114    let mut local_var_req_builder =
115        local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
116
117    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
118        let local_var_key = local_var_apikey.key.clone();
119        let local_var_value = match local_var_apikey.prefix {
120            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
121            None => local_var_key,
122        };
123        local_var_req_builder = local_var_req_builder.query(&[("api_token", local_var_value)]);
124    }
125    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
126        local_var_req_builder =
127            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
128    }
129    if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
130        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
131    };
132
133    let local_var_req = local_var_req_builder.build()?;
134    let local_var_resp = local_var_client.execute(local_var_req).await?;
135
136    let local_var_status = local_var_resp.status();
137    let local_var_content = local_var_resp.text().await?;
138
139    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
140        serde_json::from_str(&local_var_content).map_err(Error::from)
141    } else {
142        let local_var_entity: Option<DeleteChannelError> =
143            serde_json::from_str(&local_var_content).ok();
144        let local_var_error = ResponseContent {
145            status: local_var_status,
146            content: local_var_content,
147            entity: local_var_entity,
148        };
149        Err(Error::ResponseError(local_var_error))
150    }
151}
152
153/// Deletes an existing conversation. To use the endpoint, you need to have **Messengers integration** OAuth scope enabled and the Messaging manifest ready for the [Messaging app extension](https://pipedrive.readme.io/docs/messaging-app-extension).
154pub async fn delete_conversation(
155    configuration: &configuration::Configuration,
156    channel_id: &str,
157    conversation_id: &str,
158) -> Result<crate::models::DeleteChannel200Response, Error<DeleteConversationError>> {
159    let local_var_configuration = configuration;
160
161    let local_var_client = &local_var_configuration.client;
162
163    let local_var_uri_str = format!(
164        "{}/channels/{channel_id}/conversations/{conversation_id}",
165        local_var_configuration.base_path,
166        channel_id = crate::apis::urlencode(channel_id),
167        conversation_id = crate::apis::urlencode(conversation_id)
168    );
169    let mut local_var_req_builder =
170        local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
171
172    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
173        let local_var_key = local_var_apikey.key.clone();
174        let local_var_value = match local_var_apikey.prefix {
175            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
176            None => local_var_key,
177        };
178        local_var_req_builder = local_var_req_builder.query(&[("api_token", local_var_value)]);
179    }
180    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
181        local_var_req_builder =
182            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
183    }
184    if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
185        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
186    };
187
188    let local_var_req = local_var_req_builder.build()?;
189    let local_var_resp = local_var_client.execute(local_var_req).await?;
190
191    let local_var_status = local_var_resp.status();
192    let local_var_content = local_var_resp.text().await?;
193
194    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
195        serde_json::from_str(&local_var_content).map_err(Error::from)
196    } else {
197        let local_var_entity: Option<DeleteConversationError> =
198            serde_json::from_str(&local_var_content).ok();
199        let local_var_error = ResponseContent {
200            status: local_var_status,
201            content: local_var_content,
202            entity: local_var_entity,
203        };
204        Err(Error::ResponseError(local_var_error))
205    }
206}
207
208/// Adds a message to a conversation. To use the endpoint, you need to have **Messengers integration** OAuth scope enabled and the Messaging manifest ready for the [Messaging app extension](https://pipedrive.readme.io/docs/messaging-app-extension).
209pub async fn receive_message(
210    configuration: &configuration::Configuration,
211    receive_message_request: Option<crate::models::ReceiveMessageRequest>,
212) -> Result<crate::models::ReceiveMessage200Response, Error<ReceiveMessageError>> {
213    let local_var_configuration = configuration;
214
215    let local_var_client = &local_var_configuration.client;
216
217    let local_var_uri_str = format!(
218        "{}/channels/messages/receive",
219        local_var_configuration.base_path
220    );
221    let mut local_var_req_builder =
222        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
223
224    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
225        let local_var_key = local_var_apikey.key.clone();
226        let local_var_value = match local_var_apikey.prefix {
227            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
228            None => local_var_key,
229        };
230        local_var_req_builder = local_var_req_builder.query(&[("api_token", local_var_value)]);
231    }
232    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
233        local_var_req_builder =
234            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
235    }
236    if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
237        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
238    };
239    local_var_req_builder = local_var_req_builder.json(&receive_message_request);
240
241    let local_var_req = local_var_req_builder.build()?;
242    let local_var_resp = local_var_client.execute(local_var_req).await?;
243
244    let local_var_status = local_var_resp.status();
245    let local_var_content = local_var_resp.text().await?;
246
247    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
248        serde_json::from_str(&local_var_content).map_err(Error::from)
249    } else {
250        let local_var_entity: Option<ReceiveMessageError> =
251            serde_json::from_str(&local_var_content).ok();
252        let local_var_error = ResponseContent {
253            status: local_var_status,
254            content: local_var_content,
255            entity: local_var_entity,
256        };
257        Err(Error::ResponseError(local_var_error))
258    }
259}