twapi_v2/api/
post_2_media_upload_id_append.rs1use std::io::Cursor;
2
3use crate::{
4 api::{Authentication, TwapiOptions, apply_options, make_url},
5 error::Error,
6 headers::Headers,
7};
8use reqwest::RequestBuilder;
9use reqwest::multipart::{Form, Part};
10
11const URL: &str = "/2/media/upload/:id/append";
12
13#[derive(Debug, Clone, Default)]
14pub struct FormData {
15 pub segment_index: u64,
16 pub cursor: Cursor<Vec<u8>>,
17}
18
19impl FormData {
20 fn make_form(self) -> Form {
21 Form::new()
22 .text("segment_index", self.segment_index.to_string())
23 .part("media", Part::bytes(self.cursor.into_inner()))
24 }
25}
26
27#[derive(Debug, Clone, Default)]
28pub struct Api {
29 id: String,
30 form: FormData,
31 twapi_options: Option<TwapiOptions>,
32}
33
34impl Api {
35 pub fn new(id: &str, form: FormData) -> Self {
36 Self {
37 id: id.to_string(),
38 form,
39 ..Default::default()
40 }
41 }
42
43 pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
44 self.twapi_options = Some(value);
45 self
46 }
47
48 pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
49 let client = reqwest::Client::new();
50 let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
51 let builder = client.post(&url).multipart(self.form.make_form());
52 authentication.execute(
53 apply_options(builder, &self.twapi_options),
54 "POST",
55 &url,
56 &[],
57 )
58 }
59
60 pub async fn execute(self, authentication: &impl Authentication) -> Result<Headers, Error> {
61 let response = self.build(authentication).send().await?;
62 let status_code = response.status();
63 let header = response.headers();
64 let headers = Headers::new(header);
65 if status_code.is_success() {
66 Ok(headers)
67 } else {
68 let body = response.text().await?;
69 Err(Error::Other(body, Some(status_code)))
70 }
71 }
72}