Skip to content

Commit

Permalink
fix(duffel): reuse body when content encoding != gzip (#11)
Browse files Browse the repository at this point in the history
* feat(duffel): add GatewayTimeout and BadGateway errors

* fix(duffel): fix nil response when status code >= 400

* fix(duffel): reuse http body if content encoding != gzip
  • Loading branch information
0x9ef authored Sep 15, 2023
1 parent 99c3370 commit 6a4b821
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
28 changes: 15 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func (c *client[R, T]) makeRequest(ctx context.Context, resourceName string, met

func (c *client[R, T]) buildRequestURL(resourceName string) (*url.URL, error) {
u, err := url.Parse(c.options.Host)

if err != nil {
return nil, err
}
Expand All @@ -174,16 +173,20 @@ func (c *client[R, T]) buildRequestURL(resourceName string) (*url.URL, error) {
return u, nil
}

func gzipResponseReader(response *http.Response) (io.ReadCloser, error) {
var reader io.ReadCloser
func gzipResponseReader(resp *http.Response) (io.ReadCloser, error) {
var err error
if response.Header.Get("Content-Encoding") == "gzip" {
reader, err = gzip.NewReader(response.Body)
var reader io.ReadCloser
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, err = gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
} else {
reader = response.Body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
reader, resp.Body = io.NopCloser(bytes.NewBuffer(body)), io.NopCloser(bytes.NewBuffer(body))
}

return reader, nil
Expand All @@ -195,11 +198,6 @@ var notRetryableErrors = map[ErrorCode]bool{
}

func decodeError(response *http.Response) error {
reader, err := gzipResponseReader(response)
if err != nil {
return err
}

contentType := response.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "text/html") {
// Handle occasional HTML error pages at routing layer
Expand All @@ -217,7 +215,11 @@ func decodeError(response *http.Response) error {
}
}

// Try to decode error and set internal StatusCode
reader, err := gzipResponseReader(response)
if err != nil {
return err
}

derr := &DuffelError{StatusCode: response.StatusCode}
err = json.NewDecoder(reader).Decode(derr)
if err != nil {
Expand All @@ -228,5 +230,5 @@ func decodeError(response *http.Response) error {
derr.Retryable = response.StatusCode != 500 && response.StatusCode != 502 &&
!notRetryableErrors[derr.Errors[0].Code]

return err
return derr
}
3 changes: 2 additions & 1 deletion requestbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ func decodeResponse[T any](resp *http.Response, v T) error {
}
defer reader.Close()

return json.NewDecoder(reader).Decode(v)
err = json.NewDecoder(reader).Decode(v)
return err
}

// normalizeParams returns a slice of interfaces from the given params.
Expand Down

0 comments on commit 6a4b821

Please sign in to comment.