From 9621a49db8411a63e46b3a81085af02b7c97fbd1 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Tue, 27 Sep 2022 01:40:03 +0300 Subject: [PATCH] Request compressed api --- mastodon/mastodon.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mastodon/mastodon.go b/mastodon/mastodon.go index 8678314..c04080a 100644 --- a/mastodon/mastodon.go +++ b/mastodon/mastodon.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "encoding/json" + "compress/gzip" "errors" "fmt" "io" @@ -144,6 +145,7 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in } req = req.WithContext(ctx) req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) + req.Header.Set("accept-encoding", "gzip") if params != nil { req.Header.Set("Content-Type", ct) } @@ -167,7 +169,15 @@ 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 return new mastodon API client.