Request compressed api

This commit is contained in:
localhost_frssoft 2022-09-27 01:40:03 +03:00
parent 8995edec91
commit 9621a49db8
1 changed files with 11 additions and 1 deletions

View File

@ -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.