/
lazada.go
179 lines (159 loc) · 4.53 KB
/
lazada.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
// Package lazada implements lazada tenant client.
package lazada
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"
"github.com/tidwall/gjson"
log "github.com/sirupsen/logrus"
)
const Vendor = "LAZADA"
// Config is a Lazada config.
type Config struct {
Domain string `json:"domain"`
AppKey string `json:"app_key"`
AppSecret string `json:"app_secret"`
RedirectURI string `json:"redirect_uri"`
}
// Client is a Lazada client.
type Client struct {
*models.BaseTenant
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 int
const limit = 50
for {
base, err := c.request(&http.Request{
Method: http.MethodGet,
URL: c.url("/products/get", url.Values{
"offset": []string{strconv.Itoa(offset)},
"limit": []string{strconv.Itoa(limit)},
}),
})
if err != nil {
return nil, fmt.Errorf("send request: %v", err)
}
for _, product := range base.Get("data.products").Array() {
items = append(items, parseItemsFromProduct(product)...)
}
log.WithFields(log.Fields{
"tenant": c.Name,
"items": len(items),
"offset": offset,
"total": base.Get("data.total_products").Int(),
}).Debugln("Loading fresh items")
offset += limit
if offset >= int(base.Get("data.total_products").Int()) {
break
}
}
return items, nil
}
// LoadItem returns item info for a single SKU.
func (c *Client) LoadItem(sku string) (*models.Item, error) {
base, err := c.request(&http.Request{
Method: http.MethodGet,
URL: c.url("/product/item/get", url.Values{
"seller_sku": []string{sku},
}),
})
if err != nil {
return nil, fmt.Errorf("send request: %v", err)
}
items := parseItemsFromProduct(base.Get("data"))
if len(items) == 0 {
return nil, models.ErrNotFound
}
// I'm not sure about this :P What if there are multiple items.
return items[0], nil
}
// SaveItem saves item info for a single SKU.
// This only implements updating the product stock.
func (c *Client) SaveItem(item *models.Item) error {
// Compose the payload.
xml := fmt.Sprintf(`
<Request>
<Product>
<Skus>
<Sku>
<ItemId>%d</ItemId>
<SkuId>%d</SkuId>
<SellerSku>%s</SellerSku>
<Quantity>%d</Quantity>
</Sku>
</Skus>
</Product>
</Request>`,
item.TenantProps.Get("item_id").Int(),
item.TenantProps.Get("sku_id").Int(),
item.SellerSKU,
// TODO(nmcapule): Find a better way to resolve this. This depend on
// the syncer always giving the latest state of the item.
item.Stocks+int(item.TenantProps.Get("reserved").Int()))
// Do the actual update.
_, err := c.request(&http.Request{
Method: http.MethodPost,
URL: c.url("/product/price_quantity/update", nil),
Body: io.NopCloser(strings.NewReader(url.Values{
"payload": []string{xml},
}.Encode())),
})
if err != nil {
return fmt.Errorf("send request: %v", err)
}
// Poll until the update is confirmed propagated to Lazada.
return scheduler.Retry(func() bool {
log.WithFields(log.Fields{
"tenant": c.Tenant().Name,
"seller_sku": item.SellerSKU,
}).Debugln("Confirming item update...")
live, err := c.LoadItem(item.SellerSKU)
if err != nil {
log.WithFields(log.Fields{
"tenant": c.Tenant().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 parseItemsFromProduct(product gjson.Result) []*models.Item {
var items []*models.Item
for _, skuRaw := range product.Get("skus").Array() {
sku := gjson.Parse(skuRaw.String())
var totalQuantity int64
for _, n := range sku.Get("multiWarehouseInventories.#.totalQuantity").Array() {
totalQuantity += n.Int()
}
var sellableQuantity = sku.Get("quantity").Int()
items = append(items, &models.Item{
SellerSKU: sku.Get("SellerSku").String(),
Stocks: int(sellableQuantity),
TenantProps: utils.GJSONFrom(map[string]interface{}{
"item_id": product.Get("item_id").Int(),
"sku_id": sku.Get("SkuId").Int(),
"shop_sku": sku.Get("ShopSku").String(),
"price": sku.Get("price").Float(),
"reserved": totalQuantity - sellableQuantity,
}),
})
}
return items
}