This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
metafield.go
113 lines (100 loc) · 3.91 KB
/
metafield.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
package goshopify
import (
"fmt"
"time"
)
// MetafieldService is an interface for interfacing with the metafield endpoints
// of the Shopify API.
// https://help.shopify.com/api/reference/metafield
type MetafieldService interface {
List(interface{}) ([]Metafield, error)
Count(interface{}) (int, error)
Get(int, interface{}) (*Metafield, error)
Create(Metafield) (*Metafield, error)
Update(Metafield) (*Metafield, error)
Delete(int) error
}
// MetafieldsService is an interface for other Shopify resources
// to interface with the metafield endpoints of the Shopify API.
// https://help.shopify.com/api/reference/metafield
type MetafieldsService interface {
ListMetafields(int, interface{}) ([]Metafield, error)
CountMetafields(int, interface{}) (int, error)
GetMetafield(int, int, interface{}) (*Metafield, error)
CreateMetafield(int, Metafield) (*Metafield, error)
UpdateMetafield(int, Metafield) (*Metafield, error)
DeleteMetafield(int, int) error
}
// MetafieldServiceOp handles communication with the metafield
// related methods of the Shopify API.
type MetafieldServiceOp struct {
client *Client
resource string
resourceID int
}
// Metafield represents a Shopify metafield.
type Metafield struct {
ID int `json:"id,omitempty"`
Key string `json:"key,omitempty"`
Value interface{} `json:"value,omitempty"`
ValueType string `json:"value_type,omitempty"`
Namespace string `json:"namespace,omitempty"`
Description string `json:"description,omitempty"`
OwnerId int `json:"owner_id,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
OwnerResource string `json:"owner_resource,omitempty"`
}
// MetafieldResource represents the result from the metafields/X.json endpoint
type MetafieldResource struct {
Metafield *Metafield `json:"metafield"`
}
// MetafieldsResource represents the result from the metafields.json endpoint
type MetafieldsResource struct {
Metafields []Metafield `json:"metafields"`
}
// List metafields
func (s *MetafieldServiceOp) List(options interface{}) ([]Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s.json", prefix)
resource := new(MetafieldsResource)
err := s.client.Get(path, resource, options)
return resource.Metafields, err
}
// Count metafields
func (s *MetafieldServiceOp) Count(options interface{}) (int, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/count.json", prefix)
return s.client.Count(path, options)
}
// Get individual metafield
func (s *MetafieldServiceOp) Get(metafieldID int, options interface{}) (*Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d.json", prefix, metafieldID)
resource := new(MetafieldResource)
err := s.client.Get(path, resource, options)
return resource.Metafield, err
}
// Create a new metafield
func (s *MetafieldServiceOp) Create(metafield Metafield) (*Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s.json", prefix)
wrappedData := MetafieldResource{Metafield: &metafield}
resource := new(MetafieldResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Metafield, err
}
// Update an existing metafield
func (s *MetafieldServiceOp) Update(metafield Metafield) (*Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d.json", prefix, metafield.ID)
wrappedData := MetafieldResource{Metafield: &metafield}
resource := new(MetafieldResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Metafield, err
}
// Delete an existing metafield
func (s *MetafieldServiceOp) Delete(metafieldID int) error {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
return s.client.Delete(fmt.Sprintf("%s/%d.json", prefix, metafieldID))
}