-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_code.go
55 lines (52 loc) · 1.47 KB
/
cloud_code.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
package parse
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
)
// CallCloudFunction invokes the given cloud code function.
// The arguments parameter is serialized as JSON and provided as parameters.
func (c *Client) CallCloudFunction(functionName string, arguments map[string]interface{}) ([]byte, error) {
if arguments == nil {
arguments = map[string]interface{}{}
}
payload, err := json.Marshal(arguments)
if err != nil {
return nil, err
}
uri := fmt.Sprintf("/1/functions/%s", functionName)
resp, err := c.doWithBody("POST", uri, bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.trace("CallCloudFunction err", uri, string(body))
return nil, err
}
c.trace("CallCloudFunction", uri, string(body))
return body, err
}
// CallCloudJob schedules the given cloud code job.
// The arguments parameter is serialized as JSON and provided as parameters.
func (c *Client) CallCloudJob(jobName string, arguments interface{}) ([]byte, error) {
payload, err := json.Marshal(arguments)
if err != nil {
return nil, err
}
uri := fmt.Sprintf("/1/jobs/%s", jobName)
resp, err := c.doWithBody("POST", uri, bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.trace("CallCloudJob err", uri, string(body))
return nil, err
}
c.trace("CallCloudJob", uri, string(body))
return body, err
}