Revert to simple io.Writer
with client-resetting
After going through several other solutions, there's no clean way to use an `io.Writer` but also be able to call `Reset()` between writes. Filehandles can't `Reset()` and buffers can't `Seek()`. But then if you supplied `os.Stderr`, you'd be expecting appending and a `Seek()` would be nonsense. My own preference would be to make `JSONWriter` a strict `*bytes.Buffer` which lets the library handle the resetting and the client do any outputting if required - I can't see much value in ever supplying a non-`bytes.Buffer` as `JSONWriter`.
This commit is contained in:
parent
51e4324c7a
commit
08be497fae
2 changed files with 5 additions and 9 deletions
|
@ -24,17 +24,12 @@ type Config struct {
|
|||
AccessToken string
|
||||
}
|
||||
|
||||
type WriteResetter interface {
|
||||
io.Writer
|
||||
Reset()
|
||||
}
|
||||
|
||||
// Client is a API client for mastodon.
|
||||
type Client struct {
|
||||
http.Client
|
||||
Config *Config
|
||||
UserAgent string
|
||||
JSONWriter WriteResetter
|
||||
JSONWriter io.Writer
|
||||
}
|
||||
|
||||
func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, pg *Pagination) error {
|
||||
|
@ -133,7 +128,6 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
|||
}
|
||||
|
||||
if c.JSONWriter != nil {
|
||||
c.JSONWriter.Reset()
|
||||
return json.NewDecoder(io.TeeReader(resp.Body, c.JSONWriter)).Decode(&res)
|
||||
} else {
|
||||
return json.NewDecoder(resp.Body).Decode(&res)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue