-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproducts.go
220 lines (200 loc) · 6.05 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
package go_printify
import (
"fmt"
"net/http"
"time"
)
const (
productsPath = "shops/%d/products.json"
productPath = "shops/%d/products/%d.json"
publishProductPath = "shops/%d/products/%d/publish.json"
publishSuccessPath = "shops/%d/products/%d/publishing_succeeded.json"
publishFailedPath = "shops/%d/products/%d/publishing_failed.json"
unpublishPath = "shops/%d/products/%d/unpublish.json"
)
type Product struct {
Id *int `json:"id,omitempty"`
Title string `json:"title"`
Description string `json:"description"`
Tags []string `json:"tags"`
Options []map[string]interface{} `json:"options"`
Variants []ProductVariant `json:"variants"`
Images []ProductMockUpImage `json:"images"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Visible bool `json:"visible"`
BlueprintId int `json:"blueprint_id"`
PrintProviderId int `json:"print_provider_id"`
UserId int `json:"user_id"`
ShopId int `json:"shop_id"`
PrintAreas []*PrintArea `json:"print_areas"`
PrintDetails *PrintDetails `json:"print_details"`
External []*External `json:"external"`
IsLocked bool `json:"is_locked"`
SalesChannelProperties []string `json:"sales_channel_properties"`
}
type ProductVariant struct {
Id *int `json:"id"`
Sku string `json:"sku"`
Price float32 `json:"price"`
Cost float32 `json:"cost"`
Title string `json:"title"`
Grams int `json:"grams"`
IsEnabled bool `json:"is_enabled"`
InStock bool `json:"in_stock"` // Deprecated
IsDefault bool `json:"is_default"`
IsAvailable bool `json:"is_available"`
Options []int `json:"options"`
}
type ProductMockUpImage struct {
Src string `json:"src"`
VariantIds int `json:"variant_ids"`
Position string `json:"position"`
IsDefault bool `json:"is_default"`
}
type ProducePlaceholder struct {
Position string `json:"position"`
Images []ProductImage `json:"images"`
}
type ProductImage struct {
Id int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Height int `json:"height"`
Width int `json:"width"`
X int `json:"x"`
Y int `json:"y"`
Scale int `json:"scale"`
Angle int `json:"angle"`
}
type PrintArea struct {
VariantIds []int `json:"variant_ids"`
Placeholders []ProducePlaceholder `json:"placeholders"`
}
type PrintDetails struct {
PrintOnSide string `json:"print_on_side"`
}
type PublishingProperties struct {
Images bool `json:"images"`
Variants bool `json:"variants"`
Title bool `json:"title"`
Description bool `json:"description"`
Tags bool `json:"tags"`
}
type External struct {
Id int `json:"id"`
Handle string `json:"handle"`
}
/*
Retrieve a list of products
*/
func (c *Client) GetProducts(shopId int, page *int) ([]*Product, error) {
path := fmt.Sprintf(productsPath, shopId)
if page != nil {
path = fmt.Sprintf("%s?page=%d", path, &page)
}
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
products := make([]*Product, 0)
_, err = c.do(req, &products)
return products, err
}
/*
Retrieve a product
*/
func (c *Client) GetProduct(shopId, productId int) (*Product, error) {
path := fmt.Sprintf(productPath, shopId, productId)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
product := &Product{}
_, err = c.do(req, product)
return product, err
}
/*
Create a new product
*/
func (c *Client) CreateProduct(product Product) error {
req, err := c.newRequest(http.MethodPost, productsPath, product)
if err != nil {
return err
}
_, err = c.do(req, product)
return err
}
/*
Update a product
*/
func (c *Client) UpdateProduct(shopId int, product Product) (*Product, error) {
path := fmt.Sprintf(productPath, shopId, product.Id)
req, err := c.newRequest(http.MethodPut, path, product)
if err != nil {
return nil, err
}
updatedProduct := &Product{}
_, err = c.do(req, updatedProduct)
return updatedProduct, err
}
/*
Delete a product
*/
func (c *Client) DeleteProduct(shopId int, productId int) error {
path := fmt.Sprintf(productPath, shopId, productId)
req, err := c.newRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
_, err = c.do(req, nil)
return err
}
/*
Publish a product
*/
func (c *Client) PublishProduct(shopId, productId int, publishProperties PublishingProperties) error {
path := fmt.Sprintf(publishProductPath, shopId, productId)
req, err := c.newRequest(http.MethodPost, path, publishProperties)
if err != nil {
return err
}
_, err = c.do(req, nil)
return err
}
/*
Set product publish status to succeeded
*/
func (c *Client) SetProductPublishSuccess(shopId, productId int, external External) error {
path := fmt.Sprintf(publishSuccessPath, shopId, productId)
req, err := c.newRequest(http.MethodPost, path, external)
if err != nil {
return err
}
_, err = c.do(req, nil)
return err
}
/*
Set product publish status to failed
*/
func (c *Client) SetProductPublishFailre(shopId, productId int, reason string) error {
path := fmt.Sprintf(publishFailedPath, shopId, productId)
req, err := c.newRequest(http.MethodPost, path, map[string]string{"reason": reason})
if err != nil {
return err
}
_, err = c.do(req, nil)
return err
}
/*
Notify that a product has been unpublished
*/
func (c *Client) UnPublish(shopId, productId int) error {
path := fmt.Sprintf(unpublishPath, shopId, productId)
req, err := c.newRequest(http.MethodPost, path, nil)
if err != nil {
return err
}
_, err = c.do(req, nil)
return err
}