/
reactions.go
63 lines (57 loc) · 1.98 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
package http
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/shurcooL/reactions"
"golang.org/x/net/context/ctxhttp"
)
// Reactions implements reactions.Service remotely over HTTP.
type Reactions struct{}
func (Reactions) List(ctx context.Context, uri string) (map[string][]reactions.Reaction, error) {
u := url.URL{Path: "/api/react/list", RawQuery: url.Values{"ReactableURL": {uri}}.Encode()}
resp, err := ctxhttp.Get(ctx, nil, u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("did not get acceptable status code: %v body: %q", resp.Status, body)
}
var rm map[string][]reactions.Reaction
err = json.NewDecoder(resp.Body).Decode(&rm)
return rm, err
}
func (Reactions) Get(ctx context.Context, uri string, id string) ([]reactions.Reaction, error) {
u := url.URL{Path: "/api/react", RawQuery: url.Values{"reactableURL": {uri}, "reactableID": {id}}.Encode()}
resp, err := ctxhttp.Get(ctx, nil, u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("did not get acceptable status code: %v body: %q", resp.Status, body)
}
var rs []reactions.Reaction
err = json.NewDecoder(resp.Body).Decode(&rs)
return rs, err
}
func (Reactions) Toggle(ctx context.Context, uri string, id string, tr reactions.ToggleRequest) ([]reactions.Reaction, error) {
resp, err := ctxhttp.PostForm(ctx, nil, "/api/react", url.Values{"reactableURL": {uri}, "reactableID": {id}, "reaction": {string(tr.Reaction)}})
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("did not get acceptable status code: %v body: %q", resp.Status, body)
}
var rs []reactions.Reaction
err = json.NewDecoder(resp.Body).Decode(&rs)
return rs, err
}