2021-05-10 00:51:15 +00:00
|
|
|
package reddit
|
|
|
|
|
|
|
|
import (
|
2022-05-07 16:37:21 +00:00
|
|
|
"context"
|
2021-05-10 00:51:15 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-12 19:22:00 +00:00
|
|
|
const userAgent = "server:apollo-backend:v1.0 (by /u/iamthatis) contact me@christianselig.com"
|
2021-05-10 00:51:15 +00:00
|
|
|
|
|
|
|
type Request struct {
|
2021-07-15 17:27:48 +00:00
|
|
|
body url.Values
|
|
|
|
query url.Values
|
|
|
|
method string
|
|
|
|
token string
|
|
|
|
url string
|
|
|
|
auth string
|
|
|
|
tags []string
|
|
|
|
emptyResponseBytes int
|
2021-10-28 14:57:09 +00:00
|
|
|
retry bool
|
2022-05-07 16:37:21 +00:00
|
|
|
client *http.Client
|
2021-05-10 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RequestOption func(*Request)
|
|
|
|
|
|
|
|
func NewRequest(opts ...RequestOption) *Request {
|
2021-10-28 14:57:09 +00:00
|
|
|
req := &Request{
|
|
|
|
body: url.Values{},
|
|
|
|
query: url.Values{},
|
|
|
|
method: "GET",
|
|
|
|
url: "",
|
|
|
|
|
|
|
|
token: "",
|
|
|
|
auth: "",
|
|
|
|
|
|
|
|
tags: nil,
|
|
|
|
|
|
|
|
emptyResponseBytes: 0,
|
|
|
|
retry: true,
|
2022-05-07 16:37:21 +00:00
|
|
|
client: nil,
|
2021-10-28 14:57:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 21:36:59 +00:00
|
|
|
req.query.Set("raw_json", "1")
|
|
|
|
|
2021-05-10 00:51:15 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func (r *Request) HTTPRequest(ctx context.Context) (*http.Request, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, r.method, r.url, strings.NewReader(r.body.Encode()))
|
2021-06-24 02:19:43 +00:00
|
|
|
req.URL.RawQuery = r.query.Encode()
|
|
|
|
|
2022-03-14 13:42:47 +00:00
|
|
|
req.Header.Add("Accept", "application/json")
|
2021-05-10 00:51:15 +00:00
|
|
|
req.Header.Add("User-Agent", userAgent)
|
|
|
|
|
|
|
|
if r.token != "" {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", r.token))
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.auth != "" {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", r.auth))
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
2021-07-08 23:26:15 +00:00
|
|
|
func WithTags(tags []string) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.tags = tags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:51:15 +00:00
|
|
|
func WithMethod(method string) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.method = method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithURL(url string) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.url = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBasicAuth(user, password string) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
encoded := base64.StdEncoding.EncodeToString([]byte(user + ":" + password))
|
|
|
|
req.auth = encoded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithToken(token string) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.token = token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBody(key, val string) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.body.Set(key, val)
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 02:19:43 +00:00
|
|
|
|
|
|
|
func WithQuery(key, val string) RequestOption {
|
2021-09-25 18:02:00 +00:00
|
|
|
if val == "" {
|
|
|
|
return func(req *Request) {}
|
|
|
|
}
|
|
|
|
|
2021-06-24 02:19:43 +00:00
|
|
|
return func(req *Request) {
|
|
|
|
req.query.Set(key, val)
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 17:27:48 +00:00
|
|
|
|
|
|
|
func WithEmptyResponseBytes(bytes int) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.emptyResponseBytes = bytes
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 14:57:09 +00:00
|
|
|
|
|
|
|
func WithRetry(retry bool) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.retry = retry
|
|
|
|
}
|
|
|
|
}
|
2022-05-07 16:37:21 +00:00
|
|
|
|
|
|
|
func WithClient(client *http.Client) RequestOption {
|
|
|
|
return func(req *Request) {
|
|
|
|
req.client = client
|
|
|
|
}
|
|
|
|
}
|