-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.go
105 lines (83 loc) · 3.41 KB
/
function.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
package evervault
import (
"encoding/json"
"fmt"
"net/http"
)
// Struct containing a token for Function invocation.
// RunTokenResponse.Token can be used to invoke a function run by the user.
type RunTokenResponse struct {
Token string `json:"token"`
}
// Passing the name of your Evervault Function along with the data to be sent to that function will
// return a RunTokenResponse. This response contains a token that can be returned to your
// client for Function invocation.
func (c *Client) CreateFunctionRunToken(functionName string, payload any) (RunTokenResponse, error) {
tokenResponse, err := c.createRunToken(functionName, payload)
if err != nil {
return RunTokenResponse{}, err
}
return tokenResponse, nil
}
// Response containing the results of a Function run.
// - FunctionRunResponse.Status contains the status of the function invocation (success/failure).
// - FunctionRunResponse.ID contains the run ID of the function invocation.
// - FunctionRunResponse.Result contains the response from the function invocation.
type FunctionRunResponse struct {
Status string `json:"status"`
ID string `json:"id"`
Result map[string]any `json:"result"`
}
// Passing the name of your Evervault Function along with the data to be sent to that
// function will invoke a function in your Evervault App. The response from the function
// will be returned as a FunctionRunResponse.
func (c *Client) RunFunction(functionName string, payload map[string]any) (FunctionRunResponse, error) {
functionResponse, err := c.runFunction(functionName, payload)
if err != nil {
return FunctionRunResponse{}, err
}
return functionResponse, nil
}
func (c *Client) createRunToken(functionName string, payload any) (RunTokenResponse, error) {
pBytes, err := json.Marshal(payload)
if err != nil {
return RunTokenResponse{}, fmt.Errorf("error parsing payload as json %w", err)
}
runTokenURL := fmt.Sprintf("%s/v2/functions/%s/run-token", c.Config.EvAPIURL, functionName)
response, err := c.makeRequest(runTokenURL, http.MethodPost, pBytes, false)
if response.statusCode != http.StatusOK {
return RunTokenResponse{}, APIError{Message: "Error making HTTP request"}
}
if err != nil {
return RunTokenResponse{}, err
}
res := RunTokenResponse{}
if err := json.Unmarshal(response.body, &res); err != nil {
return RunTokenResponse{}, fmt.Errorf("error parsing JSON response %w", err)
}
return res, nil
}
func (c *Client) runFunction(functionName string, payload map[string]any) (FunctionRunResponse, error) {
wrappedPayload := map[string]any{"payload": payload}
pBytes, err := json.Marshal(wrappedPayload)
if err != nil {
return FunctionRunResponse{}, fmt.Errorf("error parsing payload as json %w", err)
}
apiURL := fmt.Sprintf("%s/functions/%s/runs", c.Config.EvAPIURL, functionName)
response, err := c.makeRequest(apiURL, http.MethodPost, pBytes, true)
if err != nil {
return FunctionRunResponse{}, err
}
functionRunResponse := FunctionRunResponse{}
err = json.Unmarshal(response.body, &functionRunResponse)
if err == nil && functionRunResponse.Status == "success" {
return functionRunResponse, nil
} else if err == nil && functionRunResponse.Status == "failure" {
functionRuntimeError := FunctionRuntimeError{}
err = json.Unmarshal(response.body, &functionRuntimeError)
if err == nil {
return FunctionRunResponse{}, functionRuntimeError
}
}
return FunctionRunResponse{}, ExtractAPIError(response.body)
}