-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.go
82 lines (67 loc) · 1.58 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
package parse
import (
"encoding/json"
"errors"
"net/url"
"path"
"reflect"
)
type Params map[string]interface{}
func CallFunction(name string, params Params, resp interface{}) error {
return callFn(name, params, resp, nil)
}
type callFnT struct {
name string
params Params
currentSession *sessionT
}
func (c *callFnT) method() string {
return "POST"
}
func (c *callFnT) endpoint() (string, error) {
p := path.Join(defaultClient.config.Version, "functions", c.name)
u := url.URL{}
u.Scheme = defaultClient.config.Schema
u.Host = defaultClient.config.Host
u.Path = path.Join(defaultClient.config.PathPrefix, p)
return u.String(), nil
}
func (c *callFnT) body() (string, error) {
b, err := json.Marshal(c.params)
return string(b), err
}
func (c *callFnT) useMasterKey() bool {
return false
}
func (c *callFnT) session() *sessionT {
return c.currentSession
}
func (c *callFnT) contentType() string {
return "application/json"
}
type fnRespT struct {
Result interface{} `parse:"result"`
}
func callFn(name string, params Params, resp interface{}, currentSession *sessionT) error {
rv := reflect.ValueOf(resp)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("resp must be a non-nil pointer")
}
if params == nil {
params = Params{}
}
cr := &callFnT{
name: name,
params: params,
currentSession: currentSession,
}
if b, err := defaultClient.doRequest(cr); err != nil {
return err
} else {
r := fnRespT{}
if err := json.Unmarshal(b, &r); err != nil {
return err
}
return populateValue(resp, r.Result)
}
}