-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_config.go
93 lines (74 loc) · 2.51 KB
/
functions_config.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
package webhookrelay
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/pkg/errors"
reactor_v1 "github.com/webhookrelay/webhookrelay-go/api/reactor/v1"
)
// ListConfigResponse defines function config
type ListConfigResponse = reactor_v1.ListConfigResponse
// Variable is function configuration variable
type Variable = reactor_v1.Variable
// SetFunctionConfigRequest sets/updates function configuration
type SetFunctionConfigRequest struct {
ID string `json:"-"` // function ID
Key string `json:"key"`
Value string `json:"value"`
}
// FunctionConfigurationVariablesListOptions is used to list function config variables
type FunctionConfigurationVariablesListOptions struct {
ID string
}
// ListFunctionConfigurationVariables lists function configuration variables
func (api *API) ListFunctionConfigurationVariables(options *FunctionConfigurationVariablesListOptions) ([]*Variable, error) {
resp, err := api.makeRequest(http.MethodGet, "/functions/"+options.ID+"/config", nil)
if err != nil {
return nil, errors.Wrap(err, errMakeRequestError)
}
var result *ListConfigResponse
err = json.Unmarshal(resp, &result)
if err != nil {
return nil, errors.Wrap(err, errUnmarshalError)
}
return result.Variables, nil
}
// SetFunctionConfigurationVariable allows users to set config variables for a function. Function can then use special methods
// to retrieve those variables during runtime.
func (api *API) SetFunctionConfigurationVariable(options *SetFunctionConfigRequest) (*Variable, error) {
resp, err := api.makeRequest("PUT", "/functions/"+options.ID+"/config", options)
if err != nil {
return nil, err
}
var result Variable
if err := json.Unmarshal(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// FunctionConfigurationVariableDeleteOptions is used in function configuration variable delete request
type FunctionConfigurationVariableDeleteOptions struct {
ID string
Key string
}
// DeleteFunctionConfigurationVariable - delete function configuration variable
func (api *API) DeleteFunctionConfigurationVariable(options *FunctionConfigurationVariableDeleteOptions) error {
if options.ID == "" {
return fmt.Errorf("ID must be supplied")
}
if options.Key == "" {
return fmt.Errorf("Key must be supplied")
}
id, err := api.ensureFunctionID(options.ID)
if err != nil {
return err
}
options.ID = id
path := url.PathEscape("/functions/" + options.ID + "/config/" + options.Key)
_, err = api.makeRequest("DELETE", path, nil)
if err != nil {
return err
}
return nil
}