forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
154 lines (133 loc) · 6.33 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
package goshopify
import (
"fmt"
"time"
)
const productsBasePath = "products"
const productsResourceName = "products"
// 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)
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"`
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"`
}
// 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"`
}
// List products
func (s *ProductServiceOp) List(options interface{}) ([]Product, error) {
path := fmt.Sprintf("%s/%s.json", globalApiPathPrefix, productsBasePath)
resource := new(ProductsResource)
err := s.client.Get(path, resource, options)
return resource.Products, err
}
// Count products
func (s *ProductServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/%s/count.json", globalApiPathPrefix, productsBasePath)
return s.client.Count(path, options)
}
// Get individual product
func (s *ProductServiceOp) Get(productID int64, options interface{}) (*Product, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, 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/%s.json", globalApiPathPrefix, 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/%s/%d.json", globalApiPathPrefix, 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/%s/%d.json", globalApiPathPrefix, productsBasePath, productID))
}
// List metafields 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)
}
// Get individual metafield 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)
}
// Create a new metafield 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)
}
// Update an existing metafield 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)
}
// // Delete an existing metafield 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)
}