-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.go
342 lines (298 loc) · 10.3 KB
/
products.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// product
package prom
import (
"fmt"
"net/url"
"strconv"
)
const (
ProductSellingTypeRetail = "retail"
ProductSellingTypeWholesale = "wholesale"
ProductSellingTypeUniversal = "universal"
ProductSellingTypeService = "service"
ProductPresenceAvailable = "available"
ProductPresenceNotAvailable = "not_available"
ProductPresenceOrder = "order"
ProductPresenceService = "service"
ProductPresenceWaiting = "waiting"
ProductStatusOnDisplay = "on_display"
ProductStatusDraft = "draft"
ProductStatusDeleted = "deleted"
ProductStatusNotOnDisplay = "not_on_display"
ProductStatusEditingRequired = "editing_required"
ProductStatusApprovalPending = "approval_pending"
ProductStatusDeletedByModerator = "deleted_by_moderator"
ProductDiscountTypeAmount = "amount"
ProductDiscountTypePercent = "percent"
ProductMarkMissingProductAsNone = "none"
ProductMarkMissingProductAsNotAvailable = "not_available"
ProductMarkMissingProductAsNotOnDisplay = "not_on_display"
ProductMarkMissingProductAsDeleted = "deleted"
ProductUpdatedFieldsName = "name"
ProductUpdatedFieldsSku = "sku"
ProductUpdatedFieldsPrice = "price"
ProductUpdatedFieldsImagesUrls = "images_urls"
ProductUpdatedFieldsPresence = "presence"
ProductUpdatedFieldsQuantityInStock = "quantity_in_stock"
ProductUpdatedFieldsDescription = "description"
ProductUpdatedFieldsGroup = "group"
ProductUpdatedFieldsKeywords = "keywords"
ProductUpdatedFieldsAttributes = "attributes"
ProductUpdatedFieldsDiscount = "discount"
ProductUpdatedFieldsLabels = "labels"
ProductUpdatedFieldsGtin = "gtin"
ProductUpdatedFieldsMpn = "mpn"
ProductImportRequestStatusSuccess = "success"
ProductImportRequestStatusError = "error"
ProductImportResultStatusSuccess = "SUCCESS"
ProductImportResultStatusPartial = "PARTIAL"
ProductImportResultStatusFatal = "FATAL"
)
type ProductPrice struct {
Price float64 `json:"price"`
MinimumOrderQuantity float64 `json:"minimum_order_quantity"`
}
type Product struct {
Id int `json:"id"`
ExternalId string `json:"external_id"`
Name string `json:"name"`
Sku string `json:"sku"`
Keywords string `json:"keywords"`
Description string `json:"description"`
SellingType string `json:"selling_type"`
Presence string `json:"presence"`
PresenceSure bool `json:"presence_sure"`
Price float64 `json:"price"`
MinimumOrderQuantity float64 `json:"minimum_order_quantity"`
Discount *Discount `json:"discount"`
Currency string `json:"currency"`
Group struct {
Id int `json:"id"`
Name string `json:"string"`
} `json:"group"`
Category struct {
Id int `json:"id"`
Caption string `json:"caption"`
} `json:"category"`
Prices []ProductPrice `json:"prices,omitempty"`
MainImage string `json:"main_image"`
Images []struct {
Url string `json:"url"`
ThumbnailUrl string `json:"thumbnail_url"`
Id int `json:"id"`
} `json:"images"`
Status string `json:"status"`
}
type Discount struct {
Value float64 `json:"value"`
Type string `json:"type"`
DateStart string `json:"date_start"`
DateEnd string `json:"date_end"`
}
type ProductsRequest struct {
Limit int
LastId int
GroupId int
}
type ProductsResponse struct {
Products []Product `json:"products"`
Error string `json:"error"`
}
type ProductResponse struct {
Product Product `json:"product"`
Error string `json:"error"`
}
type ProductEdit struct {
Id int `json:"id"`
Presence string `json:"presence,omitempty"`
PresenceSure bool `json:"presence_sure,omitempty"`
Price float64 `json:"price,omitempty"`
Status string `json:"status,omitempty"`
Prices []ProductPrice `json:"prices,omitempty"`
Discount *Discount `json:"discount,omitempty"`
Name string `json:"name,omitempty"`
Keywords string `json:"keywords,omitempty"`
Description string `json:"description,omitempty"`
}
type ProductEditByExternalId struct {
Id string `json:"id"`
Presence string `json:"presence"`
PresenceSure bool `json:"presence_sure"`
Price float64 `json:"price"`
Status string `json:"status"`
Prices []ProductPrice `json:"prices,omitempty"`
Discount *Discount `json:"discount"`
Name string `json:"name"`
Keywords string `json:"keywords"`
Description string `json:"description"`
QuantityInStock int `json:"quantity_in_stock"`
}
type ProductEditResponse struct {
ProcessedIds []int `json:"processed_ids"`
Errors map[string]interface{} `json:"errors"`
Error string `json:"error"`
}
type ImportProductURL struct {
Url string `json:"url"`
ForceUpdate bool `json:"force_update"`
OnlyAvailable bool `json:"only_available"`
MarkMissingProductAs string `json:"mark_missing_product_as"`
UpdatedFields []string `json:"updated_fields"`
}
type ImportProductWithFile struct {
File string `json:"file"`
Data struct {
ForceUpdate bool `json:"force_update"`
OnlyAvailable bool `json:"only_available"`
MarkMissingProductAs string `json:"mark_missing_product_as"`
UpdatedFields []string `json:"updated_fields"`
}
}
type ProductImportResponse struct {
Id string `json:"id"`
Status string `json:"status"`
Message string `json:"message"`
}
type ImportProductStatus struct {
Status string `json:"status"`
NotChanged int `json:"not_changed"`
Updated int `json:"updated"`
NotInFile int `json:"not_in_fle"`
Imported int `json:"imported"`
Created int `json:"created"`
Actualized int `json:"actualized"`
CreatedActive int `json:"created_active"`
CreatedHidden int `json:"created_hidden"`
Total int `json:"total"`
WithErrorsCount int `json:"with_errors_count"`
Errors []struct {
Download interface{} `json:"download"`
Store_file interface{} `json:"store_file"`
Validation interface{} `json:"validation"`
Import interface{} `json:"import"`
DownloadImages interface{} `json:"download_images"`
} `json:"errors"`
Message string `json:"message"`
}
func NewProductEdit(product Product) (result ProductEdit) {
result = ProductEdit{
Id: product.Id,
Presence: product.Presence,
PresenceSure: product.PresenceSure,
Price: product.Price,
Status: product.Status,
Prices: append(product.Prices[:0:0], product.Prices...),
Name: product.Name,
Keywords: product.Keywords,
Description: product.Description,
}
if product.Discount != nil {
result.Discount = &Discount{
Value: product.Discount.Value,
Type: product.Discount.Type,
DateStart: product.Discount.DateStart,
DateEnd: product.Discount.DateEnd,
}
}
return
}
func (c *Client) GetProducts(request ProductsRequest) ([]Product, error) {
var (
result ProductsResponse
params = make(map[string]string)
)
if request.Limit > MaxLimit {
return nil, fmt.Errorf("ProductsRequest.Limit can't be more than MaxLimit: %d > %d", request.Limit, MaxLimit)
}
if request.GroupId >= 0 {
params["group_id"] = strconv.Itoa(request.GroupId)
}
if request.LastId > 0 {
params["last_id"] = strconv.Itoa(request.LastId)
}
if request.Limit > 0 {
params["limit"] = strconv.Itoa(request.Limit)
}
result = ProductsResponse{}
err := c.Get("/products/list", params, &result)
if err != nil {
return nil, fmt.Errorf("Error when request products: %s", err)
}
return result.Products, nil
}
func (c *Client) GetProduct(id int) (product Product, err error) {
var result ProductResponse
err = c.Get("/products/"+strconv.Itoa(id), nil, result)
if err != nil {
err = fmt.Errorf("Error when request product: %s", err)
return
}
if len(result.Error) > 0 {
err = fmt.Errorf("Error when request product: %s", result.Error)
return
}
product = result.Product
return
}
func (c *Client) GetProductByExternalId(id string) (product Product, err error) {
var result ProductResponse
err = c.Get("/product/by_external_id/"+url.QueryEscape(id), nil, result)
if err != nil {
err = fmt.Errorf("Error when request product: %s", err)
return
}
if len(result.Error) > 0 {
err = fmt.Errorf("Error when request product: %s", result.Error)
return
}
product = result.Product
return
}
func (c *Client) UpdateProducts(products []ProductEdit) (updatedIds []int, err error) {
var result ProductEditResponse
err = c.Post("/products/edit", products, &result)
if err != nil {
err = fmt.Errorf("Error when update product: %s", err)
return
}
if len(result.Error) > 0 {
err = fmt.Errorf("Error when update product: %s", result.Error)
return
}
if len(result.Errors) > 0 {
err = fmt.Errorf("Error when update product: %#v", result.Errors)
return
}
updatedIds = result.ProcessedIds
return
}
func (c *Client) UpdateProductsByExternalId(products []ProductEditByExternalId) (updatedIds []int, err error) {
var result ProductEditResponse
err = c.Post("/products/edit_by_external_id", products, &result)
if err != nil {
err = fmt.Errorf("Error when update product: %s", err)
return
}
if len(result.Error) > 0 {
err = fmt.Errorf("Error when update product: %s", result.Error)
return
}
if len(result.Errors) > 0 {
err = fmt.Errorf("Error when update product: %#v", result.Errors)
return
}
updatedIds = result.ProcessedIds
return
}
func (c *Client) ImportProductsByUrl(request ImportProductURL) (response ProductImportResponse, err error) {
err = c.Post("/products/import_url", request, &response)
return
}
func (c *Client) ImportProductsByFile(request ImportProductURL) (response ProductImportResponse, err error) {
err = c.Post("/products/import_file", request, &response)
return
}
func (c *Client) GetProductsImportStatus(id int) (response ImportProductStatus, err error) {
err = c.Get("/products/import/status/"+strconv.Itoa(id), nil, &response)
return
}