apollo-backend/internal/reddit/client.go

647 lines
16 KiB
Go
Raw Normal View History

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"
2022-10-27 01:59:44 +00:00
"io"
2021-05-10 00:51:15 +00:00
"net/http"
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 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"
"github.com/valyala/fastjson"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
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
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
tracer trace.Tracer
2022-10-27 01:59:44 +00:00
client *http.Client
2022-03-26 17:40:51 +00:00
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
Used int
2022-03-12 18:25:34 +00:00
Reset int
Present bool
Timestamp string
2022-03-12 18:25:34 +00:00
}
var (
backoffSchedule = []time.Duration{
200 * time.Millisecond,
500 * time.Millisecond,
1 * time.Second,
2 * time.Second,
}
defaultErrorMap = map[int]error{
401: ErrOauthRevoked,
403: ErrOauthRevoked,
2022-11-09 19:19:14 +00:00
404: ErrSubredditNotFound,
2023-03-16 13:33:51 +00:00
429: ErrTooManyRequests,
}
)
2021-10-28 14:57:09 +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 ""
}
func NewClient(id, secret string, tracer trace.Tracer, statsd statsd.ClientInterface, redis *redis.Client, connLimit int, opts ...RequestOption) *Client {
2021-07-15 14:51:34 +00:00
pool := &fastjson.ParserPool{}
// Preallocate pool
parsers := make([]*fastjson.Parser, connLimit)
for i := 0; i < connLimit; i++ {
parsers[i] = pool.Get()
}
for i := 0; i < connLimit; i++ {
pool.Put(parsers[i])
}
2022-11-05 20:08:00 +00:00
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100
httpClient := &http.Client{
2022-11-05 20:08:00 +00:00
Transport: otelhttp.NewTransport(t),
Timeout: 4 * time.Second,
}
return &Client{
id,
secret,
tracer,
httpClient,
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-05-10 00:51:15 +00:00
}
type AuthenticatedClient struct {
client *Client
2021-05-10 00:51:15 +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")
}
return &AuthenticatedClient{rc, redditId, refreshToken, accessToken}
2021-05-10 00:51:15 +00:00
}
func (rc *Client) doRequest(ctx context.Context, r *Request, errmap map[int]error) ([]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
}
2021-07-08 23:26:15 +00:00
start := time.Now()
2021-10-28 14:57:09 +00:00
2022-10-27 01:59:44 +00:00
resp, err := rc.client.Do(req)
2021-10-28 14:57:09 +00:00
_ = rc.statsd.Incr("reddit.api.calls", 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
}
2022-10-27 01:59:44 +00:00
bb, err := io.ReadAll(resp.Body)
resp.Body.Close()
_ = rc.statsd.Histogram("reddit.api.latency", float64(time.Since(start).Milliseconds()), r.tags, 0.1)
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)
rli.Used, _ = strconv.Atoi(resp.Header.Get(RateLimitUsedHeader))
2022-03-12 18:25:34 +00:00
rli.Reset, _ = strconv.Atoi(resp.Header.Get(RateLimitResetHeader))
rli.Timestamp = time.Now().String()
2022-03-12 17:50:05 +00:00
}
if resp.StatusCode == 200 {
return bb, rli, nil
}
_ = rc.statsd.Incr("reddit.api.errors", r.tags, 0.1)
if err, ok := errmap[resp.StatusCode]; ok {
return nil, rli, err
} else {
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
func (rc *Client) request(ctx context.Context, r *Request, errmap map[int]error, rh ResponseHandler, empty interface{}) (interface{}, error) {
bb, _, err := rc.doRequest(ctx, r, errmap)
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, errmap)
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) {
2022-11-09 18:54:03 +00:00
url := fmt.Sprintf("https://oauth.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, defaultErrorMap, 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) {
2022-11-09 18:59:04 +00:00
url := fmt.Sprintf("https://oauth.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, defaultErrorMap, 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-10-26 22:28:11 +00:00
func obfuscate(tok string) string {
tl := len(tok)
if tl < 6 {
return "<SHORT>"
}
return fmt.Sprintf("%s...%s", tok[0:3], tok[tl-3:tl])
}
func (rac *AuthenticatedClient) ObfuscatedAccessToken() string {
return obfuscate(rac.accessToken)
}
func (rac *AuthenticatedClient) ObfuscatedRefreshToken() string {
return obfuscate(rac.refreshToken)
}
func (rac *AuthenticatedClient) request(ctx context.Context, r *Request, errmap map[int]error, 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
}
bb, rli, err := rac.client.doRequest(ctx, r, errmap)
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() {
_ = 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
}
bb, rli, err = rac.client.doRequest(ctx, r, errmap)
2021-10-28 14:57:09 +00:00
done <- struct{}{}
})
<-done
if err == nil {
break
}
}
}
if err != nil {
_ = 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
}
if r.emptyResponseBytes > 0 && len(bb) == r.emptyResponseBytes {
return empty, nil
}
parser := rac.client.pool.Get()
defer rac.client.pool.Put(parser)
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-10-27 00:20:45 +00:00
//nolint:unparam
2022-03-12 19:46:36 +00:00
func (rac *AuthenticatedClient) logRequest() error {
if rac.redditId == SkipRateLimiting {
return nil
}
2022-07-13 22:43:27 +00:00
return nil
// 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-07-13 22:43:27 +00:00
return false
/*
if rac.redditId == SkipRateLimiting {
return false
}
2022-03-12 17:50:05 +00:00
2022-07-13 22:43:27 +00:00
key := fmt.Sprintf("reddit:%s:ratelimited", rac.redditId)
_, err := rac.client.redis.Get(context.Background(), key).Result()
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-07-13 22:43:27 +00:00
return nil
2022-03-12 17:50:05 +00:00
2022-07-13 22:43:27 +00:00
/*
if rac.redditId == SkipRateLimiting {
return ErrRequiresRedditId
}
2022-03-12 18:45:50 +00:00
2022-07-13 22:43:27 +00:00
if !rli.Present {
return nil
}
2022-03-12 18:45:50 +00:00
2022-07-13 22:43:27 +00:00
if rli.Remaining > RequestRemainingBuffer {
return nil
}
_ = rac.client.statsd.Incr("reddit.api.ratelimit", nil, 1.0)
2022-03-12 18:45:50 +00:00
2022-07-13 22:43:27 +00:00
key := fmt.Sprintf("reddit:%s:ratelimited", rac.redditId)
duration := time.Duration(rli.Reset) * time.Second
info := fmt.Sprintf("%+v", *rli)
2022-07-13 22:43:27 +00:00
if rli.Used > 2000 {
_, err := rac.client.redis.HSet(context.Background(), "reddit:ratelimited:crazy", rac.redditId, info).Result()
if err != nil {
return err
}
}
2022-07-13 22:43:27 +00:00
_, err := rac.client.redis.SetEX(context.Background(), key, info, duration).Result()
return err
*/
2022-03-12 17:50:05 +00:00
}
func (rac *AuthenticatedClient) RefreshTokens(ctx context.Context, opts ...RequestOption) (*RefreshTokenResponse, error) {
errmap := map[int]error{
400: ErrOauthRevoked,
2023-03-16 13:33:51 +00:00
429: ErrTooManyRequests,
}
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"),
WithBody("refresh_token", rac.refreshToken),
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
rtr, err := rac.request(ctx, req, errmap, NewRefreshTokenResponse, nil)
2021-06-24 02:19:43 +00:00
if err != nil {
return nil, err
}
ret := rtr.(*RefreshTokenResponse)
if ret.RefreshToken == "" {
ret.RefreshToken = rac.refreshToken
}
return ret, nil
2021-05-10 00:51:15 +00:00
}
func (rac *AuthenticatedClient) AboutInfo(ctx context.Context, fullname string, opts ...RequestOption) (*ListingResponse, error) {
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"),
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...)
lr, err := rac.request(ctx, req, defaultErrorMap, NewListingResponse, nil)
2021-10-17 14:17:41 +00:00
if err != nil {
return nil, err
}
return lr.(*ListingResponse), nil
}
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)
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"),
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...)
lr, err := rac.request(ctx, req, defaultErrorMap, NewListingResponse, nil)
2021-10-09 14:59:20 +00:00
if err != nil {
return nil, err
}
return lr.(*ListingResponse), nil
}
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)
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"),
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...)
ur, err := rac.request(ctx, req, defaultErrorMap, NewUserResponse, nil)
2021-10-09 14:59:20 +00:00
if err != nil {
return nil, err
}
return ur.(*UserResponse), nil
}
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)
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"),
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...)
srr, err := rac.request(ctx, req, defaultErrorMap, NewSubredditResponse, nil)
2021-09-25 16:56:01 +00:00
if err != nil {
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
}
sr := srr.(*SubredditResponse)
if sr.Quarantined {
return nil, ErrSubredditIsQuarantined
}
return sr, nil
2021-09-25 16:56:01 +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)
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"),
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...)
lr, err := rac.request(ctx, req, defaultErrorMap, NewListingResponse, nil)
2021-09-25 16:56:01 +00:00
if err != nil {
return nil, err
}
return lr.(*ListingResponse), nil
}
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
}
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
}
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
}
func (rac *AuthenticatedClient) MessageInbox(ctx context.Context, opts ...RequestOption) (*ListingResponse, error) {
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"),
WithToken(rac.accessToken),
2022-03-14 13:40:18 +00:00
WithURL("https://oauth.reddit.com/message/inbox"),
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
lr, err := rac.request(ctx, req, defaultErrorMap, 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
}
func (rac *AuthenticatedClient) MessageUnread(ctx context.Context, opts ...RequestOption) (*ListingResponse, error) {
opts = append(rac.client.defaultOpts, opts...)
2022-03-26 17:40:51 +00:00
opts = append(opts, []RequestOption{
WithTags([]string{"url:/api/v1/message/unread"}),
WithMethod("GET"),
WithToken(rac.accessToken),
2022-03-14 13:40:18 +00:00
WithURL("https://oauth.reddit.com/message/unread"),
WithEmptyResponseBytes(122),
2022-03-26 17:40:51 +00:00
}...)
2021-07-15 14:51:34 +00:00
req := NewRequest(opts...)
lr, err := rac.request(ctx, req, defaultErrorMap, NewListingResponse, EmptyListingResponse)
if err != nil {
return nil, err
}
2021-07-15 15:51:04 +00:00
return lr.(*ListingResponse), nil
}
func (rac *AuthenticatedClient) Me(ctx context.Context, opts ...RequestOption) (*MeResponse, error) {
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"),
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...)
mr, err := rac.request(ctx, req, defaultErrorMap, 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
}
2022-10-19 13:37:41 +00:00
func (rac *AuthenticatedClient) TopLevelComments(ctx context.Context, subreddit string, threadID string, opts ...RequestOption) (*ThreadResponse, error) {
url := fmt.Sprintf("https://oauth.reddit.com/r/%s/comments/%s/.json", subreddit, threadID)
opts = append(rac.client.defaultOpts, opts...)
opts = append(opts, []RequestOption{
WithTags([]string{"url:/comments"}),
WithMethod("GET"),
WithToken(rac.accessToken),
WithURL(url),
WithQuery("sort", "new"),
WithQuery("limit", "100"),
WithQuery("depth", "1"),
}...)
req := NewRequest(opts...)
tr, err := rac.request(ctx, req, defaultErrorMap, NewThreadResponse, nil)
2022-10-19 13:37:41 +00:00
if err != nil {
return nil, err
}
return tr.(*ThreadResponse), nil
}