-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
259 lines (215 loc) · 6.29 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package gopeana
import (
"fmt"
"net/url"
"strconv"
)
// Request defines each type that implements the Client and searchURL functions
type Request interface {
searchURL() string
EscapedURL() string
Client() *Client
}
// BasicSearchRequest is a wrapper around an Europeana API Search request, defining fields such
// as reusability, profile and rows/start for basic Pagination.
// You can pass an empty string to rows, profile or start to use the API default values
// rows = "" will return 12 results, start = "" will start with item 1, profile = "" will use standard profile.
type BasicSearchRequest struct {
client *Client
reusability string
profile string
rows string
start string
}
// CursorSearchRequest is a wrapper around an Europeana API Search request, defining fields such
// as reusability, profile and rows/start for cursor-based Pagination. The first request should use cursor=*,
// for following request the value of nextCursor needs to be used. If no cursor is returned anymore, results are
// exhausted.
type CursorSearchRequest struct {
client *Client
reusability string
profile string
cursor string
}
// NewBasicSearchRequest returns a pointer to a BasicSearchRequest struct. This function will also perform error checking
// and return an error if an invalid value has been provided.
func NewBasicSearchRequest(c *Client, reusability, profile, rows, start string) (*BasicSearchRequest, error) {
var request *BasicSearchRequest
if err := checkReusability(reusability); err != nil {
return request, err
}
if err := checkProfile(profile); err != nil {
return request, err
}
if err := checkBasicPagination(rows, "rows can't be < 0", 0); err != nil {
return request, err
}
if err := checkBasicPagination(start, "start can't be < 1", 1); err != nil {
return request, err
}
return &BasicSearchRequest{
client: c,
reusability: reusability,
profile: profile,
rows: rows,
start: start,
}, nil
}
// Client returns a pointer to the client of said request
func (r *BasicSearchRequest) Client() *Client {
return r.client
}
func (r *BasicSearchRequest) searchURL() string {
url := r.Client().baseURL()
if r.reusability != "" {
url += "&reusability=" + r.reusability
}
if r.profile != "" {
url += "&profile=" + r.profile
}
if r.rows != "" {
url += "&rows=" + r.rows
}
if r.start != "" {
url += "&start=" + r.start
}
return url
}
func (r *BasicSearchRequest) EscapedURL() string {
return url.QueryEscape(r.searchURL())
}
// Reusability will set the reusability field or return an error
func (r *BasicSearchRequest) Reusability(s string) error {
if err := checkReusability(s); err != nil {
return err
}
r.reusability = s
return nil
}
// Profile will set the profile field or return an error
func (r *BasicSearchRequest) Profile(s string) error {
if err := checkProfile(s); err != nil {
return err
}
r.profile = s
return nil
}
// Rows will set the rows field or return an error
func (r *BasicSearchRequest) Rows(s string) error {
if err := checkBasicPagination(s, "rows can't be < 0", 0); err != nil {
return err
}
r.rows = s
return nil
}
// Start will set the start field or return an error
func (r *BasicSearchRequest) Start(s string) error {
if err := checkBasicPagination(s, "start can't be < 1", 1); err != nil {
return err
}
r.start = s
return nil
}
// NewCursorSearchRequest returns a pointer to a CursorSearchRequest struct. This function will perform error checking
// for the reusability and profile parameters, but not for cursor. If cursor argument is empty (""),
// will use start cursor ("*")
func NewCursorSearchRequest(c *Client, reusability, profile, cursor string) (*CursorSearchRequest, error) {
var req *CursorSearchRequest
if err := checkReusability(reusability); err != nil {
return req, err
}
if err := checkProfile(profile); err != nil {
return req, err
}
// If no cursor is provided, use start cursor
if cursor == "" {
cursor = "*"
}
return &CursorSearchRequest{
client: c,
reusability: reusability,
profile: profile,
cursor: cursor,
}, nil
}
// Client returns a pointer to the client of said request
func (r *CursorSearchRequest) Client() *Client {
return r.client
}
func (r *CursorSearchRequest) searchURL() string {
url := r.Client().baseURL()
if r.reusability != "" {
url += "&reusability=" + r.reusability
}
if r.profile != "" {
url += "&profile=" + r.profile
}
url += "&cursor=" + r.cursor
return url
}
func (r *CursorSearchRequest) EscapedURL() string {
return url.QueryEscape(r.searchURL())
}
// Reusability will set the reusability field or return an error
func (r *CursorSearchRequest) Reusability(s string) error {
if err := checkReusability(s); err != nil {
return err
}
r.reusability = s
return nil
}
// Profile will set the profile field or return an error
func (r *CursorSearchRequest) Profile(s string) error {
if err := checkProfile(s); err != nil {
return err
}
r.profile = s
return nil
}
// Cursor will set the cursor field
func (r *CursorSearchRequest) Cursor(s string) {
// TODO: URL escape
if s == "" {
s = "*"
}
r.cursor = s
}
// checkReusability will perform input validation for the reusability field
func checkReusability(s string) error {
validReusability := []string{"", "open", "restricted", "permission"}
for _, v := range validReusability {
if s == v {
return nil
}
}
return fmt.Errorf("%s not part of valid arguments: %v",
s, validReusability)
}
// checkProfile will perform input validation for the profile field
func checkProfile(s string) error {
validProfile := []string{"", "minimal", "standard", "rich"}
for _, v := range validProfile {
if s == v {
return nil
}
}
return fmt.Errorf("%s not part of valid arguments: %s",
s, validProfile)
}
// checkBasicPagination will will take a string check and try to convert it to an integer.
// If conversion fails or the converted value is smaller than a passed integer val,
// will return a custom error string passed as the info parameter.
// This function can be used to validate inputs for the rows and start field
func checkBasicPagination(check, info string, val int) error {
if check != "" {
check, err := strconv.Atoi(check)
if err != nil {
return err
}
if check < val {
return fmt.Errorf("%s", info)
}
return nil
}
return nil
}