Skip to content

Make use of function builders for a more dynamic syntax #32

@magyarosibotond

Description

@magyarosibotond

Is your feature request related to a problem? Please describe.
When defining API requests one particular issue we're always facing is where the data should be encoded in multiple places. This might happen in cases when the API is badly designed. The other issue is that due to the underlying representation of multipart requests, the syntax is strange and hard to use in the current setup.

Describe the solution you'd like
We could make use of function builders for a more dynamic syntax. There are two solutions that I've considered.

let request = DataRequest("/items/{id}", .post) {
     path(id, "id")
     body(todo) // root level encoding
     body(todo, "body") // encode with "body" key.
     multipart(data: image, name: "image.jpeg")
}

Describe alternatives you've considered
Another solution I've considered is to have a "build" (the name needs to be clarified) variable (SwiftUI like) that builds the request when the session manager needs it.

struct MyRequest: Request {
    let id: Int 
    let todo: Todo
    let image: Data

    @RequestBuilder
    var request: URLRequest {
       path(id, "id")
       body(todo) // root level encoding
       body(todo, "body") // encode with "body" key.
       multipart(data: image, name: "image.jpeg")
    }
}

The downside of this approach is that a Request object needs to be defined, and all the properties should be stored in the object.

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions