-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.go
48 lines (40 loc) · 1.01 KB
/
functions.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
package functions
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func (c *Client) Invoke(functionName string, payload interface{}) (string, error) {
// Marshal the payload to JSON
jsonData, err := json.Marshal(payload)
if err != nil {
return "", err
}
// Build the URL and create the request
url := c.clientTransport.baseUrl.String() + "/" + functionName
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
if err != nil {
return "", err
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// Execute the request using the client's session
resp, err := c.session.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read the response body
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Check HTTP response status code
if resp.StatusCode >= 400 {
return "", fmt.Errorf("server responded with error: %s", resp.Status)
}
return string(responseBody), nil
}