forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripttag.go
107 lines (93 loc) · 3.64 KB
/
scripttag.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
package goshopify
import (
"context"
"fmt"
"time"
)
const scriptTagsBasePath = "script_tags"
// ScriptTagService is an interface for interfacing with the ScriptTag endpoints
// of the Shopify API.
// See: https://help.shopify.com/api/reference/scripttag
type ScriptTagService interface {
List(context.Context, interface{}) ([]ScriptTag, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*ScriptTag, error)
Create(context.Context, ScriptTag) (*ScriptTag, error)
Update(context.Context, ScriptTag) (*ScriptTag, error)
Delete(context.Context, uint64) error
}
// ScriptTagServiceOp handles communication with the shop related methods of the
// Shopify API.
type ScriptTagServiceOp struct {
client *Client
}
// ScriptTag represents a Shopify ScriptTag.
type ScriptTag struct {
CreatedAt *time.Time `json:"created_at"`
Event string `json:"event"`
Id uint64 `json:"id"`
Src string `json:"src"`
DisplayScope string `json:"display_scope"`
UpdatedAt *time.Time `json:"updated_at"`
}
// The options provided by Shopify.
type ScriptTagOption struct {
Limit int `url:"limit,omitempty"`
Page int `url:"page,omitempty"`
SinceId uint64 `url:"since_id,omitempty"`
CreatedAtMin time.Time `url:"created_at_min,omitempty"`
CreatedAtMax time.Time `url:"created_at_max,omitempty"`
UpdatedAtMin time.Time `url:"updated_at_min,omitempty"`
UpdatedAtMax time.Time `url:"updated_at_max,omitempty"`
Src string `url:"src,omitempty"`
Fields string `url:"fields,omitempty"`
}
// ScriptTagsResource represents the result from the admin/script_tags.json
// endpoint.
type ScriptTagsResource struct {
ScriptTags []ScriptTag `json:"script_tags"`
}
// ScriptTagResource represents the result from the
// admin/script_tags/{#script_tag_id}.json endpoint.
type ScriptTagResource struct {
ScriptTag *ScriptTag `json:"script_tag"`
}
// List script tags
func (s *ScriptTagServiceOp) List(ctx context.Context, options interface{}) ([]ScriptTag, error) {
path := fmt.Sprintf("%s.json", scriptTagsBasePath)
resource := &ScriptTagsResource{}
err := s.client.Get(ctx, path, resource, options)
return resource.ScriptTags, err
}
// Count script tags
func (s *ScriptTagServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", scriptTagsBasePath)
return s.client.Count(ctx, path, options)
}
// Get individual script tag
func (s *ScriptTagServiceOp) Get(ctx context.Context, tagId uint64, options interface{}) (*ScriptTag, error) {
path := fmt.Sprintf("%s/%d.json", scriptTagsBasePath, tagId)
resource := &ScriptTagResource{}
err := s.client.Get(ctx, path, resource, options)
return resource.ScriptTag, err
}
// Create a new script tag
func (s *ScriptTagServiceOp) Create(ctx context.Context, tag ScriptTag) (*ScriptTag, error) {
path := fmt.Sprintf("%s.json", scriptTagsBasePath)
wrappedData := ScriptTagResource{ScriptTag: &tag}
resource := &ScriptTagResource{}
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.ScriptTag, err
}
// Update an existing script tag
func (s *ScriptTagServiceOp) Update(ctx context.Context, tag ScriptTag) (*ScriptTag, error) {
path := fmt.Sprintf("%s/%d.json", scriptTagsBasePath, tag.Id)
wrappedData := ScriptTagResource{ScriptTag: &tag}
resource := &ScriptTagResource{}
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.ScriptTag, err
}
// Delete an existing script tag
func (s *ScriptTagServiceOp) Delete(ctx context.Context, tagId uint64) error {
return s.client.Delete(ctx, fmt.Sprintf("%s/%d.json", scriptTagsBasePath, tagId))
}