forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
84 lines (66 loc) · 2.16 KB
/
events.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
package packngo
import (
"path"
)
const eventBasePath = "/events"
// Event struct
type Event struct {
ID string `json:"id,omitempty"`
State string `json:"state,omitempty"`
Type string `json:"type,omitempty"`
Body string `json:"body,omitempty"`
Relationships []Href `json:"relationships,omitempty"`
Interpolated string `json:"interpolated,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
Href string `json:"href,omitempty"`
}
type eventsRoot struct {
Events []Event `json:"events,omitempty"`
Meta meta `json:"meta,omitempty"`
}
// EventService interface defines available event functions
type EventService interface {
List(*ListOptions) ([]Event, *Response, error)
Get(string, *GetOptions) (*Event, *Response, error)
}
// EventServiceOp implements EventService
type EventServiceOp struct {
client *Client
}
// List returns all events
func (s *EventServiceOp) List(listOpt *ListOptions) ([]Event, *Response, error) {
return listEvents(s.client, eventBasePath, listOpt)
}
// Get returns an event by ID
func (s *EventServiceOp) Get(eventID string, getOpt *GetOptions) (*Event, *Response, error) {
if validateErr := ValidateUUID(eventID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(eventBasePath, eventID)
return get(s.client, apiPath, getOpt)
}
// list helper function for all event functions
func listEvents(client requestDoer, endpointPath string, opts *ListOptions) (events []Event, resp *Response, err error) {
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(eventsRoot)
resp, err = client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
events = append(events, subset.Events...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
func get(client *Client, endpointPath string, opts *GetOptions) (*Event, *Response, error) {
event := new(Event)
apiPathQuery := opts.WithQuery(endpointPath)
resp, err := client.DoRequest("GET", apiPathQuery, nil, event)
if err != nil {
return nil, resp, err
}
return event, resp, err
}