-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs.go
229 lines (193 loc) · 5.75 KB
/
inputs.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package webhookrelay
import (
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
var (
// ErrNoSuchInput is the error returned when the Input does not exist.
ErrNoSuchInput = errors.New("no such input")
)
// AnyResponseFromOutput indicates that the input should return response from
// whichever output responds first.
const AnyResponseFromOutput = "anyOutput"
// Input - webhook inputs are used to create endpoints which are then used
// by remote systems
type Input struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
FunctionID string `json:"function_id"`
BucketID string `json:"bucket_id"`
Headers map[string][]string `json:"headers"`
StatusCode int `json:"status_code"`
Body string `json:"body"`
// either output ID or "anyOutput" to indicate that the first response
// from any output is good enough. Empty string
ResponseFromOutput string `json:"response_from_output"`
CustomDomain string `json:"custom_domain"`
PathPrefix string `json:"path_prefix"`
Description string `json:"description"`
}
func (i *Input) String() string {
return fmt.Sprintf("%s [%s]", i.Name, i.EndpointURL())
}
// EndpointURL returns default input URL. If CustomDomain is set (all new inputs from 2020 06 01 are getting them),
// then it's this domain with path prefix, otherwise it's the URL based on the input ID
func (i *Input) EndpointURL() string {
if i.CustomDomain != "" {
return "https://" + i.CustomDomain + i.PathPrefix
}
return "https://my.webhookrelay.com/v1/webhooks/" + i.ID
}
// MarshalJSON helper to change time into unix
func (i *Input) MarshalJSON() ([]byte, error) {
type Alias Input
return json.Marshal(&struct {
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
*Alias
}{
CreatedAt: i.CreatedAt.Unix(),
UpdatedAt: i.UpdatedAt.Unix(),
Alias: (*Alias)(i),
})
}
// UnmarshalJSON helper to change time from unix
func (i *Input) UnmarshalJSON(data []byte) error {
type Alias Input
aux := &struct {
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
*Alias
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.CreatedAt = time.Unix(aux.CreatedAt, 0)
i.UpdatedAt = time.Unix(aux.UpdatedAt, 0)
return nil
}
// InputListOptions used to query inputs
type InputListOptions struct {
Bucket string // Bucket reference - ID or name
}
// ListInputs returns a list of inputs belonging to the bucket. If bucket reference not supplied,
// all account inputs will be returned
func (api *API) ListInputs(options *InputListOptions) ([]*Input, error) {
if options.Bucket == "" {
return api.allInputList(&BucketListOptions{})
}
bucket, err := api.GetBucket(options.Bucket)
if err != nil {
return nil, err
}
var inputs []*Input
for idx := range bucket.Inputs {
inputs = append(inputs, bucket.Inputs[idx])
}
return inputs, nil
}
func (api *API) allInputList(opts *BucketListOptions) ([]*Input, error) {
buckets, err := api.ListBuckets(opts)
if err != nil {
return nil, fmt.Errorf("failed to get inputs, error: %w", err)
}
var inputs []*Input
for idx := range buckets {
for bIdx := range buckets[idx].Inputs {
inputs = append(inputs, buckets[idx].Inputs[bIdx])
}
}
return inputs, nil
}
// CreateInput creates an Input and returns the new object.
func (api *API) CreateInput(options *Input) (*Input, error) {
bucketID, err := api.ensureBucketID(options.BucketID)
if err != nil {
return nil, err
}
resp, err := api.makeRequest("POST", "/buckets/"+bucketID+"/inputs", options)
if err != nil {
return nil, err
}
var input Input
err = json.Unmarshal(resp, &input)
return &input, nil
}
// UpdateInput updates existing input
func (api *API) UpdateInput(options *Input) (*Input, error) {
if options.BucketID == "" {
return nil, fmt.Errorf("bucket not specified")
}
if options.ID == "" && options.Name == "" {
return nil, fmt.Errorf("either input ID or name has to be specified")
}
bucketID, err := api.ensureBucketID(options.BucketID)
if err != nil {
return nil, err
}
inputID, err := api.ensureInputID(options.ID)
if err != nil {
return nil, err
}
resp, err := api.makeRequest("PUT", "/buckets/"+bucketID+"/inputs/"+inputID, options)
if err != nil {
return nil, err
}
var input Input
err = json.Unmarshal(resp, &input)
return &input, nil
}
// InputDeleteOptions delete options
type InputDeleteOptions struct {
Bucket string
Input string // ID or name
}
// DeleteInput removes input. If public input is used by the UUID, beware that after deleting
// an input you will not be able to recreate another one with the same ID.
func (api *API) DeleteInput(options *InputDeleteOptions) error {
if options.Bucket == "" {
return fmt.Errorf("bucket not specified")
}
if options.Input == "" {
return fmt.Errorf("input not specified")
}
bucketID, err := api.ensureBucketID(options.Bucket)
if err != nil {
return err
}
inputID, err := api.ensureInputID(options.Input)
if err != nil {
return err
}
_, err = api.makeRequest("DELETE", "/buckets/"+bucketID+"/inputs/"+inputID, nil)
return err
}
// ensureInputID - takes name/id and always returns ID (when it not fails)
func (api *API) ensureInputID(ref string) (string, error) {
if !IsUUID(ref) {
id, err := api.inputIDFromName(ref)
if err != nil {
return "", err
}
return id, nil
}
return ref, nil
}
func (api *API) inputIDFromName(name string) (id string, err error) {
inputs, err := api.ListInputs(&InputListOptions{})
if err != nil {
return
}
for _, b := range inputs {
if b.Name == name {
return b.ID, nil
}
}
return "", ErrNoSuchInput
}