2021-05-10 00:51:15 +00:00
|
|
|
package reddit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-07-12 18:36:08 +00:00
|
|
|
"fmt"
|
2021-05-10 00:51:15 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-07-08 02:19:02 +00:00
|
|
|
"net/http/httptrace"
|
2021-07-12 19:51:02 +00:00
|
|
|
"regexp"
|
2021-05-10 00:51:15 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-07-08 01:01:54 +00:00
|
|
|
|
2021-07-08 02:19:02 +00:00
|
|
|
"github.com/DataDog/datadog-go/statsd"
|
2021-07-08 01:01:54 +00:00
|
|
|
"github.com/valyala/fastjson"
|
2021-05-10 00:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tokenURL = "https://www.reddit.com/api/v1/access_token"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
id string
|
|
|
|
secret string
|
2021-07-07 20:24:23 +00:00
|
|
|
client *http.Client
|
2021-07-08 02:19:02 +00:00
|
|
|
tracer *httptrace.ClientTrace
|
2021-07-08 01:01:54 +00:00
|
|
|
parser *fastjson.Parser
|
2021-07-08 02:19:02 +00:00
|
|
|
statsd *statsd.Client
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 18:51:42 +00:00
|
|
|
func SplitID(id string) (string, string) {
|
|
|
|
if parts := strings.Split(id, "_"); len(parts) == 2 {
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
2021-07-12 19:51:02 +00:00
|
|
|
func PostIDFromContext(context string) string {
|
|
|
|
exps := []*regexp.Regexp{
|
|
|
|
regexp.MustCompile(`\/r\/[^\/]*\/comments\/([^\/]*)\/.*`),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, exp := range exps {
|
|
|
|
matches := exp.FindStringSubmatch(context)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-07-14 00:09:44 +00:00
|
|
|
func NewClient(id, secret string, statsd *statsd.Client, connLimit int) *Client {
|
2021-07-08 02:19:02 +00:00
|
|
|
tracer := &httptrace.ClientTrace{
|
|
|
|
GotConn: func(info httptrace.GotConnInfo) {
|
2021-07-08 02:44:46 +00:00
|
|
|
if info.Reused {
|
2021-07-08 02:54:26 +00:00
|
|
|
statsd.Incr("reddit.api.connections.reused", []string{}, 0.1)
|
2021-07-08 02:44:46 +00:00
|
|
|
if info.WasIdle {
|
|
|
|
idleTime := float64(int64(info.IdleTime) / int64(time.Millisecond))
|
|
|
|
statsd.Histogram("reddit.api.connections.idle_time", idleTime, []string{}, 0.1)
|
|
|
|
}
|
2021-07-08 02:19:02 +00:00
|
|
|
} else {
|
2021-07-08 02:54:26 +00:00
|
|
|
statsd.Incr("reddit.api.connections.created", []string{}, 0.1)
|
2021-07-08 02:19:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:33:25 +00:00
|
|
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
2021-07-14 00:09:44 +00:00
|
|
|
t.MaxIdleConns = connLimit / 4
|
|
|
|
t.MaxConnsPerHost = connLimit
|
|
|
|
t.MaxIdleConnsPerHost = connLimit / 4
|
2021-07-14 00:34:04 +00:00
|
|
|
t.IdleConnTimeout = 60 * time.Second
|
2021-07-14 00:16:38 +00:00
|
|
|
t.ResponseHeaderTimeout = 5 * time.Second
|
2021-07-13 23:33:25 +00:00
|
|
|
|
|
|
|
client := &http.Client{Transport: t}
|
2021-07-07 20:24:23 +00:00
|
|
|
|
2021-07-08 01:01:54 +00:00
|
|
|
parser := &fastjson.Parser{}
|
|
|
|
|
2021-07-07 20:24:23 +00:00
|
|
|
return &Client{
|
|
|
|
id,
|
|
|
|
secret,
|
|
|
|
client,
|
2021-07-08 02:19:02 +00:00
|
|
|
tracer,
|
2021-07-08 01:01:54 +00:00
|
|
|
parser,
|
2021-07-08 02:19:02 +00:00
|
|
|
statsd,
|
2021-07-07 20:24:23 +00:00
|
|
|
}
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthenticatedClient struct {
|
|
|
|
*Client
|
|
|
|
|
|
|
|
refreshToken string
|
|
|
|
accessToken string
|
|
|
|
expiry *time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *Client) NewAuthenticatedClient(refreshToken, accessToken string) *AuthenticatedClient {
|
|
|
|
return &AuthenticatedClient{rc, refreshToken, accessToken, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rac *AuthenticatedClient) request(r *Request) ([]byte, error) {
|
|
|
|
req, err := r.HTTPRequest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-08 02:19:02 +00:00
|
|
|
req = req.WithContext(httptrace.WithClientTrace(req.Context(), rac.tracer))
|
|
|
|
|
2021-07-08 23:26:15 +00:00
|
|
|
start := time.Now()
|
2021-07-07 20:24:23 +00:00
|
|
|
resp, err := rac.client.Do(req)
|
2021-07-08 23:26:15 +00:00
|
|
|
rac.statsd.Incr("reddit.api.calls", r.tags, 0.1)
|
|
|
|
rac.statsd.Histogram("reddit.api.latency", float64(time.Now().Sub(start).Milliseconds()), r.tags, 0.1)
|
|
|
|
|
2021-05-10 00:51:15 +00:00
|
|
|
if err != nil {
|
2021-07-08 23:26:15 +00:00
|
|
|
rac.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
2021-05-10 00:51:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-07 20:24:23 +00:00
|
|
|
defer resp.Body.Close()
|
2021-05-10 00:51:15 +00:00
|
|
|
|
2021-07-12 18:36:08 +00:00
|
|
|
bb, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
rac.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
rac.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
|
|
|
// Try to parse a json error. Otherwise we generate a generic one
|
|
|
|
rerr := &Error{}
|
|
|
|
if jerr := json.Unmarshal(bb, rerr); jerr != nil {
|
|
|
|
return nil, fmt.Errorf("error from reddit: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
return bb, nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 02:19:43 +00:00
|
|
|
func (rac *AuthenticatedClient) RefreshTokens() (*RefreshTokenResponse, error) {
|
2021-05-10 00:51:15 +00:00
|
|
|
req := NewRequest(
|
2021-07-08 23:26:15 +00:00
|
|
|
WithTags([]string{"url:/api/v1/access_token"}),
|
2021-05-10 00:51:15 +00:00
|
|
|
WithMethod("POST"),
|
|
|
|
WithURL(tokenURL),
|
|
|
|
WithBody("grant_type", "refresh_token"),
|
|
|
|
WithBody("refresh_token", rac.refreshToken),
|
|
|
|
WithBasicAuth(rac.id, rac.secret),
|
|
|
|
)
|
|
|
|
|
|
|
|
body, err := rac.request(req)
|
2021-06-24 02:19:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rtr := &RefreshTokenResponse{}
|
|
|
|
json.Unmarshal([]byte(body), rtr)
|
|
|
|
return rtr, nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 02:19:43 +00:00
|
|
|
func (rac *AuthenticatedClient) MessageInbox(from string) (*MessageListingResponse, error) {
|
2021-05-10 00:51:15 +00:00
|
|
|
req := NewRequest(
|
2021-07-08 23:55:14 +00:00
|
|
|
WithTags([]string{"url:/api/v1/message/inbox"}),
|
2021-05-10 00:51:15 +00:00
|
|
|
WithMethod("GET"),
|
|
|
|
WithToken(rac.accessToken),
|
|
|
|
WithURL("https://oauth.reddit.com/message/inbox.json"),
|
2021-06-24 02:19:43 +00:00
|
|
|
WithQuery("before", from),
|
2021-05-10 00:51:15 +00:00
|
|
|
)
|
|
|
|
|
2021-06-24 02:19:43 +00:00
|
|
|
body, err := rac.request(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mlr := &MessageListingResponse{}
|
|
|
|
json.Unmarshal([]byte(body), mlr)
|
|
|
|
return mlr, nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 23:33:25 +00:00
|
|
|
func (rac *AuthenticatedClient) MessageUnread(from string) (*MessageListingResponse, error) {
|
|
|
|
req := NewRequest(
|
|
|
|
WithTags([]string{"url:/api/v1/message/unread"}),
|
|
|
|
WithMethod("GET"),
|
|
|
|
WithToken(rac.accessToken),
|
|
|
|
WithURL("https://oauth.reddit.com/message/unread.json"),
|
|
|
|
WithQuery("before", from),
|
|
|
|
)
|
|
|
|
|
|
|
|
body, err := rac.request(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mlr := &MessageListingResponse{}
|
|
|
|
json.Unmarshal([]byte(body), mlr)
|
|
|
|
return mlr, nil
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:51:15 +00:00
|
|
|
func (rac *AuthenticatedClient) Me() (*MeResponse, error) {
|
|
|
|
req := NewRequest(
|
2021-07-08 23:26:15 +00:00
|
|
|
WithTags([]string{"url:/api/v1/me"}),
|
2021-05-10 00:51:15 +00:00
|
|
|
WithMethod("GET"),
|
|
|
|
WithToken(rac.accessToken),
|
|
|
|
WithURL("https://oauth.reddit.com/api/v1/me"),
|
|
|
|
)
|
|
|
|
|
|
|
|
body, err := rac.request(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mr := &MeResponse{}
|
|
|
|
err = json.Unmarshal(body, mr)
|
|
|
|
|
|
|
|
return mr, err
|
|
|
|
}
|