Skip to main content

twapi_v2/api/
post_2_media_upload_id_finalize.rs

1use crate::responses::errors::Errors;
2use crate::responses::media_upload::MediaUpload;
3use crate::{
4    api::{Authentication, TwapiOptions, apply_options, execute_twitter, make_url},
5    error::Error,
6    headers::Headers,
7};
8use reqwest::RequestBuilder;
9use serde::{Deserialize, Serialize};
10
11const URL: &str = "/2/media/upload/:id/finalize";
12
13#[derive(Debug, Clone, Default)]
14pub struct Api {
15    id: String,
16    twapi_options: Option<TwapiOptions>,
17}
18
19impl Api {
20    pub fn new(id: &str) -> Self {
21        Self {
22            id: id.to_owned(),
23            ..Default::default()
24        }
25    }
26
27    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
28        self.twapi_options = Some(value);
29        self
30    }
31
32    pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
33        let client = reqwest::Client::new();
34        let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
35        let builder = client.post(&url);
36        authentication.execute(
37            apply_options(builder, &self.twapi_options),
38            "POST",
39            &url,
40            &[],
41        )
42    }
43
44    pub async fn execute(
45        self,
46        authentication: &impl Authentication,
47    ) -> Result<(Response, Headers), Error> {
48        execute_twitter(self.build(authentication)).await
49    }
50}
51
52#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
53pub struct Response {
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub data: Option<MediaUpload>,
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub errors: Option<Vec<Errors>>,
58    #[serde(flatten)]
59    pub extra: std::collections::HashMap<String, serde_json::Value>,
60}
61
62impl Response {
63    pub fn is_empty_extra(&self) -> bool {
64        let res = self.extra.is_empty()
65            && self
66                .data
67                .as_ref()
68                .map(|it| it.is_empty_extra())
69                .unwrap_or(true)
70            && self
71                .errors
72                .as_ref()
73                .map(|it| it.iter().all(|item| item.is_empty_extra()))
74                .unwrap_or(true);
75        if !res {
76            println!("Response {:?}", self.extra);
77        }
78        res
79    }
80}