RestBird icon indicating copy to clipboard operation
RestBird copied to clipboard

Make use of function builders for a more dynamic syntax

Open magyarosibotond opened this issue 3 years ago • 0 comments

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 avatar Aug 18 '20 08:08 magyarosibotond