twapi_v2/api/
post_2_dm_conversations.rs

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