diff --git a/src/http/client.rs b/src/http/client.rs index 49ea81273ae..e14ff6d2ef9 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -26,6 +26,7 @@ use super::{ HttpError, LightMethod, MessagePagination, + MultipartUpload, UserPagination, }; use crate::builder::CreateAttachment; @@ -489,8 +490,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(files.into_iter().map(Into::into).collect()), - upload_file: None, + upload: MultipartUpload::Attachments(files.into_iter().map(Into::into).collect()), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -690,8 +690,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(files.into_iter().map(Into::into).collect()), - upload_file: None, + upload: MultipartUpload::Attachments(files.into_iter().map(Into::into).collect()), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -861,8 +860,7 @@ impl Http { self.fire(Request { body: None, multipart: Some(Multipart { - attachment_files: None, - upload_file: Some(file), + upload: MultipartUpload::File(file), fields: map.into_iter().map(|(k, v)| (k.into(), v.into())).collect(), payload_json: None, }), @@ -1525,8 +1523,9 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(new_attachments.into_iter().map(Into::into).collect()), - upload_file: None, + upload: MultipartUpload::Attachments( + new_attachments.into_iter().map(Into::into).collect(), + ), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -1814,8 +1813,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(new_attachments), - upload_file: None, + upload: MultipartUpload::Attachments(new_attachments), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -1961,8 +1959,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(new_attachments.into_iter().collect()), - upload_file: None, + upload: MultipartUpload::Attachments(new_attachments.into_iter().collect()), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -2415,8 +2412,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(files.into_iter().collect()), - upload_file: None, + upload: MultipartUpload::Attachments(files.into_iter().collect()), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -2481,8 +2477,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(new_attachments), - upload_file: None, + upload: MultipartUpload::Attachments(new_attachments), payload_json: Some(to_string(map)?), fields: vec![], }); @@ -4224,8 +4219,7 @@ impl Http { request.body = Some(to_vec(map)?); } else { request.multipart = Some(Multipart { - attachment_files: Some(files.into_iter().collect()), - upload_file: None, + upload: MultipartUpload::Attachments(files.into_iter().collect()), payload_json: Some(to_string(map)?), fields: vec![], }); diff --git a/src/http/multipart.rs b/src/http/multipart.rs index d9467481432..3c071a819d0 100644 --- a/src/http/multipart.rs +++ b/src/http/multipart.rs @@ -14,14 +14,19 @@ impl CreateAttachment { } } +#[derive(Clone, Debug)] +pub enum MultipartUpload { + /// A file sent with the form data as an individual upload. For example, a sticker. + File(CreateAttachment), + /// Files sent with the form as message attachments. + Attachments(Vec), +} + /// Holder for multipart body. Contains files, multipart fields, and payload_json for creating /// requests with attachments. #[derive(Clone, Debug)] pub struct Multipart { - /// Files that are sent with the form data as individual uploads. - pub upload_file: Option, - /// Files that are sent with the form data as message attachments. - pub attachment_files: Option>, + pub upload: MultipartUpload, /// Multipart text fields that are sent with the form data as individual fields. If a certain /// endpoint does not support passing JSON body via `payload_json`, this must be used instead. pub fields: Vec<(Cow<'static, str>, Cow<'static, str>)>, @@ -33,14 +38,15 @@ impl Multipart { pub(crate) fn build_form(self) -> Result
{ let mut multipart = Form::new(); - if let Some(upload_file) = self.upload_file { - multipart = multipart.part("file", upload_file.into_part()?); - } - - if let Some(attachment_files) = self.attachment_files { - for file in attachment_files { - multipart = multipart.part(format!("files[{}]", file.id), file.into_part()?); - } + match self.upload { + MultipartUpload::File(upload_file) => { + multipart = multipart.part("file", upload_file.into_part()?); + }, + MultipartUpload::Attachments(attachment_files) => { + for file in attachment_files { + multipart = multipart.part(format!("files[{}]", file.id), file.into_part()?); + } + }, } for (name, value) in self.fields {