-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrickset.go
237 lines (210 loc) · 6.22 KB
/
brickset.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
/**
* @Package brickset
* @Time: 2022/2/8 9:27 PM
* @Author: wuhb
* @File: brickset.go
*/
package brickset
import (
"context"
"log"
"net/url"
"path"
"strconv"
"time"
"github.com/lehoqi/brickset/storage"
)
type IBrickAuth interface {
Login(ctx context.Context) (string, error)
CheckUserHash(ctx context.Context, hash string) (bool, error)
CheckKey(ctx context.Context, key string) (bool, error)
}
type IBrickHash interface {
GetHash(ctx context.Context, username string) (string, error)
}
type IBrickSet interface {
GetSets(ctx context.Context, params *GetSetRequest) (int, []*Sets, error)
GetThemes(ctx context.Context) (int, []*Themes, error)
GetReviews(ctx context.Context, setID int) (int, []*Review, error)
GetSubthemes(ctx context.Context, theme string) (int, []*Subthemes, error)
GetInstructions(ctx context.Context, setID int) (int, []*Instruction, error)
GetInstructions2(ctx context.Context, setNumber string) (int, []*Instruction, error)
GetAdditionalImages(ctx context.Context, setID int) (int, []*Image, error)
GetYears(ctx context.Context, theme string) (int, []*Years, error)
}
type IBrickStorage interface {
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
Get(ctx context.Context, key string) (interface{}, error)
}
var Logger = log.Default()
type Option func(conf *config)
type brickSet struct {
conf *config
hash IBrickHash
auth IBrickAuth
client IClient
}
func (b brickSet) GetYears(ctx context.Context, theme string) (int, []*Years, error) {
response := &CommonResponse{}
params := url.Values{}
params.Set("apiKey", b.conf.apiKey)
params.Set("theme", theme)
err := b.client.GetJSON(ctx, getYearsURL, params, response)
if err != nil {
return 0, nil, err
}
return response.Matches, response.Years, response.Error()
}
func (b brickSet) GetAdditionalImages(ctx context.Context, setID int) (int, []*Image, error) {
response := &CommonResponse{}
params := url.Values{}
params.Set("apiKey", b.conf.apiKey)
params.Set("setID", strconv.Itoa(setID))
err := b.client.GetJSON(ctx, getAdditionalImagesURL, params, response)
if err != nil {
return 0, nil, err
}
if b.conf.imagePath != nil && response.AdditionalImages != nil {
for _, image := range response.AdditionalImages {
err := image.Download(b.conf.imagePath)
if err != nil {
Logger.Printf("download image error:%v", err)
}
}
}
return response.Matches, response.AdditionalImages, response.Error()
}
func (b brickSet) GetInstructions(ctx context.Context, setID int) (int, []*Instruction, error) {
response := &CommonResponse{}
params := url.Values{}
params.Set("apiKey", b.conf.apiKey)
params.Set("setID", strconv.Itoa(setID))
err := b.client.GetJSON(ctx, getInstructionsURL, params, response)
if err != nil {
return 0, nil, err
}
return response.Matches, response.Instructions, response.Error()
}
func (b brickSet) GetInstructions2(ctx context.Context, setNumber string) (int, []*Instruction, error) {
response := &CommonResponse{}
params := url.Values{}
params.Set("apiKey", b.conf.apiKey)
params.Set("setNumber", setNumber)
err := b.client.GetJSON(ctx, getInstructions2URL, params, response)
if err != nil {
return 0, nil, err
}
return response.Matches, response.Instructions, response.Error()
}
func (b brickSet) GetSubthemes(ctx context.Context, theme string) (int, []*Subthemes, error) {
response := &CommonResponse{}
params := url.Values{}
params.Add("apiKey", b.conf.apiKey)
params.Set("theme", theme)
err := b.client.GetJSON(ctx, getSubthemesURL, params, response)
if err != nil {
return 0, nil, err
}
return response.Matches, response.Subthemes, response.Error()
}
func (b brickSet) GetThemes(ctx context.Context) (int, []*Themes, error) {
response := &CommonResponse{}
request := url.Values{}
request.Set("apiKey", b.conf.apiKey)
err := b.client.GetJSON(ctx, getThemesURL, request, response)
if err != nil {
return 0, nil, err
}
return response.Matches, response.Themes, response.Error()
}
func (b brickSet) GetReviews(ctx context.Context, setID int) (int, []*Review, error) {
response := &CommonResponse{}
request := url.Values{}
request.Set("apiKey", b.conf.apiKey)
request.Set("setID", strconv.Itoa(setID))
err := b.client.GetJSON(ctx, getReviewsURL, request, response)
if err != nil {
return 0, nil, err
}
return response.Matches, response.Reviews, response.Error()
}
func (b brickSet) GetSets(ctx context.Context, params *GetSetRequest) (int, []*Sets, error) {
response := &CommonResponse{}
request := url.Values{}
request.Set("params", params.JSON())
request.Set("apiKey", b.conf.apiKey)
hash, err := b.hash.GetHash(ctx, b.conf.username)
if err != nil {
return 0, nil, err
}
request.Set("userHash", hash)
err = b.client.GetJSON(ctx, getSetsURL, request, response)
if err != nil {
return 0, nil, err
}
if b.conf.imagePath != nil {
for _, set := range response.Sets {
err := set.Image.Download(b.conf.imagePath)
if err != nil {
Logger.Printf("download image error:%v", err)
}
}
}
return response.Matches, response.Sets, response.Error()
}
func New(apiKey, username, password string, opts ...Option) IBrickSet {
conf := &config{
apiKey: apiKey,
hashExpires: defaultHashExpires,
username: username,
password: password,
}
for _, opt := range opts {
opt(conf)
}
if conf.storage == nil {
conf.storage = storage.NewMemory()
}
c := NewClient(baseURL, conf.debug)
a := NewAuth(conf.apiKey, conf.username, conf.password, c)
h := NewHash(a, conf.storage, conf.hashExpires)
return &brickSet{
conf: conf,
hash: h,
auth: a,
client: c,
}
}
func WithAuth(username, password string) Option {
return func(conf *config) {
conf.username = username
conf.password = password
}
}
func WithDebug(debug bool) Option {
return func(conf *config) {
conf.debug = debug
}
}
func WithHashExpires(expires time.Duration) Option {
return func(conf *config) {
conf.hashExpires = expires
}
}
func WithImagePath(basePath string, prefix string) Option {
return func(conf *config) {
if !path.IsAbs(basePath) {
Logger.Fatal("path is not absolute")
return
}
conf.imagePath = &imagePath{
base: basePath,
prefix: prefix,
}
}
}
func WithStorage(s IBrickStorage) Option {
return func(conf *config) {
conf.storage = s
}
}