/
automation.go
172 lines (144 loc) · 4.77 KB
/
automation.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"
)
// AutomationCondition zendesk automation condition
//
// ref: https://developer.zendesk.com/rest_api/docs/core/automations#conditions-reference
type AutomationCondition struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value string `json:"value"`
}
// AutomationAction is zendesk automation action
//
// ref: https://developer.zendesk.com/rest_api/docs/core/automations#actions
type AutomationAction struct {
Field string `json:"field"`
Value interface{} `json:"value"`
}
// Automation is zendesk automation JSON payload format
//
// ref: https://developer.zendesk.com/rest_api/docs/core/automations#json-format
type Automation struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title"`
Active bool `json:"active,omitempty"`
Position int64 `json:"position,omitempty"`
Conditions struct {
All []AutomationCondition `json:"all"`
Any []AutomationCondition `json:"any"`
} `json:"conditions"`
Actions []AutomationAction `json:"actions"`
RawTitle string `json:"raw_title,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
// AutomationListOptions is options for GetAutomations
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#list-automations
type AutomationListOptions struct {
PageOptions
Active bool `url:"active,omitempty"`
SortBy string `url:"sort_by,omitempty"`
SortOrder string `url:"sort_order,omitempty"`
}
// AutomationAPI an interface containing all automation related methods
type AutomationAPI interface {
GetAutomations(ctx context.Context, opts *AutomationListOptions) ([]Automation, Page, error)
CreateAutomation(ctx context.Context, automation Automation) (Automation, error)
GetAutomation(ctx context.Context, id int64) (Automation, error)
UpdateAutomation(ctx context.Context, id int64, automation Automation) (Automation, error)
DeleteAutomation(ctx context.Context, id int64) error
}
// GetAutomations fetch automation list
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#getting-automations
func (z *Client) GetAutomations(ctx context.Context, opts *AutomationListOptions) ([]Automation, Page, error) {
var data struct {
Automations []Automation `json:"automations"`
Page
}
if opts == nil {
return []Automation{}, Page{}, &OptionsError{opts}
}
u, err := addOptions("/automations.json", opts)
if err != nil {
return []Automation{}, Page{}, err
}
body, err := z.get(ctx, u)
if err != nil {
return []Automation{}, Page{}, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return []Automation{}, Page{}, err
}
return data.Automations, data.Page, nil
}
// CreateAutomation creates new automation
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#create-automation
func (z *Client) CreateAutomation(ctx context.Context, automation Automation) (Automation, error) {
var data, result struct {
Automation Automation `json:"automation"`
}
data.Automation = automation
body, err := z.post(ctx, "/automations.json", data)
if err != nil {
return Automation{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return Automation{}, err
}
return result.Automation, nil
}
// GetAutomation returns the specified automation
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#getting-automations
func (z *Client) GetAutomation(ctx context.Context, id int64) (Automation, error) {
var result struct {
Automation Automation `json:"automation"`
}
body, err := z.get(ctx, fmt.Sprintf("/automations/%d.json", id))
if err != nil {
return Automation{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return Automation{}, err
}
return result.Automation, nil
}
// UpdateAutomation updates the specified automation and returns the updated one
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#update-automation
func (z *Client) UpdateAutomation(ctx context.Context, id int64, automation Automation) (Automation, error) {
var data, result struct {
Automation Automation `json:"automation"`
}
data.Automation = automation
body, err := z.put(ctx, fmt.Sprintf("/automations/%d.json", id), data)
if err != nil {
return Automation{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return Automation{}, err
}
return result.Automation, nil
}
// DeleteAutomation deletes the specified automation
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#delete-automation
func (z *Client) DeleteAutomation(ctx context.Context, id int64) error {
err := z.delete(ctx, fmt.Sprintf("/automations/%d.json", id))
if err != nil {
return err
}
return nil
}