refactor: return full HTTP response for binary response bodies (#112)

This commit is contained in:
Daniel Moran
2021-06-10 11:20:33 -04:00
committed by GitHub
parent 1c2f61f5ca
commit a387cabcfb
23 changed files with 518 additions and 512 deletions

34
api/gunzip.go Normal file
View File

@ -0,0 +1,34 @@
package api
import (
"compress/gzip"
"io"
"net/http"
)
func GunzipIfNeeded(resp *http.Response) (io.ReadCloser, error) {
if resp.Header.Get("Content-Encoding") == "gzip" {
gzr, err := gzip.NewReader(resp.Body)
if err != nil {
return resp.Body, err
}
return &gunzipReadCloser{underlying: resp.Body, gunzip: gzr}, nil
}
return resp.Body, nil
}
type gunzipReadCloser struct {
underlying io.ReadCloser
gunzip io.ReadCloser
}
func (gzrc *gunzipReadCloser) Read(p []byte) (int, error) {
return gzrc.gunzip.Read(p)
}
func (gzrc *gunzipReadCloser) Close() error {
if err := gzrc.gunzip.Close(); err != nil {
return err
}
return gzrc.underlying.Close()
}