/
api_box.go
100 lines (88 loc) · 3.35 KB
/
api_box.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
package cex
import (
"encoding/json"
"github.com/dyninc/qstring"
)
// Box represents an individual product
type Box struct {
BoxID string `json:"boxId"`
BoxName string `json:"boxName"`
BoxDescription string `json:"boxDescription"`
IsMasterBox int `json:"isMasterBox"`
CategoryID int `json:"categoryId"`
CategoryName string `json:"categoryName"`
CategoryFriendlyName string `json:"categoryFriendlyName"`
SuperCatID int `json:"superCatId"`
SuperCatName string `json:"superCatName"`
SuperCatFriendlyName string `json:"superCatFriendlyName"`
ImageURLs ImageURLs `json:"imageUrls"`
CannotBuy int `json:"cannotBuy"`
IsNewBox int `json:"isNewBox"`
SellPrice float64 `json:"sellPrice"`
CashPrice float64 `json:"cashPrice"`
ExchangePrice float64 `json:"exchangePrice"`
FirstPrice int `json:"firstPrice"`
PreviousPrice int `json:"previousPrice"`
LastPriceUpdatedDate string `json:"lastPriceUpdatedDate"`
BoxRating float64 `json:"boxRating"`
CollectionQuantity int `json:"collectionQuantity"`
EcomQuantityOnHand int `json:"ecomQuantityOnHand"`
OutOfStock int `json:"outOfStock"`
OutOfEcomStock int `json:"outOfEcomStock"`
}
// ImageURLs contains a simple set of image URLs
type ImageURLs struct {
Large string `json:"large"`
Medium string `json:"medium"`
Small string `json:"small"`
}
// SortBy defines the different sort types
type SortBy string
// The different sort types
var (
SortByMostPopular SortBy = "relevance" //
SortBySellPrice SortBy = "sellprice" //
SortByName SortBy = "boxname" //
SortByRating SortBy = "rating" //
)
// BoxesParams represents the URL query parameters for the Boxes endpoint
type BoxesParams struct {
// One of these must be present
SuperCatIDs []int `qstring:"superCatIds"` // super category ID
CategoryIDs []int `qstring:"categoryIds"` // category ID
BoxID string `qstring:"q"` // box ID
// These are all optional
FirstRecord int `qstring:"firstRecord,omitempty"` // default: 50, basically a database OFFSET
Count int `qstring:"count,omitempty"` // default: 1, and a database LIMIT
SortBy string `qstring:"sortBy,omitempty"` // default: relevance, attribute to sort by
SortOrder string `qstring:"sortOrder,omitempty"` // default: desc, sort order
}
// Boxes returns product boxes from the given search parameters
func (client *Client) Boxes(params BoxesParams) (result []Box, total int, err error) {
q, err := qstring.Marshal(¶ms)
if err != nil {
return
}
// override the ID fields because they use weird JSON array syntax
if len(params.SuperCatIDs) > 0 {
var superCatIDList []byte
superCatIDList, err = json.Marshal(params.SuperCatIDs)
if err != nil {
return nil, 0, err
}
q.Set("superCatIds", string(superCatIDList))
}
if len(params.CategoryIDs) > 0 {
var categoryIDList []byte
categoryIDList, err = json.Marshal(params.CategoryIDs)
if err != nil {
return nil, 0, err
}
q.Set("categoryIds", string(categoryIDList))
}
payload, err := client.request("boxes", q)
if err != nil {
return
}
return payload.Data.Boxes, payload.Data.TotalRecords, nil
}