Skip to content

Commit 93033ad

Browse files
committed
create func req
1 parent a57e829 commit 93033ad

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

functions.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package webhookrelay
22

33
import (
4+
"encoding/base64"
45
"encoding/json"
56
"fmt"
7+
"io"
8+
"io/ioutil"
69
"net/http"
710

811
"github.com/pkg/errors"
@@ -24,6 +27,20 @@ type FunctionRequest struct {
2427
Driver string `json:"driver"`
2528
}
2629

30+
// CreateFunctionRequest is used when creating a new function
31+
type CreateFunctionRequest struct {
32+
Name string
33+
Driver string
34+
Payload io.Reader
35+
}
36+
37+
type UpdateFunctionRequest struct {
38+
ID string
39+
Name string
40+
Driver string
41+
Payload io.Reader
42+
}
43+
2744
// InvokeFunctionRequest is a function invoke payload
2845
type InvokeFunctionRequest struct {
2946
Headers map[string][]string `json:"headers"`
@@ -94,9 +111,20 @@ func (api *API) GetFunction(ref string) (*Function, error) {
94111
}
95112

96113
// CreateFunction - create new function
97-
func (api *API) CreateFunction(opts *FunctionRequest) (*Function, error) {
114+
func (api *API) CreateFunction(opts *CreateFunctionRequest) (*Function, error) {
115+
116+
functionBody, err := ioutil.ReadAll(opts.Payload)
117+
if err != nil {
118+
return nil, errors.Wrap(err, "failed to read function body")
119+
}
120+
121+
createOpts := &FunctionRequest{
122+
Name: opts.Name,
123+
Driver: opts.Driver,
124+
Payload: base64.StdEncoding.EncodeToString(functionBody),
125+
}
98126

99-
resp, err := api.makeRequest("POST", "/functions", opts)
127+
resp, err := api.makeRequest("POST", "/functions", createOpts)
100128
if err != nil {
101129
return nil, err
102130
}

0 commit comments

Comments
 (0)