Skip to main content

twapi_v2/upload/
post_media_upload_init.rs

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