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

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

Open
magyarosibotond opened this issue Aug 18, 2020 · 0 comments
Open

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

magyarosibotond opened this issue Aug 18, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@magyarosibotond
Copy link
Contributor

magyarosibotond commented Aug 18, 2020

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

@magyarosibotond magyarosibotond added the enhancement New feature or request label Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant