Skip to content

Commit

Permalink
chore: force decode on error (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis authored Nov 20, 2023
1 parent 89e55e9 commit 5f016dc
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions restclientgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

type RestClient struct {
httpClient *http.Client
endpoint string
requestModifier func(*http.Request) *http.Request
httpClient *http.Client
endpoint string
requestModifier func(*http.Request) *http.Request
forceDecodeOnError bool
}

type Error string
Expand Down Expand Up @@ -80,6 +81,24 @@ func (r *RestClient) SetRequestModifier(requestModifier func(*http.Request) *htt
r.requestModifier = requestModifier
}

// WithRequestModifier adds a function that will modify each request
func (r *RestClient) WithRequestModifier(requestModifier func(*http.Request) *http.Request) *RestClient {
r.requestModifier = requestModifier
return r
}

// WithHTTPClient overrides the default http client.
func (r *RestClient) WithHTTPClient(client *http.Client) *RestClient {
r.httpClient = client
return r
}

// WithDecodeOnError forces the response to be decoded even if the status code is >= 400.
func (r *RestClient) WithDecodeOnError(decodeOnError bool) *RestClient {
r.forceDecodeOnError = decodeOnError
return r
}

func (r *RestClient) SetEndpoint(endpoint string) {
r.endpoint = endpoint
}
Expand Down Expand Up @@ -161,7 +180,7 @@ func (r *RestClient) do(ctx context.Context, method httpMethod, request Request,
return err
}

if httpResponse.StatusCode >= 400 {
if httpResponse.StatusCode >= 400 && !r.forceDecodeOnError {
err = response.SetBody(httpResponse.Body)
if err != nil {
return err
Expand Down

0 comments on commit 5f016dc

Please sign in to comment.