From 6c76b245946273c77475695e6932a52d964c445a Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Sun, 1 Jan 2023 21:46:06 +0800 Subject: [PATCH] Implement gzipped responses --- mastodon.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mastodon.go b/mastodon.go index c704e10..e7791b4 100644 --- a/mastodon.go +++ b/mastodon.go @@ -3,6 +3,7 @@ package mastodon import ( "context" + "compress/gzip" "encoding/json" "errors" "fmt" @@ -76,6 +77,7 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in } } req = req.WithContext(ctx) + req.Header.Set("Accept-Encoding", "gzip") req.Header.Set("Authorization", "Bearer "+c.Config.AccessToken) if params != nil { req.Header.Set("Content-Type", ct) @@ -125,7 +127,16 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in *pg = *pg2 } } - return json.NewDecoder(resp.Body).Decode(&res) + + var reader io.ReadCloser + switch resp.Header.Get("Content-Encoding") { + case "gzip": + reader, err = gzip.NewReader(resp.Body) + defer reader.Close() + default: + reader = resp.Body + } + return json.NewDecoder(reader).Decode(&res) } // NewClient returns a new mastodon API client.