From 9e77a662a3d859a8f2bba47609a73e90d55e3e08 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Thu, 15 Jul 2021 13:27:48 -0400 Subject: [PATCH] avoid parsing JSON if it is known empty response --- internal/reddit/client.go | 17 ++++++++++++----- internal/reddit/request.go | 23 +++++++++++++++-------- internal/reddit/types.go | 2 ++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/internal/reddit/client.go b/internal/reddit/client.go index a7091b7..62211b4 100644 --- a/internal/reddit/client.go +++ b/internal/reddit/client.go @@ -97,7 +97,7 @@ func (rc *Client) NewAuthenticatedClient(refreshToken, accessToken string) *Auth return &AuthenticatedClient{rc, refreshToken, accessToken, nil} } -func (rac *AuthenticatedClient) request(r *Request, rh ResponseHandler) (interface{}, error) { +func (rac *AuthenticatedClient) request(r *Request, rh ResponseHandler, empty interface{}) (interface{}, error) { req, err := r.HTTPRequest() if err != nil { return nil, err @@ -135,6 +135,11 @@ func (rac *AuthenticatedClient) request(r *Request, rh ResponseHandler) (interfa } return nil, NewError(val) } + + if r.emptyResponseBytes > 0 && len(bb) == r.emptyResponseBytes { + return empty, nil + } + val, err := parser.ParseBytes(bb) if err != nil { return nil, err @@ -153,7 +158,7 @@ func (rac *AuthenticatedClient) RefreshTokens() (*RefreshTokenResponse, error) { WithBasicAuth(rac.id, rac.secret), ) - rtr, err := rac.request(req, NewRefreshTokenResponse) + rtr, err := rac.request(req, NewRefreshTokenResponse, nil) if err != nil { return nil, err } @@ -166,10 +171,11 @@ func (rac *AuthenticatedClient) MessageInbox(opts ...RequestOption) (*ListingRes WithMethod("GET"), WithToken(rac.accessToken), WithURL("https://oauth.reddit.com/message/inbox.json"), + WithEmptyResponseBytes(122), }, opts...) req := NewRequest(opts...) - lr, err := rac.request(req, NewListingResponse) + lr, err := rac.request(req, NewListingResponse, EmptyListingResponse) if err != nil { return nil, err } @@ -182,11 +188,12 @@ func (rac *AuthenticatedClient) MessageUnread(opts ...RequestOption) (*ListingRe WithMethod("GET"), WithToken(rac.accessToken), WithURL("https://oauth.reddit.com/message/unread.json"), + WithEmptyResponseBytes(122), }, opts...) req := NewRequest(opts...) - lr, err := rac.request(req, NewListingResponse) + lr, err := rac.request(req, NewListingResponse, EmptyListingResponse) if err != nil { return nil, err } @@ -201,7 +208,7 @@ func (rac *AuthenticatedClient) Me() (*MeResponse, error) { WithURL("https://oauth.reddit.com/api/v1/me"), ) - mr, err := rac.request(req, NewMeResponse) + mr, err := rac.request(req, NewMeResponse, nil) if err != nil { return nil, err } diff --git a/internal/reddit/request.go b/internal/reddit/request.go index 089a792..47aff4a 100644 --- a/internal/reddit/request.go +++ b/internal/reddit/request.go @@ -11,19 +11,20 @@ import ( const userAgent = "server:apollo-backend:v1.0 (by /u/iamthatis)" type Request struct { - body url.Values - query url.Values - method string - token string - url string - auth string - tags []string + body url.Values + query url.Values + method string + token string + url string + auth string + tags []string + emptyResponseBytes int } type RequestOption func(*Request) func NewRequest(opts ...RequestOption) *Request { - req := &Request{url.Values{}, url.Values{}, "GET", "", "", "", nil} + req := &Request{url.Values{}, url.Values{}, "GET", "", "", "", nil, 0} for _, opt := range opts { opt(req) } @@ -90,3 +91,9 @@ func WithQuery(key, val string) RequestOption { req.query.Set(key, val) } } + +func WithEmptyResponseBytes(bytes int) RequestOption { + return func(req *Request) { + req.emptyResponseBytes = bytes + } +} diff --git a/internal/reddit/types.go b/internal/reddit/types.go index 0cd818b..123ca21 100644 --- a/internal/reddit/types.go +++ b/internal/reddit/types.go @@ -129,3 +129,5 @@ func NewListingResponse(val *fastjson.Value) interface{} { return lr } + +var EmptyListingResponse = &ListingResponse{}