1use crate::client::{ContentType, Request};
2use reqwest::Method;
3use serde::{Deserialize, Serialize};
4
5pub const ENDPOINT: &str = "dysmsapi.aliyuncs.com";
6
7#[derive(Debug, Serialize, Deserialize, Clone)]
8#[serde(rename_all = "PascalCase")]
9pub struct SmsSendRequest {
10 pub template_code: String,
11 #[serde(flatten)]
12 pub data: SmsData,
13 #[serde(skip_serializing_if = "Option::is_none")]
14 pub out_id: Option<String>,
15}
16
17#[derive(Debug, Serialize, Deserialize, Clone)]
18#[serde(rename_all = "PascalCase")]
19pub struct SmsData {
20 pub phone_numbers: String,
21 pub sign_name: String,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub template_param: Option<String>,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub sms_up_extend_code: Option<String>,
26}
27
28impl SmsData {
29 pub fn new(
30 phone_numbers: Vec<String>,
31 sign_name: String,
32 template_param: Option<Vec<(String, String)>>,
33 sms_up_extend_code: Option<String>,
34 ) -> Self {
35 Self {
36 phone_numbers: itertools::join(phone_numbers, ","),
37 sign_name,
38 template_param: template_param.map(|v| {
39 let v = itertools::join(v.iter().map(|(k, v)| format!("\"{}\":\"{}\"", k, v)), ",");
40 format!("{{{}}}", v)
41 }),
42 sms_up_extend_code,
43 }
44 }
45}
46
47impl From<SmsSendRequest> for Request<SmsSendRequest> {
48 fn from(value: SmsSendRequest) -> Self {
49 Self {
50 action: "SendSms".to_string(),
51 ssl: true,
52 uri: "/".to_string(),
53 headers: None,
54 queries: None,
55 data: Some(value),
56 method: Method::POST,
57 content_type: ContentType::Form,
58 version: "2017-05-25".to_string(),
59 }
60 }
61}
62#[derive(Debug, Serialize, Deserialize)]
63#[serde(rename_all = "PascalCase")]
64pub struct SmsSendResponse {
65 pub biz_id: String,
66}
67
68#[derive(Debug, Serialize, Deserialize, Clone)]
69#[serde(rename_all = "PascalCase")]
70pub struct SendBatchSmsRequest {
71 pub phone_number_json: String,
72 pub sign_name_json: String,
73 pub template_code: String,
74 #[serde(skip_serializing_if = "Option::is_none")]
75 pub template_param_json: Option<String>,
76 #[serde(skip_serializing_if = "Option::is_none")]
77 pub sms_up_encode_code_json: Option<String>,
78 #[serde(skip_serializing_if = "Option::is_none")]
79 pub out_id: Option<String>,
80}
81
82impl SendBatchSmsRequest {
83 pub fn new(
84 template_code: String,
85 data: Vec<SmsData>,
86 out_id: Option<String>,
87 ) -> crate::Result<Self> {
88 let temp = data.into_iter().fold(
89 (Vec::new(), Vec::new(), Vec::new(), Vec::new()),
90 |mut acc, item| {
91 acc.0.push(item.phone_numbers);
92 acc.1.push(item.sign_name);
93 acc.2.push(item.template_param);
94 acc.3.push(item.sms_up_extend_code);
95 acc
96 },
97 );
98 let phone_number_json = serde_json::to_string(&temp.0)?;
99 let sign_name_json = serde_json::to_string(&temp.1)?;
100 let template_param_json = serde_json::to_string(&temp.2)?;
101 let sms_up_encode_code_json = serde_json::to_string(&temp.3)?;
102 Ok(Self {
103 phone_number_json,
104 sign_name_json,
105 template_code,
106 template_param_json: Some(template_param_json),
107 sms_up_encode_code_json: Some(sms_up_encode_code_json),
108 out_id,
109 })
110 }
111}