Skip to main content

twapi_v2/upload/
post_media_upload_append.rs

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