/
bufferapi.go
177 lines (155 loc) · 4.34 KB
/
bufferapi.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package bufferapi
import (
"code.google.com/p/goauth2/oauth"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
)
type Client struct {
AccessToken string
transport *oauth.Transport
}
type Profile struct {
Id string `json:"id"`
UserId string `json:"user_id"`
Avatar string `json:"avatar"`
CreatedAt int `json:"created_at"`
Default bool `json:"default"`
FormattedUsername string `json:"formatted_username"`
Schedules []Schedule `json:"schedules"`
Service string `json:service"`
ServiceId string `json:"service_id"`
ServiceUsername string `json:"service_username"`
Statistics map[string]int `json:"statistics"`
TeamMembers []string `json:"team_members"`
Timezone string `json:"timezone"`
}
type Schedule struct {
Days []string `json:"days"`
Times []string `json:"times"`
}
type NewUpdate struct {
Text string `json:"text"`
ProfileIds []string `json:"profile_ids"`
Shorten bool `json:"shorten"`
Now bool `json:"now"`
Media map[string]string `json:"media"`
}
type Media map[string]string
type Update struct {
Id string `json:"id"`
CreatedAt int `json:"created_at"`
Day string `json:"day"`
DueAt int `json:"due_at"`
DueTime string `json:"due_time"`
Media Media `json:"media"`
ProfileId string `json:"profile_id"`
ProfileService string `json:"profile_service"`
Status string `json:"status"`
Text string `json:"text"`
TextFormatted string `json:"text_formatted"`
UserId string `json:"user_id"`
Via string `json:"via"`
}
type UpdateResponse struct {
Success bool `json:"success"`
BufferCount int `json:"buffer_count"`
BufferPercentage int `json:"buffer_percentage"`
Updates []Update `json:"updates"`
}
type Valuer interface {
UrlValues() url.Values
}
func ClientFactory(token string, transport *oauth.Transport) *Client {
t := &oauth.Token{AccessToken: token}
transport.Token = t
c := Client{AccessToken: token, transport: transport}
return &c
}
func (c *Client) API(method, uri string, data Valuer) (respBody []byte, err error) {
jsonPattern, _ := regexp.Compile(`\.json$`)
if !jsonPattern.Match([]byte(uri)) {
uri += ".json"
}
uri = "https://api.bufferapp.com/1/" + uri
var resp *http.Response
switch method {
case "get":
resp, err = c.transport.Client().Get(uri)
if err != nil {
return nil, err
}
case "post":
var values url.Values
if data == nil {
values = make(url.Values)
} else {
values = data.UrlValues()
}
resp, err = c.transport.Client().PostForm(uri, values)
if err != nil {
return nil, err
}
default:
return nil, errors.New("Not a valid request type")
}
defer resp.Body.Close()
respBody, err = ioutil.ReadAll((*resp).Body)
if resp.StatusCode >= 400 {
return nil, errors.New((*resp).Status)
}
if err != nil {
return nil, err
}
if len(respBody) < 2 {
return nil, errors.New("Malformed JSON response")
}
return respBody, nil
}
func (c *Client) Get(url string) (resp []byte, err error) {
return c.API("get", url, nil)
}
func (c *Client) Post(url string, params Valuer) (resp []byte, err error) {
return c.API("post", url, params)
}
func (c *Client) Profiles() (profiles *[]Profile, err error) {
body, err := c.Get("profiles.json")
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &profiles)
if err != nil {
return nil, err
}
return
}
func (c *Client) Update(update *NewUpdate) (resp *UpdateResponse, err error) {
respBody, err := c.Post("updates/create.json", update)
if err != nil {
return nil, err
}
err = json.Unmarshal(respBody, &resp)
if err != nil {
return nil, err
} else if !resp.Success {
return nil, errors.New(string(respBody))
}
return resp, nil
}
func (u *NewUpdate) UrlValues() (values url.Values) {
values = make(url.Values)
values.Set("text", u.Text)
for key, value := range u.Media {
values.Set("media["+key+"]", value)
}
for _, profile := range u.ProfileIds {
values.Set("profile_ids[]", profile)
}
values.Set("shorten", strconv.FormatBool(u.Shorten))
values.Set("now", strconv.FormatBool(u.Now))
return
}