forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonalization.go
63 lines (58 loc) · 1.93 KB
/
personalization.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
package stream
import (
"encoding/json"
"fmt"
)
// PersonalizationClient is a specialized client for personalization features.
type PersonalizationClient struct {
client *Client
}
// Get obtains a PersonalizationResponse for the given resource and params.
func (c *PersonalizationClient) Get(resource string, params map[string]interface{}) (*PersonalizationResponse, error) {
if resource == "" {
return nil, fmt.Errorf("missing resource")
}
endpoint := c.client.makeEndpoint("%s/", resource)
for k, v := range params {
endpoint.addQueryParam(makeRequestOption(k, v))
}
resp, err := c.client.get(endpoint, nil, c.client.authenticator.personalizationAuth)
if err != nil {
return nil, err
}
var personalizationResp PersonalizationResponse
err = json.Unmarshal(resp, &personalizationResp)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal resp: %s", err)
}
return &personalizationResp, nil
}
// Post sends data to the given resource, adding the given params to the request.
func (c *PersonalizationClient) Post(resource string, params map[string]interface{}, data map[string]interface{}) error {
if resource == "" {
return fmt.Errorf("missing resource")
}
endpoint := c.client.makeEndpoint("%s/", resource)
for k, v := range params {
endpoint.addQueryParam(makeRequestOption(k, v))
}
if data != nil {
data = map[string]interface{}{
"data": data,
}
}
_, err := c.client.post(endpoint, data, c.client.authenticator.personalizationAuth)
return err
}
// Delete removes data from the given resource, adding the given params to the request.
func (c *PersonalizationClient) Delete(resource string, params map[string]interface{}) error {
if resource == "" {
return fmt.Errorf("missing resource")
}
endpoint := c.client.makeEndpoint("%s/", resource)
for k, v := range params {
endpoint.addQueryParam(makeRequestOption(k, v))
}
_, err := c.client.delete(endpoint, nil, c.client.authenticator.personalizationAuth)
return err
}