-
Notifications
You must be signed in to change notification settings - Fork 31
/
client.go
301 lines (238 loc) · 6.29 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package stream_chat // nolint: golint
import (
"bytes"
"crypto"
"crypto/hmac"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
const (
defaultBaseURL = "https://chat-us-east-1.stream-io-api.com"
defaultTimeout = 6 * time.Second
)
type Client struct {
BaseURL string
HTTP *http.Client `json:"-"`
apiKey string
apiSecret []byte
authToken string
}
func (c *Client) setHeaders(r *http.Request) {
r.Header.Set("Content-Type", "application/json")
r.Header.Set("X-Stream-Client", versionHeader())
r.Header.Set("Authorization", c.authToken)
r.Header.Set("Stream-Auth-Type", "jwt")
}
func (c *Client) parseResponse(resp *http.Response, result interface{}) error {
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode >= 399 {
msg, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("chat-client: HTTP %s %s status %s: %s",
resp.Request.Method, resp.Request.URL, resp.Status, string(msg))
}
if result != nil {
return json.NewDecoder(resp.Body).Decode(result)
}
return nil
}
func (c *Client) requestURL(path string, values url.Values) (string, error) {
_url, err := url.Parse(c.BaseURL + "/" + path)
if err != nil {
return "", errors.New("url.Parse: " + err.Error())
}
if values == nil {
values = make(url.Values)
}
values.Add("api_key", c.apiKey)
_url.RawQuery = values.Encode()
return _url.String(), nil
}
func (c *Client) newRequest(method, path string, params url.Values, data interface{}) (*http.Request, error) {
_url, err := c.requestURL(path, params)
if err != nil {
return nil, err
}
r, err := http.NewRequest(method, _url, nil)
if err != nil {
return nil, err
}
c.setHeaders(r)
switch t := data.(type) {
case nil:
r.Body = nil
case io.ReadCloser:
r.Body = t
case io.Reader:
r.Body = ioutil.NopCloser(t)
default:
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
r.Body = ioutil.NopCloser(bytes.NewReader(b))
}
return r, nil
}
func (c *Client) makeRequest(method, path string, params url.Values, data, result interface{}) error {
r, err := c.newRequest(method, path, params, data)
if err != nil {
return err
}
resp, err := c.HTTP.Do(r)
if err != nil {
return err
}
return c.parseResponse(resp, result)
}
// CreateToken creates a new token for user with optional expire time.
// Zero time is assumed to be no expire.
func (c *Client) CreateToken(userID string, expire time.Time) (string, error) {
if userID == "" {
return "", errors.New("user ID is empty")
}
claims := jwt.MapClaims{
"user_id": userID,
}
if !expire.IsZero() {
claims["exp"] = expire.Unix()
}
return c.createToken(claims)
}
func (c *Client) createToken(claims jwt.Claims) (string, error) {
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(c.apiSecret)
}
// VerifyWebhook validates if hmac signature is correct for message body.
func (c *Client) VerifyWebhook(body, signature []byte) (valid bool) {
mac := hmac.New(crypto.SHA256.New, c.apiSecret)
_, _ = mac.Write(body)
expectedMAC := hex.EncodeToString(mac.Sum(nil))
return bytes.Equal(signature, []byte(expectedMAC))
}
type sendFileResponse struct {
File string `json:"file"`
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
// this makes possible to set content type.
type multipartForm struct {
*multipart.Writer
}
// CreateFormFile is a convenience wrapper around CreatePart. It creates
// a new form-data header with the provided field name, file name and content type.
func (form *multipartForm) CreateFormFile(fieldName, filename, contentType string) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldName), escapeQuotes(filename)))
if contentType == "" {
contentType = "application/octet-stream"
}
h.Set("Content-Type", contentType)
return form.Writer.CreatePart(h)
}
func (form *multipartForm) setData(fieldName string, data interface{}) error {
field, err := form.CreateFormField(fieldName)
if err != nil {
return err
}
return json.NewEncoder(field).Encode(data)
}
func (form *multipartForm) setFile(fieldName string, r io.Reader, fileName, contentType string) error {
file, err := form.CreateFormFile(fieldName, fileName, contentType)
if err != nil {
return err
}
_, err = io.Copy(file, r)
return err
}
func (c *Client) sendFile(link string, opts SendFileRequest) (string, error) {
if opts.User == nil {
return "", errors.New("user is nil")
}
tmpfile, err := ioutil.TempFile("", opts.FileName)
if err != nil {
return "", err
}
defer func() {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
}()
form := multipartForm{multipart.NewWriter(tmpfile)}
if err := form.setData("user", opts.User); err != nil {
return "", err
}
err = form.setFile("file", opts.Reader, opts.FileName, opts.ContentType)
if err != nil {
return "", err
}
err = form.Close()
if err != nil {
return "", err
}
if _, err = tmpfile.Seek(0, 0); err != nil {
return "", err
}
r, err := c.newRequest(http.MethodPost, link, nil, tmpfile)
if err != nil {
return "", err
}
r.Header.Set("Content-Type", form.FormDataContentType())
res, err := c.HTTP.Do(r)
if err != nil {
return "", err
}
var resp sendFileResponse
err = c.parseResponse(res, &resp)
if err != nil {
return "", err
}
return resp.File, err
}
// NewClient creates new stream chat api client.
func NewClient(apiKey, apiSecret string) (*Client, error) {
switch {
case apiKey == "":
return nil, errors.New("API key is empty")
case apiSecret == "":
return nil, errors.New("API secret is empty")
}
client := &Client{
apiKey: apiKey,
apiSecret: []byte(apiSecret),
BaseURL: defaultBaseURL,
HTTP: &http.Client{
Timeout: defaultTimeout,
},
}
token, err := client.createToken(jwt.MapClaims{"server": true})
if err != nil {
return nil, err
}
client.authToken = token
return client, nil
}
// Channel prepares a Channel object for future API calls. This does not in and
// of itself call the API.
func (c *Client) Channel(channelType, channelID string) *Channel {
return &Channel{
client: c,
ID: channelID,
Type: channelType,
}
}