This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
138 lines (112 loc) · 2.7 KB
/
client.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package buffer
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
// URL is the base URL of the Buffer.com API
URL = "https://api.bufferapp.com/1"
)
// Client is the Buffer.com API client
type Client struct {
AccessToken string
URL string
}
// Update represents a Buffer.com update
type Update struct {
ID string
Text string
Day string
ProfileService string `json:"profile_service"`
TextFormatted string `json:"text_formatted"`
}
// Updates represents a set of Buffer.com updates
type Updates []Update
// Profile represents a Buffer.com profile
type Profile struct {
ID string
Service string
FormattedUsername string `json:"formatted_username"`
}
// NewClient creates a new Buffer.com API client
func NewClient(accessToken string) *Client {
return &Client{URL: URL, AccessToken: accessToken}
}
// Push creates a new status update for one or more profiles
func (c *Client) Push(text string, profileIDs []string) (Updates, error) {
var u Updates
params := url.Values{}
params.Set("text", text)
for _, p := range profileIDs {
params.Add("profile_ids[]", p)
}
res, err := c.post("updates/create", params)
if err != nil {
return u, err
}
var response struct {
Success bool
BufferCount int
BufferPercentage int
Updates Updates
Message string
}
err = json.Unmarshal(res, &response)
if err != nil {
return u, err
}
if response.Success == false {
return u, errors.New(response.Message)
}
u = response.Updates
return u, nil
}
func (c *Client) GetProfile(profileID string) (Profile, error) {
var p Profile
res, err := c.get(fmt.Sprintf("profiles/%s", profileID))
if err != nil {
return p, err
}
err = json.Unmarshal(res, &p)
if err != nil {
return p, err
}
return p, nil
}
func (c *Client) GetPendingUpdates(profileID string) (Updates, error) {
var u Updates
res, err := c.get(fmt.Sprintf("profiles/%s/updates/pending", profileID))
if err != nil {
return u, err
}
var response struct {
Total int
Updates Updates
}
err = json.Unmarshal(res, &response)
if err != nil {
return u, err
}
u = response.Updates
return u, nil
}
func (c *Client) get(res string) ([]byte, error) {
req, err := http.Get(fmt.Sprintf("%s/%s.json?access_token=%s", c.URL, res, c.AccessToken))
if err != nil {
return nil, err
}
defer req.Body.Close()
return ioutil.ReadAll(req.Body)
}
func (c *Client) post(res string, params url.Values) ([]byte, error) {
req, err := http.PostForm(fmt.Sprintf("%s/%s.json?access_token=%s", c.URL, res, c.AccessToken), params)
if err != nil {
return nil, err
}
defer req.Body.Close()
return ioutil.ReadAll(req.Body)
}