forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
92 lines (78 loc) · 2.7 KB
/
event.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
package goshopify
import (
"fmt"
"net/http"
"time"
)
const eventsBasePath = "events"
const eventsResourceName = "events"
// EventService is an interface for interfacing with the events endpoints of
// the Shopify API.
// See: https://help.shopify.com/api/reference/event
type EventService interface {
List(interface{}) ([]Event, error)
ListWithPagination(interface{}) ([]Event, *Pagination, error)
Count(interface{}) (int, error)
}
// EventServiceOp handles communication with the event related methods of the
// Shopify API.
type EventServiceOp struct {
client *Client
}
// Represents the result from the events.json endpoint
type EventsResource struct {
Events []Event `json:"events"`
}
// A struct for all available event list options.
// See: https://help.shopify.com/api/reference/event#index
type EventListOptions struct {
CreatedAtMax time.Time `url:"created_at_max,omitempty"`
CreatedAtMin time.Time `url:"created_at_min,omitempty"`
Fields string `url:"fields,omitempty"`
Limit int `url:"limit,omitempty"`
SinceID int64 `url:"since_id,omitempty"`
Verb string `url:"verb,omitempty"`
Filter string `url:"filter,omitempty"`
}
// Event represents a Shopify event
type Event struct {
ID int `json:"id,omitempty"`
SubjectID int `json:"subject_id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
SubjectType string `json:"subject_type,omitempty"`
Verb string `json:"verb,omitempty"`
Arguments []interface{} `json:"arguments,omitempty"`
Body string `json:"body,omitempty"`
Message string `json:"message,omitempty"`
Author string `json:"author,omitempty"`
Description string `json:"description,omitempty"`
Path string `json:"path,omitempty"`
}
// List events
func (s *EventServiceOp) List(options interface{}) ([]Event, error) {
events, _, err := s.ListWithPagination(options)
if err != nil {
return nil, err
}
return events, nil
}
func (s *EventServiceOp) ListWithPagination(options interface{}) ([]Event, *Pagination, error) {
path := fmt.Sprintf("%s.json", eventsBasePath)
resource := new(EventsResource)
headers := http.Header{}
headers, err := s.client.createAndDoGetHeaders("GET", path, nil, options, resource)
if err != nil {
return nil, nil, err
}
// Extract pagination info from header
linkHeader := headers.Get("Link")
pagination, err := extractPagination(linkHeader)
if err != nil {
return nil, nil, err
}
return resource.Events, pagination, nil
}
func (s *EventServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", eventsBasePath)
return s.client.Count(path, options)
}