-
Notifications
You must be signed in to change notification settings - Fork 0
/
esa.go
211 lines (194 loc) · 4.77 KB
/
esa.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
//
type EsaClient struct {
endpoint string
teamName string
token string
http *http.Client
}
//
func NewEsaClient(teamName, token string) (*EsaClient, error) {
if teamName == "" {
return nil, errors.New("teamName is required")
}
if token == "" {
return nil, errors.New("token is required")
}
return &EsaClient{
endpoint: "https://api.esa.io/v1/teams/" + teamName,
teamName: teamName,
token: token,
http: &http.Client{},
}, nil
}
type QueryOption func(*url.URL) error
func QueryOptionPage(value int) QueryOption {
return func(in *url.URL) error {
if value < 1 {
return fmt.Errorf("invalid page query: %d", value)
}
q := in.Query()
q.Set("page", fmt.Sprintf("%d", value))
in.RawQuery = q.Encode()
return nil
}
}
func QueryOptionPerPage(value int) QueryOption {
return func(in *url.URL) error {
if value < 1 && 100 < value {
return fmt.Errorf("invalid per page query: %d", value)
}
q := in.Query()
q.Set("per_page", fmt.Sprintf("%d", value))
in.RawQuery = q.Encode()
return nil
}
}
func QueryOptionSort(value string) QueryOption {
return func(in *url.URL) error {
if value == "" {
return fmt.Errorf("invalid sort query: %s", value)
}
q := in.Query()
q.Set("sort", value)
in.RawQuery = q.Encode()
return nil
}
}
func QueryOptionOrder(value string) QueryOption {
return func(in *url.URL) error {
if value != "asc" && value != "desc" {
return fmt.Errorf("invalid order query: %s", value)
}
q := in.Query()
q.Set("order", value)
in.RawQuery = q.Encode()
return nil
}
}
func buildURL(raw string, options ...QueryOption) (string, error) {
u, err := url.Parse(raw)
if err != nil {
return "", err
}
for _, opt := range options {
if err := opt(u); err != nil {
return "", err
}
}
return u.String(), nil
}
//
func (c *EsaClient) GetTeamName() string {
return c.teamName
}
//
func (c *EsaClient) InviteAccount(email string) error {
url, err := buildURL(c.endpoint + "/invitations")
if err != nil {
return err
}
body := bytes.NewBuffer([]byte("{\"member\":{\"emails\":[\"" + email + "\"]}}"))
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json")
res, err := c.http.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode < 200 || 300 <= res.StatusCode {
return fmt.Errorf("invalid status code: %d", res.StatusCode)
}
return nil
}
//
func (c *EsaClient) DeleteAccount(screenName string) error {
url, err := buildURL(c.endpoint + "/members/" + screenName)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.token)
res, err := c.http.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == 404 {
return fmt.Errorf("status code is 404, the specified account (%s) has already been deleted", screenName)
}
if res.StatusCode < 200 || 300 <= res.StatusCode {
return fmt.Errorf("invalid status code: %d", res.StatusCode)
}
return nil
}
type ListAccountResponse struct {
Members []*Member `json:"members"`
PrevPage int `json:"prev_page"`
NextPage int `json:"next_page"`
TotalCount int `json:"total_count"`
Page int `json:"page"`
PerPage int `json:"per_page"`
MaxPerPage int `json:"max_per_page"`
}
type Member struct {
Name string `json:"name"`
ScreenName string `json:"screen_name"`
Icon string `json:"icon"`
PostsCount int `json:"posts_count"`
JoinedAt string `json:"joined_at"`
LastAccessedAt string `json:"last_accessed_at"`
Email string `json:"email"`
}
func (m *Member) LastAccessedTime() (time.Time, error) {
return time.Parse(time.RFC3339, m.LastAccessedAt)
}
func (m *Member) JoinedTime() (time.Time, error) {
return time.Parse(time.RFC3339, m.JoinedAt)
}
//
func (c *EsaClient) ListAccount(options ...QueryOption) (*ListAccountResponse, error) {
url, err := buildURL(c.endpoint+"/members", options...)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.token)
res, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode < 200 || 300 <= res.StatusCode {
return nil, fmt.Errorf("invalid status code: %d", res.StatusCode)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var ret *ListAccountResponse
if err := json.Unmarshal(data, &ret); err != nil {
return nil, err
}
return ret, nil
}