Skip to main content

twapi_v2/api/
put_2_lists_id.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/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(
47            apply_options(builder, &self.twapi_options),
48            "PUT",
49            &url,
50            &[],
51        )
52    }
53
54    pub async fn execute(
55        self,
56        authentication: &impl Authentication,
57    ) -> Result<(Response, Headers), Error> {
58        execute_twitter(self.build(authentication)).await
59    }
60}
61
62#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
63pub struct Response {
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub data: Option<Data>,
66    #[serde(flatten)]
67    pub extra: std::collections::HashMap<String, serde_json::Value>,
68}
69
70impl Response {
71    pub fn is_empty_extra(&self) -> bool {
72        let res = self.extra.is_empty()
73            && self
74                .data
75                .as_ref()
76                .map(|it| it.is_empty_extra())
77                .unwrap_or(true);
78        if !res {
79            println!("Response {:?}", self.extra);
80        }
81        res
82    }
83}
84
85#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
86pub struct Data {
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub updated: Option<bool>,
89    #[serde(flatten)]
90    pub extra: std::collections::HashMap<String, serde_json::Value>,
91}
92
93impl Data {
94    pub fn is_empty_extra(&self) -> bool {
95        let res = self.extra.is_empty();
96        if !res {
97            println!("Data {:?}", self.extra);
98        }
99        res
100    }
101}