-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
380 lines (325 loc) · 11.5 KB
/
client.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package aozora
import (
"context"
"fmt"
"net/url"
"strconv"
"time"
"github.com/goark/errs"
"github.com/goark/fetch"
)
const (
defaultAPIDir = "api/v0.1"
)
//Client is http.Client for Aozora API Server.
type Client struct {
server *Server
client fetch.Client
}
//SearchBooksParamsFunc is self-referential function for functional options pattern.
type SearchBooksParamsFunc func(url.Values)
//SearchBooksRaw gets list of books. (raw data)
func (c *Client) SearchBooksRaw(opts ...SearchBooksParamsFunc) ([]byte, error) {
return c.SearchBooksRawContext(context.Background(), opts...)
}
//SearchBooks gets list of books. (struct data)
func (c *Client) SearchBooks(opts ...SearchBooksParamsFunc) ([]Book, error) {
return c.SearchBooksContext(context.Background(), opts...)
}
//SearchBooksRawContext gets list of books with context.Context. (raw data)
func (c *Client) SearchBooksRawContext(ctx context.Context, opts ...SearchBooksParamsFunc) ([]byte, error) {
params := url.Values{}
for _, opt := range opts {
opt(params)
}
return c.get(ctx, c.makeSearchCommand(TargetBooks, params))
}
//SearchBooksContext gets list of books with context.Context. (struct data)
func (c *Client) SearchBooksContext(ctx context.Context, opts ...SearchBooksParamsFunc) ([]Book, error) {
b, err := c.SearchBooksRawContext(ctx, opts...)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodeBooks(b)
}
//WithBookTitle returns function for setting Marketplace.
func WithBookTitle(title string) SearchBooksParamsFunc {
return func(params url.Values) {
if params != nil && len(title) > 0 {
params.Set("title", title)
}
}
}
//WithBookAuthor returns function for setting Marketplace.
func WithBookAuthor(author string) SearchBooksParamsFunc {
return func(params url.Values) {
if params != nil && len(author) > 0 {
params.Set("author", author)
}
}
}
//WithBookFields returns function for setting Marketplace.
func WithBookFields(fields string) SearchBooksParamsFunc {
return func(params url.Values) {
if params != nil && len(fields) > 0 {
params.Add("fields", fields)
}
}
}
//WithBookLimit returns function for setting Marketplace.
func WithBookLimit(limit int) SearchBooksParamsFunc {
return func(params url.Values) {
if params != nil && limit > 0 {
params.Set("limit", strconv.Itoa(limit))
}
}
}
//WithBookSkip returns function for setting Marketplace.
func WithBookSkip(skip int) SearchBooksParamsFunc {
return func(params url.Values) {
if params != nil && skip > 0 {
params.Set("skip", strconv.Itoa(skip))
}
}
}
//WithBookAfter returns function for setting Marketplace.
func WithBookAfter(after time.Time) SearchBooksParamsFunc {
return func(params url.Values) {
if params != nil && !after.IsZero() {
params.Set("after", after.Format("2006-01-02"))
}
}
}
//SearchPersonsParamsFunc is self-referential function for functional options pattern.
type SearchPersonsParamsFunc func(url.Values)
//SearchPersonsRaw gets list of persons. (raw data)
func (c *Client) SearchPersonsRaw(opts ...SearchPersonsParamsFunc) ([]byte, error) {
return c.SearchPersonsRawContext(context.Background(), opts...)
}
//SearchPersons gets list of persons. (struct data)
func (c *Client) SearchPersons(opts ...SearchPersonsParamsFunc) ([]Person, error) {
return c.SearchPersonsContext(context.Background(), opts...)
}
//SearchPersonsRawContext gets list of persons with context.Context. (raw data)
func (c *Client) SearchPersonsRawContext(ctx context.Context, opts ...SearchPersonsParamsFunc) ([]byte, error) {
params := url.Values{}
for _, opt := range opts {
opt(params)
}
return c.get(ctx, c.makeSearchCommand(TargetPersons, params))
}
//SearchPersonsContext gets list of persons with context.Context. (struct data)
func (c *Client) SearchPersonsContext(ctx context.Context, opts ...SearchPersonsParamsFunc) ([]Person, error) {
b, err := c.SearchPersonsRawContext(ctx, opts...)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodePersons(b)
}
//WithPersonName returns function for setting Marketplace.
func WithPersonName(name string) SearchPersonsParamsFunc {
return func(params url.Values) {
if params != nil && len(name) > 0 {
params.Set("name", name)
}
}
}
//SearchPersonsParamsFunc is self-referential function for functional options pattern.
type SearchWorkersParamsFunc func(url.Values)
//SearchWorkersRaw gets list of workers (raw data)
func (c *Client) SearchWorkersRaw(opts ...SearchWorkersParamsFunc) ([]byte, error) {
return c.SearchWorkersRawContext(context.Background(), opts...)
}
//SearchWorkers gets list of workers (struct data)
func (c *Client) SearchWorkers(opts ...SearchWorkersParamsFunc) ([]Worker, error) {
return c.SearchWorkersContext(context.Background(), opts...)
}
//SearchWorkersRawContext gets list of workers with context.Context. (raw data)
func (c *Client) SearchWorkersRawContext(ctx context.Context, opts ...SearchWorkersParamsFunc) ([]byte, error) {
params := url.Values{}
for _, opt := range opts {
opt(params)
}
return c.get(ctx, c.makeSearchCommand(TargetWorkers, params))
}
//SearchWorkersContext gets list of workers with context.Context. (struct data)
func (c *Client) SearchWorkersContext(ctx context.Context, opts ...SearchWorkersParamsFunc) ([]Worker, error) {
b, err := c.SearchWorkersRawContext(ctx, opts...)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodeWorkers(b)
}
//WithWorkerName returns function for setting Marketplace.
func WithWorkerName(name string) SearchWorkersParamsFunc {
return func(params url.Values) {
if params != nil && len(name) > 0 {
params.Set("name", name)
}
}
}
//LookupBookRaw gets book data. (raw data)
func (c *Client) LookupBookRaw(id int) ([]byte, error) {
return c.LookupBookRawContext(context.Background(), id)
}
//LookupBook gets books data. (struct data)
func (c *Client) LookupBook(id int) (*Book, error) {
return c.LookupBookContext(context.Background(), id)
}
//LookupBookCardRaw gets book card info (HTML page data)
func (c *Client) LookupBookCardRaw(id int) ([]byte, error) {
return c.LookupBookCardRawContext(context.Background(), id)
}
//LookupBookContentRaw gets book content (plain or HTML formatted text data)
func (c *Client) LookupBookContentRaw(id int, f Format) ([]byte, error) {
return c.LookupBookContentRawContext(context.Background(), id, f)
}
//LookupBookRawContext gets book data with context.Context. (raw data)
func (c *Client) LookupBookRawContext(ctx context.Context, id int) ([]byte, error) {
return c.get(ctx, c.makeLookupCommand(TargetBooks, id))
}
//LookupBookContext gets books data with context.Context. (struct data)
func (c *Client) LookupBookContext(ctx context.Context, id int) (*Book, error) {
b, err := c.LookupBookRawContext(ctx, id)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodeBook(b)
}
//LookupBookCardRawContext gets book card info with context.Context. (HTML page data)
func (c *Client) LookupBookCardRawContext(ctx context.Context, id int) ([]byte, error) {
return c.get(ctx, c.makeCardCommand(id))
}
//LookupBookContentRawContext gets book content with context.Context. (plain or HTML formatted text data)
func (c *Client) LookupBookContentRawContext(ctx context.Context, id int, f Format) ([]byte, error) {
return c.get(ctx, c.makeContentCommand(id, f))
}
//LookupPersonRaw gets person data. (raw data)
func (c *Client) LookupPersonRaw(id int) ([]byte, error) {
return c.LookupPersonRawContext(context.Background(), id)
}
//LookupPerson gets person data. (struct data)
func (c *Client) LookupPerson(id int) (*Person, error) {
return c.LookupPersonContext(context.Background(), id)
}
//LookupPersonRawContext gets person data with context.Context. (raw data)
func (c *Client) LookupPersonRawContext(ctx context.Context, id int) ([]byte, error) {
return c.get(ctx, c.makeLookupCommand(TargetPersons, id))
}
//LookupPersonContext gets person data with context.Context. (struct data)
func (c *Client) LookupPersonContext(ctx context.Context, id int) (*Person, error) {
b, err := c.LookupPersonRawContext(ctx, id)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodePerson(b)
}
//LookupWorkerRaw gets worker data. (raw data)
func (c *Client) LookupWorkerRaw(id int) ([]byte, error) {
return c.LookupWorkerRawContext(context.Background(), id)
}
//LookupWorker gets worker data. (struct data)
func (c *Client) LookupWorker(id int) (*Worker, error) {
return c.LookupWorkerContext(context.Background(), id)
}
//LookupWorkerRawContext gets worker data with context.Context. (raw data)
func (c *Client) LookupWorkerRawContext(ctx context.Context, id int) ([]byte, error) {
return c.get(ctx, c.makeLookupCommand(TargetWorkers, id))
}
//LookupWorkerContext gets worker data with context.Context. (struct data)
func (c *Client) LookupWorkerContext(ctx context.Context, id int) (*Worker, error) {
b, err := c.LookupWorkerRawContext(ctx, id)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodeWorker(b)
}
//RankingRaw gets ranking data (raw data)
func (c *Client) RankingRaw(tm time.Time) ([]byte, error) {
return c.RankingRawContext(context.Background(), tm)
}
//Ranking gets ranking data (struct data)
func (c *Client) Ranking(tm time.Time) (Ranking, error) {
return c.RankingContext(context.Background(), tm)
}
//RankingRawContext gets ranking data (raw data)
func (c *Client) RankingRawContext(ctx context.Context, tm time.Time) ([]byte, error) {
return c.get(ctx, c.makeRankingCommand(tm))
}
//RankingContext gets ranking data (struct data)
func (c *Client) RankingContext(ctx context.Context, tm time.Time) (Ranking, error) {
b, err := c.RankingRawContext(ctx, tm)
if err != nil {
return nil, errs.Wrap(err)
}
return DecodeRanking(b)
}
func (c *Client) makeSearchCommand(t Target, v url.Values) *url.URL {
var u *url.URL
if c == nil {
u = New().URL()
} else {
u = c.server.URL()
}
u.Path = fmt.Sprintf("/%v/%v", c.apiDir(), t)
u.RawQuery = v.Encode()
return u
}
func (c *Client) makeLookupCommand(t Target, id int) *url.URL {
var u *url.URL
if c == nil {
u = New().URL()
} else {
u = c.server.URL()
}
u.Path = fmt.Sprintf("/%v/%v/%v", c.apiDir(), t, strconv.Itoa(id))
return u
}
func (c *Client) makeCardCommand(id int) *url.URL {
u := c.makeLookupCommand(TargetBooks, id)
u.Path = u.Path + "/card"
return u
}
func (c *Client) makeContentCommand(id int, f Format) *url.URL {
u := c.makeLookupCommand(TargetBooks, id)
u.Path = u.Path + "/content"
u.RawQuery = (url.Values{"format": {f.String()}}).Encode()
return u
}
func (c *Client) makeRankingCommand(tm time.Time) *url.URL {
var u *url.URL
if c == nil {
u = New().URL()
} else {
u = c.server.URL()
}
u.Path = fmt.Sprintf("/%v/%v/%v/%v", c.apiDir(), TargetRanking, "xhtml", tm.Format("2006/01"))
return u
}
func (c *Client) apiDir() string {
return defaultAPIDir
}
func (c *Client) get(ctx context.Context, u *url.URL) ([]byte, error) {
if c == nil {
return nil, errs.Wrap(ErrNullPointer)
}
resp, err := c.client.Get(u, fetch.WithContext(ctx))
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("url", u.String()))
}
return resp.DumpBodyAndClose()
}
/* Copyright 2019-2021 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/