-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbricklink.go
206 lines (168 loc) · 4.82 KB
/
bricklink.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
package bricklinkapi
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
brickLinkAPIBaseURL = "https://api.bricklink.com/api/store/v1"
oauthVersion = "1.0"
oauthSignatureMethod = "HMAC-SHA1"
)
var (
itemTypes = []string{"MINIFIG", "PART", "SET", "BOOK", "GEAR", "CATALOG", "INSTRUCTION", "UNSORTED_LOT", "ORIGINAL_BOX"}
)
// Bricklink is the main handler for the Bricklink API requests
type Bricklink struct {
ConsumerKey string
ConsumerSecret string
Token string
TokenSecret string
request RequestHandler
}
// New returns a Bricklink handler ready to use
func New(consumerKey, consumerSecret, token, tokenSecret string) *Bricklink {
bl := &Bricklink{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
Token: token,
TokenSecret: tokenSecret,
request: &request{
consumerKey: consumerKey,
consumerSecret: consumerSecret,
token: token,
tokenSecret: tokenSecret,
},
}
return bl
}
// GetItem issues a GET request to the Bricklink API and querys for the specified item.
func (bl Bricklink) GetItem(itemType, itemNumber string) (response string, err error) {
// validate itemType
err = validateParam(itemType, itemTypes)
if err != nil {
return response, err
}
// validate itemNumber
if itemNumber == "" {
return response, errors.New("itemNumber is not specified")
}
// build uri
uri := "/items/" + itemType + "/" + itemNumber
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// GetItemImage issues a GET request to the Bricklink API and querys for the specified item image.
func (bl Bricklink) GetItemImage(itemType, itemNumber string, colorID int) (response string, err error) {
// validate itemType
err = validateParam(itemType, itemTypes)
if err != nil {
return response, err
}
// validate itemNumber
if itemNumber == "" {
return response, errors.New("itemNumber is not specified")
}
// build uri
uri := "/items/" + itemType + "/" + itemNumber + "/images/" + strconv.Itoa(colorID)
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// GetItemPrice issues a GET request to the Bricklink API and querys for the price of an item.
func (bl Bricklink) GetItemPrice(itemType, itemNumber string, params map[string]string) (response string, err error) {
// validate itemType
err = validateParam(itemType, itemTypes)
if err != nil {
return response, err
}
// validate itemNumber
if itemNumber == "" {
return response, errors.New("itemNumber is not specified")
}
// build uri
uri := "/items/" + itemType + "/" + itemNumber + "/price"
// validate and build params
if len(params) != 0 {
var paramString string
for k, v := range params {
if paramString != "" {
paramString += "&"
}
paramString += k + "=" + v
}
uri += "?" + paramString
}
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// GetColorList issues a GET request to the Bricklink API and querys for a list of all colors.
func (bl Bricklink) GetColorList() (response string, err error) {
// build uri
uri := "/colors"
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// GetColor issues a GET request to the Bricklink API and querys for the specified color.
func (bl Bricklink) GetColor(colorID int) (response string, err error) {
// build uri
uri := "/colors/" + strconv.Itoa(colorID)
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// GetCategoryList issues a GET request to the Bricklink API and querys for a list of all categories.
func (bl Bricklink) GetCategoryList() (response string, err error) {
// build uri
uri := "/categories"
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// GetCategory issues a GET request to the Bricklink API and querys for a specified category.
func (bl Bricklink) GetCategory(categoryID int) (response string, err error) {
// build uri
uri := "/categories/" + strconv.Itoa(categoryID)
body, err := bl.request.Request("GET", uri)
if err != nil {
return response, err
}
return string(body), nil
}
// helper function to validate a param
func validateParam(param string, list []string) (err error) {
// parameter must be set
if param == "" {
return errors.New("param is empty")
}
// param must be valid
if !stringInSlice(param, list) {
return fmt.Errorf("param \"%v\" is not valid", param)
}
return nil
}
// helper function to check if a string is in a slice
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if strings.ToLower(b) == strings.ToLower(a) {
return true
}
}
return false
}