modio/request/files/multipart/
add_multipart_upload_file.rs

1use std::future::IntoFuture;
2
3use serde_derive::Serialize;
4
5use crate::client::Client;
6use crate::error;
7use crate::request::multipart::{Form, Part};
8use crate::request::{Output, RequestBuilder, Route};
9use crate::response::ResponseFuture;
10use crate::types::files::multipart::UploadId;
11use crate::types::files::File;
12use crate::types::id::{GameId, ModId};
13
14pub struct AddMultipartUploadFile<'a> {
15    http: &'a Client,
16    game_id: GameId,
17    mod_id: ModId,
18    fields: AddMultipartUploadFileFields<'a>,
19}
20
21#[derive(Serialize)]
22struct AddMultipartUploadFileFields<'a> {
23    upload_id: UploadId,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    active: Option<bool>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    changelog: Option<&'a str>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    filehash: Option<&'a str>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    metadata_blob: Option<&'a str>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    version: Option<&'a str>,
34}
35
36impl<'a> AddMultipartUploadFile<'a> {
37    pub(crate) const fn new(
38        http: &'a Client,
39        game_id: GameId,
40        mod_id: ModId,
41        upload_id: UploadId,
42    ) -> Self {
43        Self {
44            http,
45            game_id,
46            mod_id,
47            fields: AddMultipartUploadFileFields {
48                upload_id,
49                active: None,
50                changelog: None,
51                filehash: None,
52                metadata_blob: None,
53                version: None,
54            },
55        }
56    }
57
58    pub const fn active(mut self, active: bool) -> Self {
59        self.fields.active = Some(active);
60        self
61    }
62
63    pub const fn changelog(mut self, changelog: &'a str) -> Self {
64        self.fields.changelog = Some(changelog);
65        self
66    }
67
68    pub const fn filehash(mut self, filehash: &'a str) -> Self {
69        self.fields.filehash = Some(filehash);
70        self
71    }
72
73    pub const fn metadata_blob(mut self, metadata: &'a str) -> Self {
74        self.fields.metadata_blob = Some(metadata);
75        self
76    }
77
78    pub const fn version(mut self, version: &'a str) -> Self {
79        self.fields.version = Some(version);
80        self
81    }
82}
83
84impl IntoFuture for AddMultipartUploadFile<'_> {
85    type Output = Output<File>;
86    type IntoFuture = ResponseFuture<File>;
87
88    fn into_future(self) -> Self::IntoFuture {
89        let route = Route::AddFile {
90            game_id: self.game_id,
91            mod_id: self.mod_id,
92        };
93
94        let mut form = Form::new();
95        form = form.text("upload_id", self.fields.upload_id.to_string());
96
97        if let Some(value) = self.fields.active {
98            form = form.text("active", value.to_string());
99        }
100        if let Some(value) = self.fields.changelog {
101            form = form.text("changelog", value.to_owned());
102        }
103        if let Some(value) = self.fields.metadata_blob {
104            form = form.text("metadata_blob", value.to_owned());
105        }
106        if let Some(value) = self.fields.version {
107            form = form.text("version", value.to_owned());
108        }
109
110        form = match serde_json::to_vec(&self.fields) {
111            Ok(json) => form.part("input_json", Part::bytes(json.into())),
112            Err(err) => return ResponseFuture::failed(error::builder(err)),
113        };
114
115        match RequestBuilder::from_route(&route).multipart(form) {
116            Ok(req) => self.http.request(req),
117            Err(err) => ResponseFuture::failed(err),
118        }
119    }
120}