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
24
mastodon.go
24
mastodon.go
|
@ -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,17 +128,22 @@ 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 {
|
||||||
// read all of it. But we restrict ourselves to a max of 100M.
|
// We want to store the JSON received -> we absolutely have to
|
||||||
safer := &io.LimitedReader{resp.Body, 100 * 1_048_576}
|
// read all of it. But we restrict ourselves to a max of 100M.
|
||||||
c.LastJSON, err = io.ReadAll(safer)
|
safer := &io.LimitedReader{resp.Body, 100 * 1_048_576}
|
||||||
|
c.LastJSON, err = io.ReadAll(safer)
|
||||||
|
|
||||||
if err != nil || c.LastJSON == nil {
|
if err != nil || c.LastJSON == nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...which means we can't use `NewDecoder.Decode` any more.
|
|
||||||
return json.Unmarshal(c.LastJSON, &res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new mastodon API client.
|
// NewClient returns a new mastodon API client.
|
||||||
|
|
Loading…
Reference in New Issue