slack_rust/team/
billable_info.rs1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::team::billing::BillableInfo;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6use std::collections::HashMap;
7
8#[skip_serializing_none]
9#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
10pub struct BillableInfoRequest {
11 pub team_id: Option<String>,
12 pub user: Option<String>,
13}
14
15#[skip_serializing_none]
16#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
17pub struct BillableInfoResponse {
18 pub ok: bool,
19 pub error: Option<String>,
20 pub response_metadata: Option<ResponseMetadata>,
21 pub billable_info: Option<HashMap<String, BillableInfo>>,
22}
23
24pub async fn billable_info<T>(
25 client: &T,
26 param: &BillableInfoRequest,
27 bot_token: &str,
28) -> Result<BillableInfoResponse, Error>
29where
30 T: SlackWebAPIClient,
31{
32 let url = get_slack_url("team.billableInfo");
33 let json = serde_json::to_string(¶m)?;
34
35 client
36 .post_json(&url, &json, bot_token)
37 .await
38 .and_then(|result| {
39 serde_json::from_str::<BillableInfoResponse>(&result).map_err(Error::SerdeJsonError)
40 })
41}
42
43#[cfg(test)]
44mod test {
45 use super::*;
46 use crate::http_client::MockSlackWebAPIClient;
47
48 #[test]
49 fn convert_request() {
50 let request = BillableInfoRequest {
51 team_id: Some("T1234567890".to_string()),
52 user: Some("W1234567890".to_string()),
53 };
54 let json = r##"{
55 "team_id": "T1234567890",
56 "user": "W1234567890"
57}"##;
58
59 let j = serde_json::to_string_pretty(&request).unwrap();
60 assert_eq!(json, j);
61
62 let s = serde_json::from_str::<BillableInfoRequest>(json).unwrap();
63 assert_eq!(request, s);
64 }
65
66 #[test]
67 fn convert_response() {
68 let mut billable_info = HashMap::new();
69 billable_info.insert(
70 "U02UCPE1R".to_string(),
71 BillableInfo {
72 billing_active: true,
73 },
74 );
75
76 let response = BillableInfoResponse {
77 ok: true,
78 billable_info: Some(billable_info),
79 ..Default::default()
80 };
81 let json = r##"{
82 "ok": true,
83 "billable_info": {
84 "U02UCPE1R": {
85 "billing_active": true
86 }
87 }
88}"##;
89
90 let j = serde_json::to_string_pretty(&response).unwrap();
91 assert_eq!(json, j);
92
93 let s = serde_json::from_str::<BillableInfoResponse>(json).unwrap();
94 assert_eq!(response, s);
95 }
96
97 #[async_std::test]
98 async fn test_billable_info() {
99 let param = BillableInfoRequest {
100 team_id: Some("T1234567890".to_string()),
101 ..Default::default()
102 };
103 let mut mock = MockSlackWebAPIClient::new();
104 mock.expect_post_json().returning(|_, _, _| {
105 Ok(r##"{
106 "ok": true,
107 "billable_info": {
108 "U02UCPE1R": {
109 "billing_active": true
110 }
111 }
112}"##
113 .to_string())
114 });
115
116 let response = billable_info(&mock, ¶m, &"test_token".to_string())
117 .await
118 .unwrap();
119
120 let mut billable_info = HashMap::new();
121 billable_info.insert(
122 "U02UCPE1R".to_string(),
123 BillableInfo {
124 billing_active: true,
125 },
126 );
127
128 let expect = BillableInfoResponse {
129 ok: true,
130 billable_info: Some(billable_info),
131 ..Default::default()
132 };
133
134 assert_eq!(expect, response);
135 }
136}