Skip to main content

twapi_v2/api/
post_2_dm_conversations.rs

1use crate::{
2    api::{Authentication, TwapiOptions, apply_options, 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(
70            apply_options(builder, &self.twapi_options),
71            "POST",
72            &url,
73            &[],
74        )
75    }
76
77    pub async fn execute(
78        self,
79        authentication: &impl Authentication,
80    ) -> Result<(Response, Headers), Error> {
81        execute_twitter(self.build(authentication)).await
82    }
83}
84
85#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
86pub struct Response {
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub data: Option<Data>,
89    #[serde(flatten)]
90    pub extra: std::collections::HashMap<String, serde_json::Value>,
91}
92
93impl Response {
94    pub fn is_empty_extra(&self) -> bool {
95        let res = self.extra.is_empty()
96            && self
97                .data
98                .as_ref()
99                .map(|it| it.is_empty_extra())
100                .unwrap_or(true);
101        if !res {
102            println!("Response {:?}", self.extra);
103        }
104        res
105    }
106}
107
108#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
109pub struct Data {
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub dm_conversation_id: Option<String>,
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub dm_event_id: Option<String>,
114    #[serde(flatten)]
115    pub extra: std::collections::HashMap<String, serde_json::Value>,
116}
117
118impl Data {
119    pub fn is_empty_extra(&self) -> bool {
120        let res = self.extra.is_empty();
121        if !res {
122            println!("Data {:?}", self.extra);
123        }
124        res
125    }
126}