-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
155 lines (130 loc) · 2.75 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package ahrefs
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
type requestBuilder struct {
client *Client
columns string
from string
target string
mode string
where string
having string
orderBy string
limit string
// User-provided options.
opts []Option
}
func (r *requestBuilder) WithColumns(columns string) *requestBuilder {
r.columns = columns
return r
}
func (r *requestBuilder) WithFrom(from string) *requestBuilder {
r.from = from
return r
}
func (r *requestBuilder) WithTarget(target string) *requestBuilder {
r.target = target
return r
}
func (r *requestBuilder) WithMode(mode string) *requestBuilder {
r.mode = mode
return r
}
func (r *requestBuilder) WithWhere(where string) *requestBuilder {
r.where = where
return r
}
func (r *requestBuilder) WithHaving(having string) *requestBuilder {
r.having = having
return r
}
func (r *requestBuilder) WithOrderBy(orderBy string) *requestBuilder {
r.orderBy = orderBy
return r
}
func (r *requestBuilder) WithLimit(limit string) *requestBuilder {
r.limit = limit
return r
}
func (r *requestBuilder) request() (*http.Request, error) {
if !strings.HasSuffix(r.client.BaseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", r.client.BaseURL)
}
const urlStr = "/"
u, err := r.client.BaseURL.Parse(urlStr)
if err != nil {
return nil, err
}
q, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
for _, fn := range r.opts {
fn(r)
}
q.Add("output", "json")
if r.client.token != "" {
q.Add("token", r.client.token)
}
if r.columns != "" {
q.Add("select", r.columns)
}
if r.from != "" {
q.Add("from", r.from)
}
if r.target != "" {
q.Add("target", r.target)
}
if r.mode != "" {
q.Add("mode", r.mode)
}
if r.where != "" {
q.Add("where", r.where)
}
if r.having != "" {
q.Add("having", r.having)
}
if r.orderBy != "" {
q.Add("orderBy", r.orderBy)
}
if r.limit != "" {
q.Add("limit", r.limit)
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
if r.client.UserAgent != "" {
req.Header.Set("User-Agent", r.client.UserAgent)
}
return req, nil
}
// Option is a function that changes the request.
type Option func(*requestBuilder)
func WithTarget(target string) Option {
return func(rb *requestBuilder) {
rb.WithTarget(target)
}
}
func WithLimit(limit int64) Option {
return func(rb *requestBuilder) {
rb.WithLimit(strconv.FormatInt(limit, 10))
}
}
func WithHaving(having string) Option {
return func(rb *requestBuilder) {
rb.WithHaving(having)
}
}
func WithWhere(where string) Option {
return func(rb *requestBuilder) {
rb.WithWhere(where)
}
}