Skip to main content

twapi_v2/api/
put_2_lists_id.rs

1use 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/lists/:id";
10
11#[derive(Serialize, Deserialize, Debug, Default, Clone)]
12pub struct Body {
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub name: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub description: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub private: Option<bool>,
19}
20
21#[derive(Debug, Clone, Default)]
22pub struct Api {
23    id: String,
24    body: Body,
25    twapi_options: Option<TwapiOptions>,
26}
27
28impl Api {
29    pub fn new(id: &str, body: Body) -> Self {
30        Self {
31            id: id.to_owned(),
32            body,
33            ..Default::default()
34        }
35    }
36
37    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
38        self.twapi_options = Some(value);
39        self
40    }
41
42    pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
43        let client = reqwest::Client::new();
44        let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
45        let builder = client.put(&url).json(&self.body);
46        authentication.execute(builder, "PUT", &url, &[])
47    }
48
49    pub async fn execute(
50        &self,
51        authentication: &impl Authentication,
52    ) -> Result<(Response, Headers), Error> {
53        execute_twitter(|| self.build(authentication), &self.twapi_options).await
54    }
55}
56
57#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
58pub struct Response {
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub data: Option<Data>,
61    #[serde(flatten)]
62    pub extra: std::collections::HashMap<String, serde_json::Value>,
63}
64
65impl Response {
66    pub fn is_empty_extra(&self) -> bool {
67        let res = self.extra.is_empty()
68            && self
69                .data
70                .as_ref()
71                .map(|it| it.is_empty_extra())
72                .unwrap_or(true);
73        if !res {
74            println!("Response {:?}", self.extra);
75        }
76        res
77    }
78}
79
80#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
81pub struct Data {
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub updated: Option<bool>,
84    #[serde(flatten)]
85    pub extra: std::collections::HashMap<String, serde_json::Value>,
86}
87
88impl Data {
89    pub fn is_empty_extra(&self) -> bool {
90        let res = self.extra.is_empty();
91        if !res {
92            println!("Data {:?}", self.extra);
93        }
94        res
95    }
96}