forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
109 lines (95 loc) · 3.41 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
package goshopify
import (
"fmt"
"time"
)
const productsBasePath = "admin/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(int, interface{}) (*Product, error)
Create(Product) (*Product, error)
Update(Product) (*Product, error)
Delete(int) error
}
// 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 int `json:"id"`
Title string `json:"title"`
BodyHTML string `json:"body_html"`
Vendor string `json:"vendor"`
ProductType string `json:"product_type"`
Handle string `json:"handle"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
PublishedAt *time.Time `json:"published_at"`
PublishedScope string `json:"published_scope"`
Tags string `json:"tags"`
Options []ProductOption `json:"options"`
Variants []Variant `json:"variants"`
Image Image `json:"image"`
Images []Image `json:"images"`
}
// The options provided by Shopify
type ProductOption struct {
ID int `json:"id"`
ProductID int `json:"product_id"`
Name string `json:"name"`
Position int `json:"position"`
Values []string `json:"values"`
}
// 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.json", 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/count.json", productsBasePath)
return s.client.Count(path, options)
}
// Get individual product
func (s *ProductServiceOp) Get(productID int, 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 int) error {
return s.client.Delete(fmt.Sprintf("%s/%d.json", productsBasePath, productID))
}