You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simplest use case - we need to check status code and in case of error - print the response body.
The generator generates:
type createObjectResponse struct {
Body []byte
HTTPResponse *http.Response
JSON201 *ObjectCreateResponse
}
// Status returns HTTPResponse.Status
func (r createObjectResponse) Status() string {
if r.HTTPResponse != nil {
return r.HTTPResponse.Status
}
return http.StatusText(0)
}
// StatusCode returns HTTPResponse.StatusCode
func (r createObjectResponse) StatusCode() int {
if r.HTTPResponse != nil {
return r.HTTPResponse.StatusCode
}
return 0
}
the HTTPResponse doesn't have body anymore, since it was consumed during parsing and populating Body and JSON201. Since every response is a different struct type - you would have to handle them case by case in every place you perform a call. There's no way we can introduce common handler methods for all responses.
The idea (although I believe has some implications..) is to add a new method for each response GetBody() []byte (Body() is reserved by the field).
This way we can now introduce our own common interface for the responses without introducing any interfaces to oapi-codegen.
type XXX interface {
StatusCode() int
}
Additionally, we could add the following snippet to client-with-responses.tmpl so the interface is available out of the box.
type Response interface {
Status() string
StatusCode() int
GetBody() []byte
}
PS: Currently, we have overriden the client-with-responses.tmpl in our project to have both of the mentioned improvements. I am still interested in your thoughts whether you would want to put those improvements into the oapi-codegen.
I'm ready to open a PR for the improvements if needed.
The text was updated successfully, but these errors were encountered:
ernestas2k
changed the title
Add common interface for responses in ClientWithResponses
Add body method and common interface to responses in ClientWithResponses
Oct 14, 2020
As noted in #240, it may be useful to have a more convenient means to
access the underlying bytes of a given response type, without using the
`r.Body` field, we can introduce a new method, `Bytes()`.
This convenience method should not be generated by default, as it
introduces a large diff, and may not be expected, and is wired in with a
new Output Option.
Closes#240.
Co-authored-by: Jamie Tanna <[email protected]>
Description
Simplest use case - we need to check status code and in case of error - print the response body.
The generator generates:
the
HTTPResponse
doesn't havebody
anymore, since it was consumed during parsing and populatingBody
andJSON201
. Since every response is a different struct type - you would have to handle them case by case in every place you perform a call. There's no way we can introduce common handler methods for all responses.GetBody() []byte
(Body()
is reserved by the field).This way we can now introduce our own common interface for the responses without introducing any interfaces to oapi-codegen.
client-with-responses.tmpl
so the interface is available out of the box.PS: Currently, we have overriden the
client-with-responses.tmpl
in our project to have both of the mentioned improvements. I am still interested in your thoughts whether you would want to put those improvements into the oapi-codegen.I'm ready to open a PR for the improvements if needed.
The text was updated successfully, but these errors were encountered: