Gate the JSON saving behind a client flag

This way the library will behave as it used to for anyone who doesn't
explicitly set `client.SaveJSON` to be `true`.  No surprises = good.
pull/183/head
rjp 2023-05-23 08:33:15 +01:00
parent 78d8d7fc43
commit 63cf193a13
1 changed files with 15 additions and 9 deletions

View File

@ -29,6 +29,7 @@ type Client struct {
http.Client
Config *Config
UserAgent string
SaveJSON bool
LastJSON []byte
}
@ -127,7 +128,8 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
}
}
// If we want to store the JSON received, we absolutely have to
if c.SaveJSON {
// We want to store the JSON received -> we absolutely have to
// read all of it. But we restrict ourselves to a max of 100M.
safer := &io.LimitedReader{resp.Body, 100 * 1_048_576}
c.LastJSON, err = io.ReadAll(safer)
@ -138,6 +140,10 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
// ...which means we can't use `NewDecoder.Decode` any more.
return json.Unmarshal(c.LastJSON, &res)
} else {
// We don't want the JSON, just do the previous streaming decode.
return json.NewDecoder(resp.Body).Decode(&res)
}
}
// NewClient returns a new mastodon API client.