Skip to content

Commit

Permalink
Add MultipartUpload enum
Browse files Browse the repository at this point in the history
  • Loading branch information
kangalio committed Oct 30, 2023
1 parent 7560377 commit f8275b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
30 changes: 12 additions & 18 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use super::{
HttpError,
LightMethod,
MessagePagination,
MultipartUpload,
UserPagination,
};
use crate::builder::CreateAttachment;
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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,
}),
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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![],
});
Expand Down Expand Up @@ -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![],
});
Expand Down
29 changes: 17 additions & 12 deletions src/http/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ impl CreateAttachment {
}
}

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<CreateAttachment>),
}

/// 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<CreateAttachment>,
/// Files that are sent with the form data as message attachments.
pub attachment_files: Option<Vec<CreateAttachment>>,
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>)>,
Expand All @@ -33,14 +37,15 @@ impl Multipart {
pub(crate) fn build_form(self) -> Result<Form> {
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 {
Expand Down

0 comments on commit f8275b6

Please sign in to comment.