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

Add body method and common interface to responses in ClientWithResponses #240

Closed
ernestas2k opened this issue Oct 14, 2020 · 0 comments · Fixed by #1780
Closed

Add body method and common interface to responses in ClientWithResponses #240

ernestas2k opened this issue Oct 14, 2020 · 0 comments · Fixed by #1780

Comments

@ernestas2k
Copy link

ernestas2k commented Oct 14, 2020

Description

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.

  1. 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
}
  1. 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.

@ernestas2k ernestas2k changed the title Add common interface for responses in ClientWithResponses Add body method and common interface to responses in ClientWithResponses Oct 14, 2020
jamietanna added a commit that referenced this issue Nov 28, 2024
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant