Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multipart/form-data support #44

Merged
merged 30 commits into from
Sep 6, 2023
Merged

Conversation

tonyskansf
Copy link
Contributor

@tonyskansf tonyskansf commented Jun 19, 2023

This pull request adds multipart/form-data content type upload support on top of the existing functionality in #43. Also, the changes contain an example showcasing the usage of the API.

Implementation

MultipartFormDataEncoder

Provides two ways of encoding the MultipartFormData: either in-memory encoding or encoding to disk. In-memory encoding is suitable for small payloads while encoding to disk is preferred and more efficient for large payloads to prevent memory issues.

Regardless of the encoding type, all data are encoded into this representation:

--boundary-{some id}
Content-Disposition: form-data; name="field-name"

{Field data}
--boundary-{some id}
Content-Disposition: form-data; name="attachment"; filename="file.txt"
Content-Type: text/plain

{Contents of the file}
--boundary-{some id}--

Usage

let uploadManager = UploadAPIManager()

// Get form data from the user (multipart form body parts)
let textFieldData = Data(text.utf8)
let attachmentUrl = URL(string: "path/to/file")!

// Add them to the MultipartFormData instance
let multipartFormData = MultipartFormData(/* boundary: override boundary */)
multipartFormData.append(textFieldData, name: "text-field")
try multipartFormData.append(from: attachmentUrl, name: "attachment-field")

// Send request
try await uploadManager.upload(
  multipartFormData: multipartFormData,
  to: endpoint,
  retryConfiguration: .default
)

Copy link

@brenovaladao brenovaladao left a comment

Choose a reason for hiding this comment

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

🪙

@tonyskansf tonyskansf marked this pull request as ready for review June 21, 2023 05:55
Copy link
Collaborator

@cejanen cejanen left a comment

Choose a reason for hiding this comment

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

I left some comments in first round

@cejanen cejanen self-requested a review July 17, 2023 11:40
Copy link
Collaborator

@cejanen cejanen left a comment

Choose a reason for hiding this comment

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

One thing left on my side... @tonyskansf
otherwise we're done here 👍

@cejanen cejanen merged commit 70fb5a2 into feat/upload Sep 6, 2023
@cejanen cejanen deleted the feat/multipart-upload branch September 6, 2023 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants