forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactions.go
120 lines (101 loc) · 3.22 KB
/
reactions.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package stream
import (
"encoding/json"
"errors"
"fmt"
)
// ReactionsClient is a specialized client used to interact with the Reactions endpoints.
type ReactionsClient struct {
client *Client
}
//Add adds a reaction.
func (c *ReactionsClient) Add(r AddReactionRequestObject) (*Reaction, error) {
if r.ParentID != "" {
return nil, errors.New("`Parent` not empty. For adding child reactions use `AddChild`")
}
return c.addReaction(r)
}
//AddChild adds a child reaction to the provided parent.
func (c *ReactionsClient) AddChild(parentID string, r AddReactionRequestObject) (*Reaction, error) {
r.ParentID = parentID
return c.addReaction(r)
}
func (c *ReactionsClient) addReaction(r AddReactionRequestObject) (*Reaction, error) {
endpoint := c.client.makeEndpoint("reaction/")
resp, err := c.client.post(endpoint, r, c.client.authenticator.reactionsAuth)
if err != nil {
return nil, err
}
result := &Reaction{}
err = json.Unmarshal(resp, result)
if err != nil {
return nil, err
}
return result, nil
}
// Update updates the reaction's data and/or target feeds.
func (c *ReactionsClient) Update(id string, data map[string]interface{}, targetFeeds []string) (*Reaction, error) {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
reqData := map[string]interface{}{
"data": data,
"target_feeds": targetFeeds,
}
resp, err := c.client.put(endpoint, reqData, c.client.authenticator.reactionsAuth)
if err != nil {
return nil, err
}
result := &Reaction{}
err = json.Unmarshal(resp, result)
if err != nil {
return nil, err
}
return result, nil
}
//Get retrieves a reaction having the given id.
func (c *ReactionsClient) Get(id string) (*Reaction, error) {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
resp, err := c.client.get(endpoint, nil, c.client.authenticator.reactionsAuth)
if err != nil {
return nil, err
}
result := &Reaction{}
err = json.Unmarshal(resp, result)
if err != nil {
return nil, err
}
return result, nil
}
//Delete deletes a reaction having the given id.
func (c *ReactionsClient) Delete(id string) error {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
_, err := c.client.delete(endpoint, nil, c.client.authenticator.reactionsAuth)
return err
}
//Filter lists reactions based on the provided criteria and with the specified pagination.
func (c *ReactionsClient) Filter(attr FilterReactionsAttribute, opts ...FilterReactionsOption) (*FilterReactionResponse, error) {
var endpointURI string
endpointURI = fmt.Sprintf("reaction/%s/", attr())
endpoint := c.client.makeEndpoint(endpointURI)
for _, opt := range opts {
endpoint.addQueryParam(opt)
}
resp, err := c.client.get(endpoint, nil, c.client.authenticator.reactionsAuth)
if err != nil {
return nil, err
}
result := &FilterReactionResponse{}
err = json.Unmarshal(resp, result)
if err != nil {
return nil, err
}
result.meta.attr = attr
return result, nil
}
// GetNextPageFilteredReactions returns the reactions at the "next" page of a previous *FilterReactionResponse response, if any.
func (c *ReactionsClient) GetNextPageFilteredReactions(resp *FilterReactionResponse) (*FilterReactionResponse, error) {
opts, err := resp.parseNext()
if err != nil {
return nil, err
}
return c.Filter(resp.meta.attr, opts...)
}