Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions twilight-http/src/request/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ impl RequestBuilder {
self
}

/// Set the multipart form.
#[allow(clippy::missing_const_for_fn)]
pub fn form(mut self, form: Form) -> Self {
/// Set the the multipart form from an existing boundary and buffer.
pub fn multipart(mut self, boundary: [u8; 15], buffer: Vec<u8>) -> Self {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this. This is essentially a wrapper around the new Form::from_parts method and inlines the new RequestBuilder::form. Because Form is publicly exported from twilight_http::request I think it would be simpler to make Form::from_parts public and have users call the new RequestBuilder::form method. This way we're only increasing the API surface of this type by one method instead of two.

To summarize:

  • Remove multipart
  • Make Form::from_parts public

What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I've added this method was for it to match the json/body methods where you have one method which expects a Serializable struct and one which expects raw bytes.

So in case of forms I made it so now have one which expects a Form and one which expects raw bytes.

Asking users to call two methods (Form::from_parts and then RequestBuilder::form) sounds like a worse user experience but I get where you are coming from.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to hear opinions on this matter from a couple more people.

if let Ok(request) = self.0.as_mut() {
request.form = Some(form);
request.form = Some(Form::from_parts(boundary, buffer));
}

self
Expand All @@ -121,6 +120,15 @@ impl RequestBuilder {
self
}

/// Set the multipart form.
pub fn form(mut self, form: Form) -> Self {
if let Ok(request) = self.0.as_mut() {
request.form = Some(form);
}

self
}

/// Whether to use the client's authorization token in the request, if one
/// is set.
///
Expand Down
8 changes: 8 additions & 0 deletions twilight-http/src/request/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ impl Form {
Self::default()
}

pub(crate) const fn from_parts(boundary: [u8; 15], buffer: Vec<u8>) -> Self {
Self { boundary, buffer }
}

/// Consume the form, returning the buffer's contents.
pub fn build(mut self) -> Vec<u8> {
self.buffer.extend(Self::BOUNDARY_TERMINATOR);
Expand Down Expand Up @@ -106,6 +110,10 @@ impl Form {

self
}

pub fn into_parts(self) -> ([u8; 15], Vec<u8>) {
(self.boundary, self.buffer)
}
}

impl Default for Form {
Expand Down
Loading