-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducts.go
354 lines (292 loc) · 11.6 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
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2023 J&J Ideenschmiede GmbH. All rights reserved.
package goshopify
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
// ProductBody is to structure the product data
type ProductBody struct {
Product ProductBodyProduct `json:"product"`
}
type ProductBodyProduct struct {
Id int `json:"id,omitempty"`
Title string `json:"title,omitempty"`
BodyHtml string `json:"body_html"`
Vendor string `json:"vendor"`
ProductType string `json:"product_type,omitempty"`
Status string `json:"status,omitempty"`
Tags string `json:"tags,omitempty"`
Images []ProductBodyImages `json:"images,omitempty"`
Variants []ProductBodyVariants `json:"variants,omitempty"`
Options []ProductBodyOptions `json:"options,omitempty"`
Metafields []ProductBodyMetafields `json:"metafields,omitempty"`
MetafieldsGlobalTitleTag string `json:"metafields_global_title_tag,omitempty"`
MetafieldsGlobalDescriptionTag string `json:"metafields_global_description_tag,omitempty"`
}
type ProductBodyImages struct {
Position int `json:"position,omitempty"`
Src string `json:"src"`
Alt string `json:"alt,omitempty"`
}
type ProductBodyVariants struct {
Title string `json:"title,omitempty"`
Price string `json:"price"`
Sku string `json:"sku,omitempty"`
CompareAtPrice string `json:"compare_at_price"`
FulfillmentService string `json:"fulfillment_service,omitempty"`
InventoryManagement string `json:"inventory_management,omitempty"`
InventoryPolicy string `json:"inventory_policy,omitempty"`
Option1 string `json:"option1,omitempty"`
Option2 string `json:"option2,omitempty"`
Option3 string `json:"option3,omitempty"`
Taxable bool `json:"taxable"`
Barcode string `json:"barcode"`
Grams int `json:"grams"`
Weight float64 `json:"weight"`
WeightUnit string `json:"weight_unit,omitempty"`
InventoryItemId int `json:"inventory_item_id,omitempty"`
InventoryQuantity int `json:"inventory_quantity,omitempty"`
RequiresShipping bool `json:"requires_shipping,omitempty"`
Metafields []ProductBodyVariantsMetafields `json:"metafields,omitempty"`
}
type ProductBodyVariantsMetafields struct {
Id int `json:"id,omitempty"`
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
Namespace string `json:"namespace"`
}
type ProductBodyOptions struct {
Name string `json:"name,omitempty"`
Values []string `json:"values,omitempty"`
}
type ProductBodyMetafields struct {
Id int `json:"id,omitempty"`
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
Namespace string `json:"namespace"`
}
// ProductReturn is to decode the json return
type ProductReturn struct {
Product ProductReturnProduct `json:"product"`
Errors interface{} `json:"errors,omitempty"`
}
type ProductReturnProduct struct {
Id int `json:"id"`
Title string `json:"title"`
BodyHtml string `json:"body_html"`
Vendor string `json:"vendor"`
ProductType string `json:"product_type"`
CreatedAt string `json:"created_at"`
Handle string `json:"handle"`
UpdatedAt string `json:"updated_at"`
PublishedAt string `json:"published_at"`
TemplateSuffix string `json:"template_suffix"`
Status string `json:"status"`
PublishedScope string `json:"published_scope"`
Tags string `json:"tags"`
AdminGraphqlApiId string `json:"admin_graphql_api_id"`
Variants []ProductReturnVariants `json:"variants"`
Options []ProductReturnOptions `json:"options"`
Images []ProductReturnImages `json:"images"`
Image ProductReturnImage `json:"image"`
}
type ProductReturnVariants struct {
Id int `json:"id"`
ProductId int `json:"product_id"`
Title string `json:"title"`
Price string `json:"price"`
Sku string `json:"sku"`
Position int `json:"position"`
InventoryPolicy string `json:"inventory_policy"`
CompareAtPrice string `json:"compare_at_price"`
FulfillmentService string `json:"fulfillment_service"`
InventoryManagement string `json:"inventory_management"`
Option1 string `json:"option1"`
Option2 string `json:"option2"`
Option3 string `json:"option3"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Taxable bool `json:"taxable"`
Barcode string `json:"barcode"`
Grams int `json:"grams"`
ImageId int `json:"image_id"`
Weight float64 `json:"weight"`
WeightUnit string `json:"weight_unit"`
InventoryItemId int `json:"inventory_item_id"`
InventoryQuantity int `json:"inventory_quantity"`
OldInventoryQuantity int `json:"old_inventory_quantity"`
RequiresShipping bool `json:"requires_shipping"`
AdminGraphqlApiId string `json:"admin_graphql_api_id"`
}
type ProductReturnOptions struct {
Id int `json:"id"`
ProductId int `json:"product_id"`
Name string `json:"name"`
Positions int `json:"positions"`
Values []string `json:"values"`
}
type ProductReturnImages struct {
Id int `json:"id"`
ProductId int `json:"product_id"`
Position int `json:"position"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Alt string `json:"alt"`
Width int `json:"width"`
Height int `json:"height"`
Src string `json:"src"`
VariantsIds []interface{} `json:"variants_ids"`
AdminGraphqlApiId string `json:"admin_graphql_api_id"`
}
type ProductReturnImage struct {
Id int `json:"id"`
ProductId int `json:"product_id"`
Position int `json:"position"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Alt string `json:"alt"`
Width int `json:"width"`
Height int `json:"height"`
Src string `json:"src"`
VariantsIds []interface{} `json:"variants_ids"`
AdminGraphqlApiId string `json:"admin_graphql_api_id"`
}
// ProductMetafieldsReturn is to decode the json return
type ProductMetafieldsReturn struct {
Metafields []struct {
Id int `json:"id"`
Namespace string `json:"namespace"`
Key string `json:"key"`
Value string `json:"value"`
Description interface{} `json:"description"`
OwnerId int `json:"owner_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
OwnerResource string `json:"owner_resource"`
Type string `json:"type"`
AdminGraphqlApiId string `json:"admin_graphql_api_id"`
} `json:"metafields"`
Errors interface{} `json:"errors,omitempty"`
}
// Product is to get a product via id
func Product(id int, r Request) (ProductReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/products/%d.json", id), http.MethodGet, nil}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductReturn{}, err
}
// Return data
return decode, err
}
// AddProduct is to create a new single or variant product
func AddProduct(body ProductBody, r Request) (ProductReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductReturn{}, err
}
// Set config for new request
c := Config{"/products.json", http.MethodPost, convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductReturn{}, err
}
// Return data
return decode, err
}
// UpdateProduct is to create a new single or variant product
func UpdateProduct(body ProductBody, r Request) (ProductReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/products/%d.json", body.Product.Id), http.MethodPut, convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductReturn{}, err
}
// Return data
return decode, err
}
// DeleteProduct is to remove a product form shop
func DeleteProduct(id int, r Request) error {
// Set config for new request
c := Config{fmt.Sprintf("/products/%d.json", id), http.MethodDelete, nil}
// Send request
response, err := c.Send(r)
if err != nil {
return err
}
// Close request
defer response.Body.Close()
// Return nothing
return nil
}
// ProductMetafields is to get a list of all product variant metafields
func ProductMetafields(id int, r Request) (ProductMetafieldsReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/products/%d/metafields.json", id), http.MethodGet, nil}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductMetafieldsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductMetafieldsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductMetafieldsReturn{}, err
}
// Return data
return decode, err
}
// DeleteProductMetafield is to remove a metafield from a product
func DeleteProductMetafield(productId, metafieldId int, r Request) error {
// Set config for new request
c := Config{fmt.Sprintf("/products/%d/metafields/%d.json", productId, metafieldId), http.MethodDelete, nil}
// Send request
response, err := c.Send(r)
if err != nil {
return err
}
// Close request
defer response.Body.Close()
// Return nothing
return nil
}