/
shopee.go
229 lines (207 loc) · 6.77 KB
/
shopee.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
// Package shopee implements interfacing with Shopee.
package shopee
import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/nmcapule/oclz-go/integrations/models"
"github.com/nmcapule/oclz-go/oauth2"
"github.com/nmcapule/oclz-go/utils"
"github.com/nmcapule/oclz-go/utils/scheduler"
log "github.com/sirupsen/logrus"
)
const Vendor = "SHOPEE"
// Config is a Lazada config.
type Config struct {
Domain string `json:"domain"`
ShopID int64 `json:"shop_id"`
PartnerID int64 `json:"partner_id"`
PartnerKey string `json:"partner_key"`
RedirectURI string `json:"redirect_uri"`
}
// Client is a Lazada client.
type Client struct {
*models.BaseTenant
DatabaseTenant *models.BaseDatabaseTenant
Config *Config
Credentials *oauth2.Credentials
}
// CollectAllItems collects and returns all items registered in this client.
func (c *Client) CollectAllItems() ([]*models.Item, error) {
var items []*models.Item
var offset int64
const limit = 50
for {
base, err := c.request(&http.Request{
Method: http.MethodGet,
URL: c.url("/api/v2/product/get_item_list", url.Values{
"offset": []string{strconv.FormatInt(offset, 10)},
"page_size": []string{strconv.FormatInt(limit, 10)},
"item_status": []string{"NORMAL"},
}),
}, signatureMode(signatureModeShopAPI))
if err != nil {
return nil, fmt.Errorf("error response: %v", err)
}
for _, product := range base.Get("response.item").Array() {
parsed, err := c.loadItemsFromProduct(int(product.Get("item_id").Int()))
if err != nil {
return nil, fmt.Errorf("load items from models: %v", err)
}
items = append(items, parsed...)
}
log.WithFields(log.Fields{
"tenant": c.Name,
"items": len(items),
"offset": offset,
"total": base.Get("response.total_count").Int(),
}).Debugln("Loading fresh items")
if !base.Get("response.has_next_page").Bool() {
break
}
offset = base.Get("response.next_offset").Int()
}
return items, nil
}
// LoadItem returns item info for a single SKU. Loading items from the Shopee
// client requires that this item has already been collected beforehand.
func (c *Client) LoadItem(sku string) (*models.Item, error) {
cached, err := c.DatabaseTenant.LoadItem(sku)
if err != nil {
return nil, fmt.Errorf("retrieving db tenant item: %v", err)
}
itemID := cached.TenantProps.Get("item_id").Int()
// Load all products and models associated to this item id.
items, err := c.loadItemsFromProduct(int(itemID))
if err != nil {
return nil, fmt.Errorf("load items from product: %v", err)
}
// Only include items that have exact SKU match from search.
var filtered []*models.Item
for i := range items {
if items[i].SellerSKU == sku {
filtered = append(filtered, items[i])
}
}
items = filtered
if len(items) == 0 {
return nil, models.ErrNotFound
}
if len(items) > 1 {
log.Warningf("Multiple items with same SKU retrieved for %s: %+v", sku, items)
}
return items[0], nil
}
// SaveItem saves item info for a single SKU. This only implements updating
// the product stock. Shopee API documentation:
// https://open.shopee.com/documents/v2/v2.product.update_stock?module=89&type=1
func (c *Client) SaveItem(item *models.Item) error {
_, err := c.request(&http.Request{
Method: http.MethodPost,
URL: c.url("/api/v2/product/update_stock", nil),
Body: io.NopCloser(strings.NewReader(utils.GJSONFrom(map[string]any{
"item_id": item.TenantProps.Get("item_id").Int(),
"stock_list": []map[string]any{{
// Shopee API allows model_id = 0, which means that this SKU
// does not have a model associated to it, and can ignore this
// field safely.
"model_id": item.TenantProps.Get("model_id").Int(),
"seller_stock": []map[string]any{{
"stock": item.Stocks,
}},
}},
}).String())),
}, signatureMode(signatureModeShopAPI))
if err != nil {
return fmt.Errorf("error response: %v", err)
}
// Poll until the update is confirmed propagated to Shopee.
return scheduler.Retry(func() bool {
log.WithFields(log.Fields{
"tenant": c.Name,
"seller_sku": item.SellerSKU,
}).Debugln("Confirming item update...")
live, err := c.LoadItem(item.SellerSKU)
if err != nil {
log.WithFields(log.Fields{
"tenant": c.Name,
"seller_sku": item.SellerSKU,
}).Errorf("Failed to confirm item update: %v", err)
return false
}
return live.Stocks == item.Stocks
}, scheduler.RetryConfig{
RetryWait: time.Second,
RetryLimit: 10,
BackoffMultiply: 2,
})
}
func (c *Client) loadItemsFromProduct(id int) ([]*models.Item, error) {
base, err := c.request(&http.Request{
Method: http.MethodGet,
URL: c.url("/api/v2/product/get_item_base_info", url.Values{
"item_id_list": []string{strconv.Itoa(id)},
}),
}, signatureMode(signatureModeShopAPI))
if err != nil {
return nil, fmt.Errorf("error response: %v", err)
}
var items []*models.Item
for _, item := range base.Get("response.item_list").Array() {
// If model exists, load from models endpoint instead.
if item.Get("has_model").Bool() {
parsed, err := c.loadItemsFromModelOfItemID(id)
if err != nil {
return nil, fmt.Errorf("load from model: %v", err)
}
items = append(items, parsed...)
continue
}
if item.Get("item_sku").String() == "" {
log.Debugf("skipping item %d, empty sku", id)
continue
}
items = append(items, &models.Item{
SellerSKU: item.Get("item_sku").String(),
Stocks: int(item.Get("stock_info_v2.summary_info.total_available_stock").Int()),
TenantProps: utils.GJSONFrom(map[string]any{
"item_id": id,
"current_price": item.Get("price_info.current_price").Float(),
"original_price": item.Get("price_info.original_price").Float(),
"currency": item.Get("price_info.currency").String(),
"item_name": item.Get("item_name").String(),
}),
})
}
return items, nil
}
func (c *Client) loadItemsFromModelOfItemID(itemID int) ([]*models.Item, error) {
base, err := c.request(&http.Request{
Method: http.MethodGet,
URL: c.url("/api/v2/product/get_model_list", url.Values{
"item_id": []string{strconv.Itoa(itemID)},
}),
}, signatureMode(signatureModeShopAPI))
if err != nil {
return nil, fmt.Errorf("error response: %v", err)
}
var items []*models.Item
for _, model := range base.Get("response.model").Array() {
items = append(items, &models.Item{
SellerSKU: model.Get("model_sku").String(),
Stocks: int(model.Get("stock_info_v2.summary_info.total_available_stock").Int()),
TenantProps: utils.GJSONFrom(map[string]any{
"item_id": itemID,
"model_id": model.Get("model_id").Int(),
"current_price": model.Get("price_info.current_price").Float(),
"original_price": model.Get("price_info.original_price").Float(),
"currency": model.Get("price_info.currency").String(),
}),
})
}
return items, nil
}