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
parent
78d8d7fc43
commit
63cf193a13
|
@ -29,6 +29,7 @@ type Client struct {
|
||||||
http.Client
|
http.Client
|
||||||
Config *Config
|
Config *Config
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
SaveJSON bool
|
||||||
LastJSON []byte
|
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.
|
// read all of it. But we restrict ourselves to a max of 100M.
|
||||||
safer := &io.LimitedReader{resp.Body, 100 * 1_048_576}
|
safer := &io.LimitedReader{resp.Body, 100 * 1_048_576}
|
||||||
c.LastJSON, err = io.ReadAll(safer)
|
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.
|
// ...which means we can't use `NewDecoder.Decode` any more.
|
||||||
return json.Unmarshal(c.LastJSON, &res)
|
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.
|
// NewClient returns a new mastodon API client.
|
||||||
|
|
Loading…
Reference in New Issue