Skip to content

Commit

Permalink
Better handling of "save the JSON"
Browse files Browse the repository at this point in the history
As suggested by 'mattn, use a `TeeReader` with an `io.Writer` to keep
the semantics of `json.NewDecoder().Decode()` whilst also being able
to save the JSON if requested by the client.

However we need to use our own interface (`WriteResetter`) in order to
be able to call `Reset()` on the underlying buffer (which does somewhat
limit the usage to buffers instead of generic `io.Writer` and may not
be the 100% ideal solution.)
  • Loading branch information
rjp authored and mattn committed Oct 24, 2024
1 parent 79bee3e commit 019b11d
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions mastodon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ 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
SaveJSON bool
LastJSON []byte
Config *Config
UserAgent string
JSONWriter WriteResetter
}

func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, pg *Pagination) error {
Expand Down Expand Up @@ -128,20 +132,10 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
}
}

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)

if err != nil || c.LastJSON == nil {
return err
}

// ...which means we can't use `NewDecoder.Decode` any more.
return json.Unmarshal(c.LastJSON, &res)
if c.JSONWriter != nil {
c.JSONWriter.Reset()
return json.NewDecoder(io.TeeReader(resp.Body, c.JSONWriter)).Decode(&res)
} else {
// We don't want the JSON, just do the previous streaming decode.
return json.NewDecoder(resp.Body).Decode(&res)
}
}
Expand Down

0 comments on commit 019b11d

Please sign in to comment.