2021-05-10 00:51:15 +00:00
|
|
|
package reddit
|
|
|
|
|
|
|
|
import (
|
2022-03-12 17:50:05 +00:00
|
|
|
"context"
|
2021-09-25 16:56:01 +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"
|
2022-03-12 17:50:05 +00:00
|
|
|
"strconv"
|
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"
|
2022-03-12 17:50:05 +00:00
|
|
|
"github.com/go-redis/redis/v8"
|
2021-07-08 01:01:54 +00:00
|
|
|
"github.com/valyala/fastjson"
|
2021-05-10 00:51:15 +00:00
|
|
|
)
|
|
|
|
|
2022-03-12 17:50:05 +00:00
|
|
|
const (
|
|
|
|
SkipRateLimiting = "<SKIP_RATE_LIMITING>"
|
|
|
|
RequestRemainingBuffer = 50
|
2022-03-12 18:15:59 +00:00
|
|
|
|
2022-03-12 19:22:00 +00:00
|
|
|
RateLimitRemainingHeader = "x-ratelimit-remaining"
|
|
|
|
RateLimitUsedHeader = "x-ratelimit-used"
|
|
|
|
RateLimitResetHeader = "x-ratelimit-reset"
|
2022-03-12 17:50:05 +00:00
|
|
|
)
|
|
|
|
|
2021-05-10 00:51:15 +00:00
|
|
|
type Client struct {
|
2022-03-26 17:40:51 +00:00
|
|
|
id string
|
|
|
|
secret string
|
|
|
|
client *http.Client
|
|
|
|
tracer *httptrace.ClientTrace
|
|
|
|
pool *fastjson.ParserPool
|
|
|
|
statsd statsd.ClientInterface
|
|
|
|
redis *redis.Client
|
|
|
|
defaultOpts []RequestOption
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 18:25:34 +00:00
|
|
|
type RateLimitingInfo struct {
|
|
|
|
Remaining float64
|
2022-03-12 19:22:00 +00:00
|
|
|
Used int
|
2022-03-12 18:25:34 +00:00
|
|
|
Reset int
|
|
|
|
Present bool
|
2022-03-12 21:00:41 +00:00
|
|
|
Timestamp string
|
2022-03-12 18:25:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:57:09 +00:00
|
|
|
var backoffSchedule = []time.Duration{
|
|
|
|
4 * time.Second,
|
|
|
|
8 * time.Second,
|
|
|
|
16 * time.Second,
|
|
|
|
}
|
|
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
2022-03-26 17:40:51 +00:00
|
|
|
func NewClient(id, secret string, statsd statsd.ClientInterface, redis *redis.Client, connLimit int, opts ...RequestOption) *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-09-25 13:19:42 +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))
|
2021-09-25 13:19:42 +00:00
|
|
|
_ = statsd.Histogram("reddit.api.connections.idle_time", idleTime, []string{}, 0.1)
|
2021-07-08 02:44:46 +00:00
|
|
|
}
|
2021-07-08 02:19:02 +00:00
|
|
|
} else {
|
2021-09-25 13:19:42 +00:00
|
|
|
_ = statsd.Incr("reddit.api.connections.created", []string{}, 0.1)
|
2021-07-08 02:19:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-13 20:38:10 +00:00
|
|
|
/*
|
|
|
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
t.MaxIdleConns = connLimit / 4 / 100
|
|
|
|
t.MaxConnsPerHost = connLimit / 100
|
|
|
|
t.MaxIdleConnsPerHost = connLimit / 4 / 100
|
|
|
|
t.IdleConnTimeout = 60 * time.Second
|
|
|
|
t.ResponseHeaderTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
client := &http.Client{Transport: t}
|
|
|
|
*/
|
|
|
|
client := &http.Client{}
|
2021-07-07 20:24:23 +00:00
|
|
|
|
2021-07-15 14:51:34 +00:00
|
|
|
pool := &fastjson.ParserPool{}
|
2021-07-08 01:01:54 +00:00
|
|
|
|
2021-07-07 20:24:23 +00:00
|
|
|
return &Client{
|
|
|
|
id,
|
|
|
|
secret,
|
|
|
|
client,
|
2021-07-08 02:19:02 +00:00
|
|
|
tracer,
|
2021-07-15 14:51:34 +00:00
|
|
|
pool,
|
2021-07-08 02:19:02 +00:00
|
|
|
statsd,
|
2022-03-12 17:50:05 +00:00
|
|
|
redis,
|
2022-03-26 17:40:51 +00:00
|
|
|
opts,
|
2021-07-07 20:24:23 +00:00
|
|
|
}
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthenticatedClient struct {
|
2022-05-25 23:22:05 +00:00
|
|
|
client *Client
|
2021-05-10 00:51:15 +00:00
|
|
|
|
2022-05-26 00:17:03 +00:00
|
|
|
redditId string
|
|
|
|
refreshToken string
|
|
|
|
accessToken string
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 17:50:05 +00:00
|
|
|
func (rc *Client) NewAuthenticatedClient(redditId, refreshToken, accessToken string) *AuthenticatedClient {
|
|
|
|
if redditId == "" {
|
|
|
|
panic("requires a redditId")
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:28:41 +00:00
|
|
|
if accessToken == "" {
|
|
|
|
panic("requires an access token")
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:49:14 +00:00
|
|
|
if refreshToken == "" {
|
|
|
|
panic("requires a refresh token")
|
|
|
|
}
|
|
|
|
|
2022-05-26 00:17:03 +00:00
|
|
|
return &AuthenticatedClient{rc, redditId, refreshToken, accessToken}
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rc *Client) doRequest(ctx context.Context, r *Request) ([]byte, *RateLimitingInfo, error) {
|
|
|
|
req, err := r.HTTPRequest(ctx)
|
2021-05-10 00:51:15 +00:00
|
|
|
if err != nil {
|
2022-03-12 18:25:34 +00:00
|
|
|
return nil, nil, err
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
req = req.WithContext(httptrace.WithClientTrace(ctx, rc.tracer))
|
2021-07-08 02:19:02 +00:00
|
|
|
|
2021-07-08 23:26:15 +00:00
|
|
|
start := time.Now()
|
2021-10-28 14:57:09 +00:00
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
client := r.client
|
|
|
|
if client == nil {
|
|
|
|
client = rc.client
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2021-10-28 14:57:09 +00:00
|
|
|
|
|
|
|
_ = rc.statsd.Incr("reddit.api.calls", r.tags, 0.1)
|
|
|
|
_ = rc.statsd.Histogram("reddit.api.latency", float64(time.Since(start).Milliseconds()), r.tags, 0.1)
|
2021-07-08 23:26:15 +00:00
|
|
|
|
2021-05-10 00:51:15 +00:00
|
|
|
if err != nil {
|
2021-10-28 14:57:09 +00:00
|
|
|
_ = rc.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
2021-08-14 18:07:19 +00:00
|
|
|
if strings.Contains(err.Error(), "http2: timeout awaiting response headers") {
|
2022-03-12 18:25:34 +00:00
|
|
|
return nil, nil, ErrTimeout
|
2021-08-14 18:07:19 +00:00
|
|
|
}
|
2022-03-12 18:25:34 +00:00
|
|
|
return nil, nil, err
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
2021-07-07 20:24:23 +00:00
|
|
|
defer resp.Body.Close()
|
2021-05-10 00:51:15 +00:00
|
|
|
|
2022-03-12 18:25:34 +00:00
|
|
|
rli := &RateLimitingInfo{Present: false}
|
2022-03-12 18:45:50 +00:00
|
|
|
if resp.Header.Get(RateLimitRemainingHeader) != "" {
|
2022-03-12 18:25:34 +00:00
|
|
|
rli.Present = true
|
|
|
|
rli.Remaining, _ = strconv.ParseFloat(resp.Header.Get(RateLimitRemainingHeader), 64)
|
2022-03-12 19:22:00 +00:00
|
|
|
rli.Used, _ = strconv.Atoi(resp.Header.Get(RateLimitUsedHeader))
|
2022-03-12 18:25:34 +00:00
|
|
|
rli.Reset, _ = strconv.Atoi(resp.Header.Get(RateLimitResetHeader))
|
2022-03-12 21:00:41 +00:00
|
|
|
rli.Timestamp = time.Now().String()
|
2022-03-12 17:50:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 21:26:40 +00:00
|
|
|
bb, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
2022-05-23 15:33:15 +00:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case 200:
|
|
|
|
return bb, rli, err
|
2022-07-13 17:09:11 +00:00
|
|
|
case 401, 403:
|
2022-05-23 15:33:15 +00:00
|
|
|
return nil, rli, ErrOauthRevoked
|
|
|
|
default:
|
2021-10-28 14:57:09 +00:00
|
|
|
_ = rc.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
2022-05-23 21:26:40 +00:00
|
|
|
return nil, rli, ServerError{string(bb), resp.StatusCode}
|
2021-10-28 14:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-15 14:51:34 +00:00
|
|
|
|
2022-05-26 21:54:02 +00:00
|
|
|
func (rc *Client) request(ctx context.Context, r *Request, rh ResponseHandler, empty interface{}) (interface{}, error) {
|
|
|
|
bb, _, err := rc.doRequest(ctx, r)
|
|
|
|
|
|
|
|
if err != nil && err != ErrOauthRevoked && r.retry {
|
|
|
|
for _, backoff := range backoffSchedule {
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
time.AfterFunc(backoff, func() {
|
|
|
|
_ = rc.statsd.Incr("reddit.api.retries", r.tags, 0.1)
|
|
|
|
bb, _, err = rc.doRequest(ctx, r)
|
|
|
|
done <- struct{}{}
|
|
|
|
})
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = rc.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
|
|
|
if strings.Contains(err.Error(), "http2: timeout awaiting response headers") {
|
|
|
|
return nil, ErrTimeout
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.emptyResponseBytes > 0 && len(bb) == r.emptyResponseBytes {
|
|
|
|
return empty, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := rc.pool.Get()
|
|
|
|
defer rc.pool.Put(parser)
|
|
|
|
|
|
|
|
val, err := parser.ParseBytes(bb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rh(val), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *Client) subredditPosts(ctx context.Context, subreddit string, sort string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
url := fmt.Sprintf("https://www.reddit.com/r/%s/%s.json", subreddit, sort)
|
|
|
|
opts = append(rc.defaultOpts, opts...)
|
|
|
|
opts = append(opts, []RequestOption{
|
|
|
|
WithMethod("GET"),
|
|
|
|
WithURL(url),
|
|
|
|
}...)
|
|
|
|
req := NewRequest(opts...)
|
|
|
|
|
|
|
|
lr, err := rc.request(ctx, req, NewListingResponse, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lr.(*ListingResponse), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *Client) SubredditHot(ctx context.Context, subreddit string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
return rc.subredditPosts(ctx, subreddit, "hot", opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *Client) SubredditTop(ctx context.Context, subreddit string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
return rc.subredditPosts(ctx, subreddit, "top", opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *Client) SubredditNew(ctx context.Context, subreddit string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
return rc.subredditPosts(ctx, subreddit, "new", opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *Client) SubredditAbout(ctx context.Context, subreddit string, opts ...RequestOption) (*SubredditResponse, error) {
|
|
|
|
url := fmt.Sprintf("https://www.reddit.com/r/%s/about.json", subreddit)
|
|
|
|
opts = append(rc.defaultOpts, opts...)
|
|
|
|
opts = append(opts, []RequestOption{
|
|
|
|
WithMethod("GET"),
|
|
|
|
WithURL(url),
|
|
|
|
}...)
|
|
|
|
req := NewRequest(opts...)
|
|
|
|
srr, err := rc.request(ctx, req, NewSubredditResponse, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == ErrOauthRevoked {
|
|
|
|
return nil, ErrSubredditIsPrivate
|
|
|
|
} else if serr, ok := err.(ServerError); ok {
|
|
|
|
if serr.StatusCode == 404 {
|
|
|
|
return nil, ErrSubredditNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sr := srr.(*SubredditResponse)
|
|
|
|
if sr.Quarantined {
|
|
|
|
return nil, ErrSubredditIsQuarantined
|
|
|
|
}
|
|
|
|
|
|
|
|
return sr, nil
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) request(ctx context.Context, r *Request, rh ResponseHandler, empty interface{}) (interface{}, error) {
|
2022-03-12 18:45:50 +00:00
|
|
|
if rac.isRateLimited() {
|
2022-03-12 17:50:05 +00:00
|
|
|
return nil, ErrRateLimited
|
|
|
|
}
|
|
|
|
|
2022-03-26 17:40:51 +00:00
|
|
|
if err := rac.logRequest(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:22:05 +00:00
|
|
|
bb, rli, err := rac.client.doRequest(ctx, r)
|
2022-03-12 17:50:05 +00:00
|
|
|
|
2022-05-23 21:37:51 +00:00
|
|
|
if err != nil && err != ErrOauthRevoked && r.retry {
|
2021-10-28 14:57:09 +00:00
|
|
|
for _, backoff := range backoffSchedule {
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
time.AfterFunc(backoff, func() {
|
2022-05-25 23:22:05 +00:00
|
|
|
_ = rac.client.statsd.Incr("reddit.api.retries", r.tags, 0.1)
|
2022-03-26 17:40:51 +00:00
|
|
|
|
|
|
|
if err = rac.logRequest(); err != nil {
|
|
|
|
done <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:22:05 +00:00
|
|
|
bb, rli, err = rac.client.doRequest(ctx, r)
|
2021-10-28 14:57:09 +00:00
|
|
|
done <- struct{}{}
|
|
|
|
})
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-25 23:22:05 +00:00
|
|
|
_ = rac.client.statsd.Incr("reddit.api.errors", r.tags, 0.1)
|
2021-10-28 14:57:09 +00:00
|
|
|
if strings.Contains(err.Error(), "http2: timeout awaiting response headers") {
|
|
|
|
return nil, ErrTimeout
|
|
|
|
}
|
|
|
|
return nil, err
|
2022-03-12 18:45:50 +00:00
|
|
|
} else {
|
2022-03-26 17:40:51 +00:00
|
|
|
_ = rac.markRateLimited(rli)
|
2021-07-12 18:36:08 +00:00
|
|
|
}
|
2021-07-15 17:27:48 +00:00
|
|
|
|
|
|
|
if r.emptyResponseBytes > 0 && len(bb) == r.emptyResponseBytes {
|
|
|
|
return empty, nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:22:05 +00:00
|
|
|
parser := rac.client.pool.Get()
|
|
|
|
defer rac.client.pool.Put(parser)
|
2021-07-15 17:40:29 +00:00
|
|
|
|
2021-07-15 15:51:04 +00:00
|
|
|
val, err := parser.ParseBytes(bb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rh(val), nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 19:46:36 +00:00
|
|
|
func (rac *AuthenticatedClient) logRequest() error {
|
|
|
|
if rac.redditId == SkipRateLimiting {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:22:05 +00:00
|
|
|
return rac.client.redis.HIncrBy(context.Background(), "reddit:requests", rac.redditId, 1).Err()
|
2022-03-12 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 18:45:50 +00:00
|
|
|
func (rac *AuthenticatedClient) isRateLimited() bool {
|
2022-03-12 17:50:05 +00:00
|
|
|
if rac.redditId == SkipRateLimiting {
|
2022-03-12 18:45:50 +00:00
|
|
|
return false
|
2022-03-12 17:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key := fmt.Sprintf("reddit:%s:ratelimited", rac.redditId)
|
2022-05-25 23:22:05 +00:00
|
|
|
_, err := rac.client.redis.Get(context.Background(), key).Result()
|
2022-03-12 18:45:50 +00:00
|
|
|
return err != redis.Nil
|
2022-03-12 17:50:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 18:45:50 +00:00
|
|
|
func (rac *AuthenticatedClient) markRateLimited(rli *RateLimitingInfo) error {
|
2022-03-12 17:50:05 +00:00
|
|
|
if rac.redditId == SkipRateLimiting {
|
|
|
|
return ErrRequiresRedditId
|
|
|
|
}
|
|
|
|
|
2022-03-12 18:45:50 +00:00
|
|
|
if !rli.Present {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if rli.Remaining > RequestRemainingBuffer {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:22:05 +00:00
|
|
|
_ = rac.client.statsd.Incr("reddit.api.ratelimit", nil, 1.0)
|
2022-03-12 18:45:50 +00:00
|
|
|
|
2022-03-12 17:50:05 +00:00
|
|
|
key := fmt.Sprintf("reddit:%s:ratelimited", rac.redditId)
|
2022-03-12 18:45:50 +00:00
|
|
|
duration := time.Duration(rli.Reset) * time.Second
|
2022-03-12 19:22:00 +00:00
|
|
|
info := fmt.Sprintf("%+v", *rli)
|
2022-03-12 21:00:41 +00:00
|
|
|
|
|
|
|
if rli.Used > 2000 {
|
2022-05-25 23:22:05 +00:00
|
|
|
_, err := rac.client.redis.HSet(context.Background(), "reddit:ratelimited:crazy", rac.redditId, info).Result()
|
2022-03-12 21:00:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:22:05 +00:00
|
|
|
_, err := rac.client.redis.SetEX(context.Background(), key, info, duration).Result()
|
2022-03-12 17:50:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) RefreshTokens(ctx context.Context, opts ...RequestOption) (*RefreshTokenResponse, error) {
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
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"),
|
2021-07-15 22:47:11 +00:00
|
|
|
WithURL("https://www.reddit.com/api/v1/access_token"),
|
2021-05-10 00:51:15 +00:00
|
|
|
WithBody("grant_type", "refresh_token"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithBody("refresh_token", rac.refreshToken),
|
2022-05-25 23:22:05 +00:00
|
|
|
WithBasicAuth(rac.client.id, rac.client.secret),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2022-03-26 17:29:58 +00:00
|
|
|
req := NewRequest(opts...)
|
2021-05-10 00:51:15 +00:00
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
rtr, err := rac.request(ctx, req, NewRefreshTokenResponse, nil)
|
2021-06-24 02:19:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 22:53:22 +00:00
|
|
|
|
|
|
|
ret := rtr.(*RefreshTokenResponse)
|
|
|
|
if ret.RefreshToken == "" {
|
2022-05-26 00:17:03 +00:00
|
|
|
ret.RefreshToken = rac.refreshToken
|
2021-07-15 22:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) AboutInfo(ctx context.Context, fullname string, opts ...RequestOption) (*ListingResponse, error) {
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-10-17 14:17:41 +00:00
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2021-10-17 14:17:41 +00:00
|
|
|
WithURL("https://oauth.reddit.com/api/info"),
|
|
|
|
WithQuery("id", fullname),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-10-17 14:17:41 +00:00
|
|
|
req := NewRequest(opts...)
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
lr, err := rac.request(ctx, req, NewListingResponse, nil)
|
2021-10-17 14:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lr.(*ListingResponse), nil
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) UserPosts(ctx context.Context, user string, opts ...RequestOption) (*ListingResponse, error) {
|
2022-03-14 13:40:18 +00:00
|
|
|
url := fmt.Sprintf("https://oauth.reddit.com/u/%s/submitted", user)
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-10-09 14:59:20 +00:00
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2021-10-09 14:59:20 +00:00
|
|
|
WithURL(url),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-10-09 14:59:20 +00:00
|
|
|
req := NewRequest(opts...)
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
lr, err := rac.request(ctx, req, NewListingResponse, nil)
|
2021-10-09 14:59:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lr.(*ListingResponse), nil
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) UserAbout(ctx context.Context, user string, opts ...RequestOption) (*UserResponse, error) {
|
2022-03-14 13:40:18 +00:00
|
|
|
url := fmt.Sprintf("https://oauth.reddit.com/u/%s/about", user)
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-10-09 14:59:20 +00:00
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2021-10-09 14:59:20 +00:00
|
|
|
WithURL(url),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-10-09 14:59:20 +00:00
|
|
|
req := NewRequest(opts...)
|
2022-05-07 16:37:21 +00:00
|
|
|
ur, err := rac.request(ctx, req, NewUserResponse, nil)
|
2021-10-09 14:59:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ur.(*UserResponse), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) SubredditAbout(ctx context.Context, subreddit string, opts ...RequestOption) (*SubredditResponse, error) {
|
2022-03-14 13:40:18 +00:00
|
|
|
url := fmt.Sprintf("https://oauth.reddit.com/r/%s/about", subreddit)
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-09-25 16:56:01 +00:00
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2021-09-25 16:56:01 +00:00
|
|
|
WithURL(url),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-09-25 16:56:01 +00:00
|
|
|
req := NewRequest(opts...)
|
2022-05-26 21:54:02 +00:00
|
|
|
srr, err := rac.request(ctx, req, NewSubredditResponse, nil)
|
2021-09-25 16:56:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-05-26 21:54:02 +00:00
|
|
|
if err == ErrOauthRevoked {
|
|
|
|
return nil, ErrSubredditIsPrivate
|
|
|
|
} else if serr, ok := err.(ServerError); ok {
|
|
|
|
if serr.StatusCode == 404 {
|
|
|
|
return nil, ErrSubredditNotFound
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 16:56:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:54:02 +00:00
|
|
|
sr := srr.(*SubredditResponse)
|
|
|
|
if sr.Quarantined {
|
|
|
|
return nil, ErrSubredditIsQuarantined
|
|
|
|
}
|
|
|
|
|
|
|
|
return sr, nil
|
2021-09-25 16:56:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) subredditPosts(ctx context.Context, subreddit string, sort string, opts ...RequestOption) (*ListingResponse, error) {
|
2022-03-14 13:40:18 +00:00
|
|
|
url := fmt.Sprintf("https://oauth.reddit.com/r/%s/%s", subreddit, sort)
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-09-25 16:56:01 +00:00
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2021-09-25 16:56:01 +00:00
|
|
|
WithURL(url),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-09-25 16:56:01 +00:00
|
|
|
req := NewRequest(opts...)
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
lr, err := rac.request(ctx, req, NewListingResponse, nil)
|
2021-09-25 16:56:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lr.(*ListingResponse), nil
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) SubredditHot(ctx context.Context, subreddit string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
return rac.subredditPosts(ctx, subreddit, "hot", opts...)
|
2021-09-25 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) SubredditTop(ctx context.Context, subreddit string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
return rac.subredditPosts(ctx, subreddit, "top", opts...)
|
2021-10-10 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) SubredditNew(ctx context.Context, subreddit string, opts ...RequestOption) (*ListingResponse, error) {
|
|
|
|
return rac.subredditPosts(ctx, subreddit, "new", opts...)
|
2021-09-25 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) MessageInbox(ctx context.Context, opts ...RequestOption) (*ListingResponse, error) {
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
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"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2022-03-14 13:40:18 +00:00
|
|
|
WithURL("https://oauth.reddit.com/message/inbox"),
|
2021-07-15 17:27:48 +00:00
|
|
|
WithEmptyResponseBytes(122),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-07-15 14:51:34 +00:00
|
|
|
req := NewRequest(opts...)
|
2021-06-24 02:19:43 +00:00
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
lr, err := rac.request(ctx, req, NewListingResponse, EmptyListingResponse)
|
2021-07-15 00:52:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 15:51:04 +00:00
|
|
|
return lr.(*ListingResponse), nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) MessageUnread(ctx context.Context, opts ...RequestOption) (*ListingResponse, error) {
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-07-13 23:33:25 +00:00
|
|
|
WithTags([]string{"url:/api/v1/message/unread"}),
|
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2022-03-14 13:40:18 +00:00
|
|
|
WithURL("https://oauth.reddit.com/message/unread"),
|
2021-07-15 17:27:48 +00:00
|
|
|
WithEmptyResponseBytes(122),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-07-13 23:33:25 +00:00
|
|
|
|
2021-07-15 14:51:34 +00:00
|
|
|
req := NewRequest(opts...)
|
2021-07-13 23:33:25 +00:00
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
lr, err := rac.request(ctx, req, NewListingResponse, EmptyListingResponse)
|
2021-07-13 23:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 15:51:04 +00:00
|
|
|
return lr.(*ListingResponse), nil
|
2021-07-13 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (rac *AuthenticatedClient) Me(ctx context.Context, opts ...RequestOption) (*MeResponse, error) {
|
2022-05-25 23:22:05 +00:00
|
|
|
opts = append(rac.client.defaultOpts, opts...)
|
2022-03-26 17:40:51 +00:00
|
|
|
opts = append(opts, []RequestOption{
|
2021-07-08 23:26:15 +00:00
|
|
|
WithTags([]string{"url:/api/v1/me"}),
|
2021-05-10 00:51:15 +00:00
|
|
|
WithMethod("GET"),
|
2022-05-26 00:17:03 +00:00
|
|
|
WithToken(rac.accessToken),
|
2021-05-10 00:51:15 +00:00
|
|
|
WithURL("https://oauth.reddit.com/api/v1/me"),
|
2022-03-26 17:40:51 +00:00
|
|
|
}...)
|
2021-05-10 00:51:15 +00:00
|
|
|
|
2022-03-26 17:29:58 +00:00
|
|
|
req := NewRequest(opts...)
|
2022-05-07 16:37:21 +00:00
|
|
|
mr, err := rac.request(ctx, req, NewMeResponse, nil)
|
2021-05-10 00:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 15:51:04 +00:00
|
|
|
return mr.(*MeResponse), nil
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|