avoid parsing JSON if it is known empty response

This commit is contained in:
Andre Medeiros 2021-07-15 13:27:48 -04:00
parent 1dc4c72da2
commit 9e77a662a3
3 changed files with 29 additions and 13 deletions

View file

@ -97,7 +97,7 @@ func (rc *Client) NewAuthenticatedClient(refreshToken, accessToken string) *Auth
return &AuthenticatedClient{rc, refreshToken, accessToken, nil} 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() req, err := r.HTTPRequest()
if err != nil { if err != nil {
return nil, err return nil, err
@ -135,6 +135,11 @@ func (rac *AuthenticatedClient) request(r *Request, rh ResponseHandler) (interfa
} }
return nil, NewError(val) return nil, NewError(val)
} }
if r.emptyResponseBytes > 0 && len(bb) == r.emptyResponseBytes {
return empty, nil
}
val, err := parser.ParseBytes(bb) val, err := parser.ParseBytes(bb)
if err != nil { if err != nil {
return nil, err return nil, err
@ -153,7 +158,7 @@ func (rac *AuthenticatedClient) RefreshTokens() (*RefreshTokenResponse, error) {
WithBasicAuth(rac.id, rac.secret), WithBasicAuth(rac.id, rac.secret),
) )
rtr, err := rac.request(req, NewRefreshTokenResponse) rtr, err := rac.request(req, NewRefreshTokenResponse, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -166,10 +171,11 @@ func (rac *AuthenticatedClient) MessageInbox(opts ...RequestOption) (*ListingRes
WithMethod("GET"), WithMethod("GET"),
WithToken(rac.accessToken), WithToken(rac.accessToken),
WithURL("https://oauth.reddit.com/message/inbox.json"), WithURL("https://oauth.reddit.com/message/inbox.json"),
WithEmptyResponseBytes(122),
}, opts...) }, opts...)
req := NewRequest(opts...) req := NewRequest(opts...)
lr, err := rac.request(req, NewListingResponse) lr, err := rac.request(req, NewListingResponse, EmptyListingResponse)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -182,11 +188,12 @@ func (rac *AuthenticatedClient) MessageUnread(opts ...RequestOption) (*ListingRe
WithMethod("GET"), WithMethod("GET"),
WithToken(rac.accessToken), WithToken(rac.accessToken),
WithURL("https://oauth.reddit.com/message/unread.json"), WithURL("https://oauth.reddit.com/message/unread.json"),
WithEmptyResponseBytes(122),
}, opts...) }, opts...)
req := NewRequest(opts...) req := NewRequest(opts...)
lr, err := rac.request(req, NewListingResponse) lr, err := rac.request(req, NewListingResponse, EmptyListingResponse)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -201,7 +208,7 @@ func (rac *AuthenticatedClient) Me() (*MeResponse, error) {
WithURL("https://oauth.reddit.com/api/v1/me"), WithURL("https://oauth.reddit.com/api/v1/me"),
) )
mr, err := rac.request(req, NewMeResponse) mr, err := rac.request(req, NewMeResponse, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -18,12 +18,13 @@ type Request struct {
url string url string
auth string auth string
tags []string tags []string
emptyResponseBytes int
} }
type RequestOption func(*Request) type RequestOption func(*Request)
func NewRequest(opts ...RequestOption) *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 { for _, opt := range opts {
opt(req) opt(req)
} }
@ -90,3 +91,9 @@ func WithQuery(key, val string) RequestOption {
req.query.Set(key, val) req.query.Set(key, val)
} }
} }
func WithEmptyResponseBytes(bytes int) RequestOption {
return func(req *Request) {
req.emptyResponseBytes = bytes
}
}

View file

@ -129,3 +129,5 @@ func NewListingResponse(val *fastjson.Value) interface{} {
return lr return lr
} }
var EmptyListingResponse = &ListingResponse{}