Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from suzuki-shunsuke/feat/change-pointer-to-st…
Browse files Browse the repository at this point in the history
…ruct

feat: change pointer to struct
  • Loading branch information
suzuki-shunsuke authored Jul 11, 2020
2 parents 4bf383f + 4cfc047 commit 81b4684
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/suzuki-shunsuke/flute/v2 v2.0.0-0 h1:uxzfDeVxNp7GA4GSAlTBV7rA5TbHXV4Q68cNebOEAZk=
github.com/suzuki-shunsuke/flute/v2 v2.0.0-0/go.mod h1:wsUAUZbz+DSjv+XDv/gZeEX/mMu1y9kzIXCN5Slu6NY=
github.com/suzuki-shunsuke/flute/v2 v2.0.0 h1:cDpi+h9wceJuOEjOxTvZNQXsIS+lv2Ism4ZdfYgMOnU=
github.com/suzuki-shunsuke/flute/v2 v2.0.0/go.mod h1:wsUAUZbz+DSjv+XDv/gZeEX/mMu1y9kzIXCN5Slu6NY=
github.com/suzuki-shunsuke/go-cliutil v0.0.0-20181211154308-176f852d9bca/go.mod h1:Vq3NkhgmA9DT/2UZ08x/3A34xxvzQ/vTMABnTWKoMbY=
github.com/suzuki-shunsuke/go-jsoneq v0.1.2 h1:A4czEbmFqSELTbrEtXVo4dSgfz2e2Z0y6G3OpExUML8=
Expand Down
19 changes: 8 additions & 11 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Client struct {
Timeout time.Duration
}

func New(endpoint string) *Client {
return &Client{
func New(endpoint string) Client {
return Client{
Endpoint: endpoint,
HTTPClient: http.DefaultClient,
}
Expand All @@ -36,30 +36,27 @@ type Error struct {
err error
}

func (e *Error) StatusCode() int {
func (e Error) StatusCode() int {
return e.statusCode
}

func (e *Error) BodyByte() []byte {
func (e Error) BodyByte() []byte {
return e.bodyByte
}

func (e *Error) Body() interface{} {
func (e Error) Body() interface{} {
return e.body
}

func (e *Error) Error() string {
if e == nil {
return ""
}
func (e Error) Error() string {
a := ""
if e.err != nil {
a = e.err.Error()
}
return "status code: " + strconv.Itoa(e.statusCode) + ", " + string(e.bodyByte) + ": " + a
}

func (e *Error) Unwrap() error {
func (e Error) Unwrap() error {
return e.err
}

Expand All @@ -74,7 +71,7 @@ type CallParams struct {
Timeout time.Duration
}

func (client *Client) Call(ctx context.Context, params *CallParams) (*http.Response, error) {
func (client Client) Call(ctx context.Context, params CallParams) (*http.Response, error) {
if params.Timeout > 0 {
c, cancel := context.WithTimeout(ctx, params.Timeout)
defer cancel()
Expand Down
20 changes: 10 additions & 10 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func TestError_Error(t *testing.T) {
data := []struct {
title string
exp string
err *Error
err Error
}{
{
title: "normal",
err: &Error{
err: Error{
statusCode: 500,
bodyByte: []byte(`{"error": "Internal Server Error"}`),
err: errors.New("status code >= 300"),
Expand Down Expand Up @@ -137,15 +137,15 @@ func TestClient_Call(t *testing.T) {
ctx := context.Background()
data := []struct {
title string
params *CallParams
params CallParams
routes []flute.Route
exp interface{}
expErrorResponse interface{}
isErr bool
}{
{
title: "request body is struct",
params: &CallParams{
params: CallParams{
Method: "POST",
Path: "/users",
RequestBody: struct {
Expand All @@ -166,7 +166,7 @@ func TestClient_Call(t *testing.T) {
},
{
title: "request body is string",
params: &CallParams{
params: CallParams{
Method: "POST",
Path: "/users",
RequestBody: `{"name": "foo", "email": "foo@example.com"}`,
Expand All @@ -181,7 +181,7 @@ func TestClient_Call(t *testing.T) {
},
{
title: "request body is []byte",
params: &CallParams{
params: CallParams{
Method: "POST",
Path: "/users",
RequestBody: []byte(`{"name": "foo", "email": "foo@example.com"}`),
Expand All @@ -196,7 +196,7 @@ func TestClient_Call(t *testing.T) {
},
{
title: "error response",
params: &CallParams{
params: CallParams{
Method: "GET",
Path: "/groups/foo",
ResponseErrorBody: &map[string]interface{}{},
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestClient_Call(t *testing.T) {
},
{
title: "error response with query and header",
params: &CallParams{
params: CallParams{
Method: "GET",
Path: "/groups",
Header: http.Header{
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestClient_Call(t *testing.T) {
},
{
title: "client timeout",
params: &CallParams{
params: CallParams{
Method: "POST",
Path: "/users",
RequestBody: struct {
Expand All @@ -284,7 +284,7 @@ func TestClient_Call(t *testing.T) {
},
{
title: "params imeout",
params: &CallParams{
params: CallParams{
Method: "POST",
Path: "/users",
RequestBody: struct {
Expand Down

0 comments on commit 81b4684

Please sign in to comment.