Skip to main content

twapi_v2/upload/
post_media_upload_finalize.rs

1use crate::{
2    api::{Authentication, TwapiOptions, execute_twitter},
3    error::Error,
4    headers::Headers,
5    upload::{make_url, response::Response},
6};
7use reqwest::{RequestBuilder, multipart::Form};
8
9#[derive(Debug, Clone, Default)]
10pub struct Data {
11    pub media_id: String,
12}
13
14impl Data {
15    fn make_form(self) -> Form {
16        Form::new()
17            .text("command", "FINALIZE")
18            .text("media_id", self.media_id)
19    }
20}
21
22#[derive(Debug, Clone, Default)]
23pub struct Api {
24    data: Data,
25    twapi_options: Option<TwapiOptions>,
26}
27
28impl Api {
29    pub fn new(data: Data) -> Self {
30        Self {
31            data,
32            ..Default::default()
33        }
34    }
35
36    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
37        self.twapi_options = Some(value);
38        self
39    }
40
41    pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
42        let client = reqwest::Client::new();
43        let url = make_url(&self.twapi_options, None);
44        let builder = client.post(&url).multipart(self.data.make_form());
45        authentication.execute(builder, "POST", &url, &[])
46    }
47
48    pub async fn execute(
49        self,
50        authentication: &impl Authentication,
51    ) -> Result<(Response, Headers), Error> {
52        execute_twitter(self.build(authentication)).await
53    }
54}