twapi_v2/api/
post_2_dm_conversations.rs1use crate::{
2 api::{Authentication, TwapiOptions, execute_twitter, make_url},
3 error::Error,
4 headers::Headers,
5};
6use reqwest::RequestBuilder;
7use serde::{Deserialize, Serialize};
8
9const URL: &str = "/2/dm_conversations";
10
11#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
12pub enum ConversationType {
13 #[serde(rename = "Group")]
14 #[default]
15 Group,
16}
17
18impl std::fmt::Display for ConversationType {
19 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20 match self {
21 Self::Group => write!(f, "Group"),
22 }
23 }
24}
25
26#[derive(Serialize, Deserialize, Debug, Default, Clone)]
27pub struct Attachment {
28 pub media_id: String,
29}
30
31#[derive(Serialize, Deserialize, Debug, Default, Clone)]
32pub struct Message {
33 #[serde(skip_serializing_if = "Option::is_none")]
34 pub attachments: Option<Vec<Attachment>>,
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub text: Option<String>,
37}
38
39#[derive(Serialize, Deserialize, Debug, Default, Clone)]
40pub struct Body {
41 pub conversation_type: ConversationType,
42 pub participant_ids: Vec<String>,
43 pub message: Message,
44}
45
46#[derive(Debug, Clone, Default)]
47pub struct Api {
48 body: Body,
49 twapi_options: Option<TwapiOptions>,
50}
51
52impl Api {
53 pub fn new(body: Body) -> Self {
54 Self {
55 body,
56 ..Default::default()
57 }
58 }
59
60 pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
61 self.twapi_options = Some(value);
62 self
63 }
64
65 pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
66 let client = reqwest::Client::new();
67 let url = make_url(&self.twapi_options, URL);
68 let builder = client.post(&url).json(&self.body);
69 authentication.execute(builder, "POST", &url, &[])
70 }
71
72 pub async fn execute(
73 &self,
74 authentication: &impl Authentication,
75 ) -> Result<(Response, Headers), Error> {
76 execute_twitter(|| self.build(authentication), &self.twapi_options).await
77 }
78}
79
80#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
81pub struct Response {
82 #[serde(skip_serializing_if = "Option::is_none")]
83 pub data: Option<Data>,
84 #[serde(flatten)]
85 pub extra: std::collections::HashMap<String, serde_json::Value>,
86}
87
88impl Response {
89 pub fn is_empty_extra(&self) -> bool {
90 let res = self.extra.is_empty()
91 && self
92 .data
93 .as_ref()
94 .map(|it| it.is_empty_extra())
95 .unwrap_or(true);
96 if !res {
97 println!("Response {:?}", self.extra);
98 }
99 res
100 }
101}
102
103#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
104pub struct Data {
105 #[serde(skip_serializing_if = "Option::is_none")]
106 pub dm_conversation_id: Option<String>,
107 #[serde(skip_serializing_if = "Option::is_none")]
108 pub dm_event_id: Option<String>,
109 #[serde(flatten)]
110 pub extra: std::collections::HashMap<String, serde_json::Value>,
111}
112
113impl Data {
114 pub fn is_empty_extra(&self) -> bool {
115 let res = self.extra.is_empty();
116 if !res {
117 println!("Data {:?}", self.extra);
118 }
119 res
120 }
121}