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, 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(builder, "POST", &url, &[])
37    }
38
39    pub async fn execute(
40        &self,
41        authentication: &impl Authentication,
42    ) -> Result<(Response, Headers), Error> {
43        execute_twitter(|| self.build(authentication), &self.twapi_options).await
44    }
45}
46
47#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
48pub struct Response {
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub data: Option<MediaUpload>,
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub errors: Option<Vec<Errors>>,
53    #[serde(flatten)]
54    pub extra: std::collections::HashMap<String, serde_json::Value>,
55}
56
57impl Response {
58    pub fn is_empty_extra(&self) -> bool {
59        let res = self.extra.is_empty()
60            && self
61                .data
62                .as_ref()
63                .map(|it| it.is_empty_extra())
64                .unwrap_or(true)
65            && self
66                .errors
67                .as_ref()
68                .map(|it| it.iter().all(|item| item.is_empty_extra()))
69                .unwrap_or(true);
70        if !res {
71            println!("Response {:?}", self.extra);
72        }
73        res
74    }
75}