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

fix: decompress resonse when appropriate, fixes #46 #47

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package koios

import (
"compress/flate"
"compress/gzip"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -81,7 +83,20 @@

defer func() { _ = rsp.Body.Close() }()

return io.ReadAll(rsp.Body)
rb := rsp.Body

if strings.Contains(rsp.Header.Get("Content-Encoding"), "gzip") {
if rb, err = gzip.NewReader(rsp.Body); err == nil {
defer rb.Close()
} else {
return nil, err
}
} else if rsp.Header.Get("Content-Encoding") == "deflate" {
rb = flate.NewReader(rsp.Body)
defer rb.Close()
}

Check warning on line 97 in response.go

View check run for this annotation

Codecov / codecov/patch

response.go#L86-L97

Added lines #L86 - L97 were not covered by tests

return io.ReadAll(rb)

Check warning on line 99 in response.go

View check run for this annotation

Codecov / codecov/patch

response.go#L99

Added line #L99 was not covered by tests
}

// ReadAndUnmarshalResponse is helper to unmarchal json responses.
Expand Down
Loading