kean / Get

Web API client built using async/await
MIT License
943 stars 75 forks source link

Duplicating requests with different body #13

Closed picoHz closed 2 years ago

picoHz commented 2 years ago

Hi. I want to make multiple requests whose parameters are identical except for their body. Since requests can be created only by the factory methods, I have to write code like this to duplicate requests. Is there a better way to handle this case?

func duplicate<Response: Decodable, Body: Encodable>(request: Request<Response>, body: Body) -> Request<Response> {
        switch request.method {
    case "post":
        return .post(request.path, query: request.query, body: body, headers: request.headers)
    case "put":
        return .put(request.path, query: request.query, body: body, headers: request.headers)
    case "delete":
        return .delete(request.path, query: request.query, body: body, headers: request.headers)
    default:
        return .get(request.path)
    }
}
kean commented 2 years ago

I added a public Request initializer in 0.4.0.