-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtrigger.go
172 lines (149 loc) · 4.83 KB
/
trigger.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package zendesk
import (
"context"
"encoding/json"
"fmt"
"time"
)
// TriggerCondition zendesk trigger condition
//
// ref: https://developer.zendesk.com/rest_api/docs/core/triggers#conditions-reference
type TriggerCondition struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value interface{} `json:"value"`
}
// TriggerAction is zendesk trigger action
//
// ref: https://developer.zendesk.com/rest_api/docs/core/triggers#actions
type TriggerAction struct {
Field string `json:"field"`
Value interface{} `json:"value"`
}
// Trigger is zendesk trigger JSON payload format
//
// ref: https://developer.zendesk.com/rest_api/docs/core/triggers#json-format
type Trigger struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title"`
Active bool `json:"active,omitempty"`
Position int64 `json:"position,omitempty"`
Conditions struct {
All []TriggerCondition `json:"all"`
Any []TriggerCondition `json:"any"`
} `json:"conditions"`
Actions []TriggerAction `json:"actions"`
Description string `json:"description,omitempty"`
CategoryID string `json:"category_id,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
// TriggerListOptions is options for GetTriggers
//
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#list-triggers
type TriggerListOptions struct {
PageOptions
Active bool `url:"active,omitempty"`
CategoryID string `url:"category_id,omitempty"`
SortBy string `url:"sort_by,omitempty"`
SortOrder string `url:"sort_order,omitempty"`
}
// TriggerAPI an interface containing all trigger related methods
type TriggerAPI interface {
GetTriggers(ctx context.Context, opts *TriggerListOptions) ([]Trigger, Page, error)
CreateTrigger(ctx context.Context, trigger Trigger) (Trigger, error)
GetTrigger(ctx context.Context, id int64) (Trigger, error)
UpdateTrigger(ctx context.Context, id int64, trigger Trigger) (Trigger, error)
DeleteTrigger(ctx context.Context, id int64) error
GetTriggersIterator(ctx context.Context, opts *PaginationOptions) *Iterator[Trigger]
GetTriggersOBP(ctx context.Context, opts *OBPOptions) ([]Trigger, Page, error)
GetTriggersCBP(ctx context.Context, opts *CBPOptions) ([]Trigger, CursorPaginationMeta, error)
}
// GetTriggers fetch trigger list
//
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#getting-triggers
func (z *Client) GetTriggers(ctx context.Context, opts *TriggerListOptions) ([]Trigger, Page, error) {
var data struct {
Triggers []Trigger `json:"triggers"`
Page
}
if opts == nil {
return []Trigger{}, Page{}, &OptionsError{opts}
}
u, err := addOptions("/triggers.json", opts)
if err != nil {
return []Trigger{}, Page{}, err
}
body, err := z.get(ctx, u)
if err != nil {
return []Trigger{}, Page{}, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return []Trigger{}, Page{}, err
}
return data.Triggers, data.Page, nil
}
// CreateTrigger creates new trigger
//
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#create-trigger
func (z *Client) CreateTrigger(ctx context.Context, trigger Trigger) (Trigger, error) {
var data, result struct {
Trigger Trigger `json:"trigger"`
}
data.Trigger = trigger
body, err := z.post(ctx, "/triggers.json", data)
if err != nil {
return Trigger{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return Trigger{}, err
}
return result.Trigger, nil
}
// GetTrigger returns the specified trigger
//
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#getting-triggers
func (z *Client) GetTrigger(ctx context.Context, id int64) (Trigger, error) {
var result struct {
Trigger Trigger `json:"trigger"`
}
body, err := z.get(ctx, fmt.Sprintf("/triggers/%d.json", id))
if err != nil {
return Trigger{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return Trigger{}, err
}
return result.Trigger, nil
}
// UpdateTrigger updates the specified trigger and returns the updated one
//
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#update-trigger
func (z *Client) UpdateTrigger(ctx context.Context, id int64, trigger Trigger) (Trigger, error) {
var data, result struct {
Trigger Trigger `json:"trigger"`
}
data.Trigger = trigger
body, err := z.put(ctx, fmt.Sprintf("/triggers/%d.json", id), data)
if err != nil {
return Trigger{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return Trigger{}, err
}
return result.Trigger, nil
}
// DeleteTrigger deletes the specified trigger
//
// ref: https://developer.zendesk.com/rest_api/docs/support/triggers#delete-trigger
func (z *Client) DeleteTrigger(ctx context.Context, id int64) error {
err := z.delete(ctx, fmt.Sprintf("/triggers/%d.json", id))
if err != nil {
return err
}
return nil
}