forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
192 lines (165 loc) · 7.5 KB
/
product.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
package goshopify
import (
"fmt"
"regexp"
"time"
)
const productsBasePath = "products"
const productsResourceName = "products"
// linkRegex is used to extract pagination links from product search results.
var linkRegex = regexp.MustCompile(`^ *<([^>]+)>; rel="(previous|next)" *$`)
// ProductService is an interface for interfacing with the product endpoints
// of the Shopify API.
// See: https://help.shopify.com/api/reference/product
type ProductService interface {
List(interface{}) ([]Product, error)
ListWithPagination(interface{}) ([]Product, *Pagination, error)
Count(interface{}) (int, error)
Get(int64, interface{}) (*Product, error)
Create(Product) (*Product, error)
Update(Product) (*Product, error)
Delete(int64) error
// MetafieldsService used for Product resource to communicate with Metafields resource
MetafieldsService
}
// ProductServiceOp handles communication with the product related methods of
// the Shopify API.
type ProductServiceOp struct {
client *Client
}
// Product represents a Shopify product
type Product struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
BodyHTML string `json:"body_html,omitempty"`
Vendor string `json:"vendor,omitempty"`
ProductType string `json:"product_type,omitempty"`
Handle string `json:"handle,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
PublishedAt *time.Time `json:"published_at,omitempty"`
PublishedScope string `json:"published_scope,omitempty"`
Tags string `json:"tags,omitempty"`
Status string `json:"status,omitempty"`
Options []ProductOption `json:"options,omitempty"`
Variants []Variant `json:"variants,omitempty"`
Image Image `json:"image,omitempty"`
Images []Image `json:"images,omitempty"`
TemplateSuffix string `json:"template_suffix,omitempty"`
MetafieldsGlobalTitleTag string `json:"metafields_global_title_tag,omitempty"`
MetafieldsGlobalDescriptionTag string `json:"metafields_global_description_tag,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
}
// The options provided by Shopify
type ProductOption struct {
ID int64 `json:"id,omitempty"`
ProductID int64 `json:"product_id,omitempty"`
Name string `json:"name,omitempty"`
Position int `json:"position,omitempty"`
Values []string `json:"values,omitempty"`
}
type ProductListOptions struct {
ListOptions
CollectionID int64 `url:"collection_id,omitempty"`
ProductType string `url:"product_type,omitempty"`
Vendor string `url:"vendor,omitempty"`
Handle string `url:"handle,omitempty"`
PublishedAtMin time.Time `url:"published_at_min,omitempty"`
PublishedAtMax time.Time `url:"published_at_max,omitempty"`
PublishedStatus string `url:"published_status,omitempty"`
PresentmentCurrencies string `url:"presentment_currencies,omitempty"`
}
// Represents the result from the products/X.json endpoint
type ProductResource struct {
Product *Product `json:"product"`
}
// Represents the result from the products.json endpoint
type ProductsResource struct {
Products []Product `json:"products"`
}
// Pagination of results
type Pagination struct {
NextPageOptions *ListOptions
PreviousPageOptions *ListOptions
}
// List products
func (s *ProductServiceOp) List(options interface{}) ([]Product, error) {
products, _, err := s.ListWithPagination(options)
if err != nil {
return nil, err
}
return products, nil
}
// ListWithPagination lists products and return pagination to retrieve next/previous results.
func (s *ProductServiceOp) ListWithPagination(options interface{}) ([]Product, *Pagination, error) {
path := fmt.Sprintf("%s.json", productsBasePath)
resource := new(ProductsResource)
pagination, err := s.client.ListWithPagination(path, resource, options)
if err != nil {
return nil, nil, err
}
return resource.Products, pagination, nil
}
// Count products
func (s *ProductServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", productsBasePath)
return s.client.Count(path, options)
}
// Get individual product
func (s *ProductServiceOp) Get(productID int64, options interface{}) (*Product, error) {
path := fmt.Sprintf("%s/%d.json", productsBasePath, productID)
resource := new(ProductResource)
err := s.client.Get(path, resource, options)
return resource.Product, err
}
// Create a new product
func (s *ProductServiceOp) Create(product Product) (*Product, error) {
path := fmt.Sprintf("%s.json", productsBasePath)
wrappedData := ProductResource{Product: &product}
resource := new(ProductResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Product, err
}
// Update an existing product
func (s *ProductServiceOp) Update(product Product) (*Product, error) {
path := fmt.Sprintf("%s/%d.json", productsBasePath, product.ID)
wrappedData := ProductResource{Product: &product}
resource := new(ProductResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Product, err
}
// Delete an existing product
func (s *ProductServiceOp) Delete(productID int64) error {
return s.client.Delete(fmt.Sprintf("%s/%d.json", productsBasePath, productID))
}
// ListMetafields for a product
func (s *ProductServiceOp) ListMetafields(productID int64, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: productsResourceName, resourceID: productID}
return metafieldService.List(options)
}
// Count metafields for a product
func (s *ProductServiceOp) CountMetafields(productID int64, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: productsResourceName, resourceID: productID}
return metafieldService.Count(options)
}
// GetMetafield for a product
func (s *ProductServiceOp) GetMetafield(productID int64, metafieldID int64, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: productsResourceName, resourceID: productID}
return metafieldService.Get(metafieldID, options)
}
// CreateMetafield for a product
func (s *ProductServiceOp) CreateMetafield(productID int64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: productsResourceName, resourceID: productID}
return metafieldService.Create(metafield)
}
// UpdateMetafield for a product
func (s *ProductServiceOp) UpdateMetafield(productID int64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: productsResourceName, resourceID: productID}
return metafieldService.Update(metafield)
}
// DeleteMetafield for a product
func (s *ProductServiceOp) DeleteMetafield(productID int64, metafieldID int64) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: productsResourceName, resourceID: productID}
return metafieldService.Delete(metafieldID)
}