Skip to content

Commit

Permalink
chore Refactor eliminating readCloser (#3)
Browse files Browse the repository at this point in the history
* chore: refactor eliminating readCloser

* chore: close body
  • Loading branch information
henomis authored Mar 18, 2023
1 parent 8610976 commit a31cdcb
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type MyResponse struct {
// ...
}

func (r *MyResponse) Decode(body io.ReadCloser) error {
func (r *MyResponse) Decode(body io.Reader) error {
// Decode the response body into the response model
}

Expand Down
10 changes: 6 additions & 4 deletions examples/cmd/context/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ type TodoResponse struct {
Comleted bool `json:"completed"`
}

func (r *TodoResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *TodoResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}

func (r *TodoResponse) SetBody(body io.Reader) {
func (r *TodoResponse) SetBody(body io.Reader) error {
return nil

}

func (r *TodoResponse) AcceptContentType() string {
return "application/json"
}

func (r *TodoResponse) SetStatusCode(code int) {
func (r *TodoResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
11 changes: 7 additions & 4 deletions examples/cmd/delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ type DeletePostResponse struct {
HTTPStatusCode int `json:"-"`
}

func (r *DeletePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *DeletePostResponse) Decode(body io.Reader) error {

return nil
}

func (r *DeletePostResponse) SetBody(body io.Reader) {}
func (r *DeletePostResponse) SetBody(body io.Reader) error {
return nil
}

func (r *DeletePostResponse) AcceptContentType() string {
return ""
}

func (r *DeletePostResponse) SetStatusCode(code int) {
func (r *DeletePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
11 changes: 7 additions & 4 deletions examples/cmd/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ type TodoResponse struct {
Comleted bool `json:"completed"`
}

func (r *TodoResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *TodoResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}

func (r *TodoResponse) SetBody(body io.Reader) {}
func (r *TodoResponse) SetBody(body io.Reader) error {
return nil
}

func (r *TodoResponse) AcceptContentType() string {
return "application/json"
}

func (r *TodoResponse) SetStatusCode(code int) {
func (r *TodoResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
11 changes: 7 additions & 4 deletions examples/cmd/patch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ type UpdatePostResponse struct {
Body string `json:"body,omitempty"`
}

func (r *UpdatePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *UpdatePostResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}

func (r *UpdatePostResponse) SetBody(body io.Reader) {}
func (r *UpdatePostResponse) SetBody(body io.Reader) error {
return nil
}

func (r *UpdatePostResponse) AcceptContentType() string {
return "application/json"
}

func (r *UpdatePostResponse) SetStatusCode(code int) {
func (r *UpdatePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
11 changes: 7 additions & 4 deletions examples/cmd/post/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,22 @@ type CreatePostResponse struct {
Body string `json:"body"`
}

func (r *CreatePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *CreatePostResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}

func (r *CreatePostResponse) SetBody(body io.Reader) {}
func (r *CreatePostResponse) SetBody(body io.Reader) error {
return nil
}

func (r *CreatePostResponse) AcceptContentType() string {
return "application/json"
}

func (r *CreatePostResponse) SetStatusCode(code int) {
func (r *CreatePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
11 changes: 7 additions & 4 deletions examples/cmd/put/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ type UpdatePostResponse struct {
Body string `json:"body"`
}

func (r *UpdatePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *UpdatePostResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}

func (r *UpdatePostResponse) SetBody(body io.Reader) {}
func (r *UpdatePostResponse) SetBody(body io.Reader) error {
return nil
}

func (r *UpdatePostResponse) AcceptContentType() string {
return "application/json"
}

func (r *UpdatePostResponse) SetStatusCode(code int) {
func (r *UpdatePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
11 changes: 7 additions & 4 deletions examples/cmd/url_values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ type CommentsResponse struct {
} `json:"data"`
}

func (r *CommentsResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *CommentsResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(&r.Data)
}

func (r *CommentsResponse) SetBody(body io.Reader) {}
func (r *CommentsResponse) SetBody(body io.Reader) error {
return nil
}

func (r *CommentsResponse) AcceptContentType() string {
return "application/json"
}

func (r *CommentsResponse) SetStatusCode(code int) {
func (r *CommentsResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

func main() {
Expand Down
52 changes: 36 additions & 16 deletions restclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ type TodoResponse struct {
func (r *todoRequest) Path() (string, error) { return "/todos/" + r.ID, nil }
func (r *todoRequest) Encode() (io.Reader, error) { return nil, nil }
func (r *todoRequest) ContentType() string { return "" }
func (r *TodoResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *TodoResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}
func (r *TodoResponse) SetBody(body io.Reader) {}
func (r *TodoResponse) SetBody(body io.Reader) error {
return nil
}
func (r *TodoResponse) AcceptContentType() string { return "application/json" }
func (r *TodoResponse) SetStatusCode(code int) { r.HTTPStatusCode = code }
func (r *TodoResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

//---------------------------------------------

Expand All @@ -47,13 +52,18 @@ func (r *deletePostRequest) Path() (string, error) { return "/posts/" + fmt.Spri

func (r *deletePostRequest) Encode() (io.Reader, error) { return nil, nil }
func (r *deletePostRequest) ContentType() string { return "" }
func (r *DeletePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *DeletePostResponse) Decode(body io.Reader) error {

return nil
}
func (r *DeletePostResponse) SetBody(body io.Reader) error {
return nil
}
func (r *DeletePostResponse) SetBody(body io.Reader) {}
func (r *DeletePostResponse) AcceptContentType() string { return "" }
func (r *DeletePostResponse) SetStatusCode(code int) { r.HTTPStatusCode = code }
func (r *DeletePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

// ---------------------------------------------

Expand Down Expand Up @@ -82,13 +92,18 @@ func (r *updatePostRequest) Encode() (io.Reader, error) {
return bytes.NewReader(jsonBytes), nil
}
func (r *updatePostRequest) ContentType() string { return "application/json; charset=UTF-8" }
func (r *UpdatePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *UpdatePostResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}
func (r *UpdatePostResponse) SetBody(body io.Reader) {}
func (r *UpdatePostResponse) SetBody(body io.Reader) error {
return nil
}
func (r *UpdatePostResponse) AcceptContentType() string { return "application/json" }
func (r *UpdatePostResponse) SetStatusCode(code int) { r.HTTPStatusCode = code }
func (r *UpdatePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

// ---------------------------------------------

Expand Down Expand Up @@ -118,13 +133,18 @@ func (r *createPostRequest) Encode() (io.Reader, error) {
return bytes.NewReader(jsonBytes), nil
}
func (r *createPostRequest) ContentType() string { return "application/json" }
func (r *CreatePostResponse) Decode(body io.ReadCloser) error {
defer body.Close()
func (r *CreatePostResponse) Decode(body io.Reader) error {

return json.NewDecoder(body).Decode(r)
}
func (r *CreatePostResponse) SetBody(body io.Reader) {}
func (r *CreatePostResponse) SetBody(body io.Reader) error {
return nil
}
func (r *CreatePostResponse) AcceptContentType() string { return "application/json" }
func (r *CreatePostResponse) SetStatusCode(code int) { r.HTTPStatusCode = code }
func (r *CreatePostResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}

// ---------------------------------------------

Expand Down
12 changes: 6 additions & 6 deletions restclientgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ type Request interface {

type Response interface {
// Decode decodes the response body into the given interface if the
// response is a success and the content type is json.
Decode(body io.ReadCloser) error
// SetBody sets the response raw body if the response is a success but
// the content type is not json.
SetBody(body io.Reader)
// response matches the AcceptContentType.
Decode(body io.Reader) error
// SetBody sets the response raw body if the response can't be decoded.
SetBody(body io.Reader) error
// AcceptContentType returns the content type that the response should be decoded to.
AcceptContentType() string
// SetStatusCode sets the HTTP response status code.
SetStatusCode(code int)
SetStatusCode(code int) error
}

const (
Expand Down Expand Up @@ -140,6 +139,7 @@ func (r *RestClient) do(ctx context.Context, method httpMethod, request Request,
if err != nil {
return fmt.Errorf("%w: %s", ErrHTTPRequest, err)
}
defer httpResponse.Body.Close()

response.SetStatusCode(httpResponse.StatusCode)
if httpResponse.StatusCode >= 400 {
Expand Down

0 comments on commit a31cdcb

Please sign in to comment.