slack_rust/conversations/
set_purpose.rs1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
7pub struct SetPurposeRequest {
8 pub channel: String,
9 pub purpose: String,
10}
11
12#[skip_serializing_none]
13#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
14pub struct SetPurposeResponse {
15 pub ok: bool,
16 pub error: Option<String>,
17 pub response_metadata: Option<ResponseMetadata>,
18 pub purpose: Option<String>,
19}
20
21pub async fn set_purpose<T>(
22 client: &T,
23 param: &SetPurposeRequest,
24 bot_token: &str,
25) -> Result<SetPurposeResponse, Error>
26where
27 T: SlackWebAPIClient,
28{
29 let url = get_slack_url("conversations.setPurpose");
30 let json = serde_json::to_string(¶m)?;
31
32 client
33 .post_json(&url, &json, bot_token)
34 .await
35 .and_then(|result| {
36 serde_json::from_str::<SetPurposeResponse>(&result).map_err(Error::SerdeJsonError)
37 })
38}
39
40#[cfg(test)]
41mod test {
42 use super::*;
43 use crate::http_client::MockSlackWebAPIClient;
44
45 #[test]
46 fn convert_request() {
47 let request = SetPurposeRequest {
48 channel: "C1234567890".to_string(),
49 purpose: "My More Special Purpose".to_string(),
50 };
51 let json = r##"{
52 "channel": "C1234567890",
53 "purpose": "My More Special Purpose"
54}"##;
55
56 let j = serde_json::to_string_pretty(&request).unwrap();
57 assert_eq!(json, j);
58
59 let s = serde_json::from_str::<SetPurposeRequest>(json).unwrap();
60 assert_eq!(request, s);
61 }
62
63 #[test]
64 fn convert_response() {
65 let response = SetPurposeResponse {
66 ok: true,
67 purpose: Some("I didn't set this purpose on purpose!".to_string()),
68 ..Default::default()
69 };
70 let json = r##"{
71 "ok": true,
72 "purpose": "I didn't set this purpose on purpose!"
73}"##;
74
75 let j = serde_json::to_string_pretty(&response).unwrap();
76 assert_eq!(json, j);
77
78 let s = serde_json::from_str::<SetPurposeResponse>(json).unwrap();
79 assert_eq!(response, s);
80 }
81
82 #[async_std::test]
83 async fn test_set_purpose() {
84 let param = SetPurposeRequest {
85 channel: "C1234567890".to_string(),
86 purpose: "My More Special Purpose".to_string(),
87 };
88
89 let mut mock = MockSlackWebAPIClient::new();
90 mock.expect_post_json().returning(|_, _, _| {
91 Ok(r##"{
92 "ok": true,
93 "purpose": "I didn't set this purpose on purpose!"
94}"##
95 .to_string())
96 });
97
98 let response = set_purpose(&mock, ¶m, &"test_token".to_string())
99 .await
100 .unwrap();
101 let expect = SetPurposeResponse {
102 ok: true,
103 purpose: Some("I didn't set this purpose on purpose!".to_string()),
104 ..Default::default()
105 };
106
107 assert_eq!(expect, response);
108 }
109}