kean / Get

Web API client built using async/await
MIT License
937 stars 74 forks source link

multipart/form-data integration? #79

Closed AndrewSB closed 1 year ago

AndrewSB commented 1 year ago

I didn't see an example of how to do this, i'm using MultipartFormDataKit & what i'm doing is

return Future {
    let filename = UUID().uuidString
    let boundary = RandomBoundaryGenerator.generate()
    let multipartFormData = try MultipartFormDataKit.MultipartFormData.Builder.build(
        with: [(
            name: "file",
            filename: "\(filename).png",
            mimeType: MIMEType.imagePng,
            data: data
        )],
        willSeparateBy: boundary
    ).body

    return try await self.apiStore.send(Paths.me.picture.patch(multipartFormData), configure: {
        $0.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
    }).value
}

but it feels a little verbose at the callsite. my swagger definition says that the form should contain data at the key file, for example

is this the expected integration? or is there something a little less verbose?

kean commented 1 year ago

It looks good, but one thing I would look for is making sure you are not passing the binary data (Data) in the methods that accept Decodable. By default, JSONDecoder encodes binary data using base64, which is not what you want in most cases. I think you are good in this case, because APIClient/send doesn't use JSONDecoder when you pass Data.

For other questions, I'd suggest following the MultipartFormDataKit docs. Get is a micro-framework: there is nothing built-in by design.

AndrewSB commented 1 year ago

appreciate the lookover, and all the other work you put out @kean!

zabojad commented 3 months ago

@AndrewSB Hi ! Could you please tell me what Paths.me.picture.patch does ? Can you share its code ?