forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
450 lines (365 loc) · 13.2 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package pusher
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
)
var pusherPathRegex = regexp.MustCompile("^/apps/([0-9]+)$")
var maxTriggerableChannels = 100
/*
Client to the HTTP API of Pusher.
There easiest way to configure the library is by creating a `Pusher` instance:
client := pusher.Client{
AppID: "your_app_id",
Key: "your_app_key",
Secret: "your_app_secret",
}
To ensure requests occur over HTTPS, set the `Encrypted` property of a
`pusher.Client` to `true`.
client.Secure = true // false by default
If you wish to set a time-limit for each HTTP request, set the `Timeout`
property to an instance of `time.Duration`, for example:
client.Timeout = time.Second * 3 // 5 seconds by default
Changing the `pusher.Client`'s `Host` property will make sure requests are sent
to your specified host.
client.Host = "foo.bar.com" // by default this is "api.pusherapp.com".
*/
type Client struct {
AppID string
Key string
Secret string
Host string // host or host:port pair
Secure bool // true for HTTPS
Cluster string
HTTPClient *http.Client
}
/*
ClientFromURL allows client instantiation from a specially-crafted Pusher URL.
c := pusher.ClientFromURL("http://key:[email protected]/apps/app_id")
*/
func ClientFromURL(serverURL string) (*Client, error) {
url2, err := url.Parse(serverURL)
if err != nil {
return nil, err
}
c := Client{
Host: url2.Host,
}
matches := pusherPathRegex.FindStringSubmatch(url2.Path)
if len(matches) == 0 {
return nil, errors.New("No app ID found")
}
c.AppID = matches[1]
if url2.User == nil {
return nil, errors.New("Missing <key>:<secret>")
}
c.Key = url2.User.Username()
var isSet bool
c.Secret, isSet = url2.User.Password()
if !isSet {
return nil, errors.New("Missing <secret>")
}
if url2.Scheme == "https" {
c.Secure = true
}
return &c, nil
}
/*
ClientFromEnv allows instantiation of a client from an environment variable.
This is particularly relevant if you are using Pusher as a Heroku add-on,
which stores credentials in a `"PUSHER_URL"` environment variable. For example:
client := pusher.ClientFromEnv("PUSHER_URL")
*/
func ClientFromEnv(key string) (*Client, error) {
url := os.Getenv(key)
return ClientFromURL(url)
}
/*
Returns the underlying HTTP client.
Useful to set custom properties to it.
*/
func (c *Client) requestClient() *http.Client {
if c.HTTPClient == nil {
c.HTTPClient = &http.Client{Timeout: time.Second * 5}
}
return c.HTTPClient
}
func (c *Client) request(method, url string, body []byte) ([]byte, error) {
return request(c.requestClient(), method, url, body)
}
/*
Trigger triggers an event to the Pusher API.
It is possible to trigger an event on one or more channels. Channel names can
contain only characters which are alphanumeric, `_` or `-`` and have
to be at most 200 characters long. Event name can be at most 200 characters long too.
Pass in the channel's name, the event's name, and a data payload. The data payload must
be marshallable into JSON.
data := map[string]string{"hello": "world"}
client.Trigger("greeting_channel", "say_hello", data)
*/
func (c *Client) Trigger(channel string, eventName string, data interface{}) error {
return c.trigger([]string{channel}, eventName, data, nil)
}
/*
TriggerMulti is the same as `client.Trigger`, except one passes in a slice of
`channels` as the first parameter. The maximum length of channels is 100.
client.TriggerMulti([]string{"a_channel", "another_channel"}, "event", data)
*/
func (c *Client) TriggerMulti(channels []string, eventName string, data interface{}) error {
return c.trigger(channels, eventName, data, nil)
}
/*
TriggerExclusive triggers an event excluding a recipient whose connection has
the `socket_id` you specify here from receiving the event.
You can read more here: http://pusher.com/docs/duplicates.
client.TriggerExclusive("a_channel", "event", data, "123.12")
*/
func (c *Client) TriggerExclusive(channel string, eventName string, data interface{}, socketID string) error {
return c.trigger([]string{channel}, eventName, data, &socketID)
}
/*
TriggerMultiExclusive triggers an event to multiple channels excluding a
recipient whose connection has the `socket_id` you specify here from receiving
the event on any of the channels.
client.TriggerMultiExclusive([]string{"a_channel", "another_channel"}, "event", data, "123.12")
*/
func (c *Client) TriggerMultiExclusive(channels []string, eventName string, data interface{}, socketID string) error {
return c.trigger(channels, eventName, data, &socketID)
}
func (c *Client) trigger(channels []string, eventName string, data interface{}, socketID *string) error {
if len(channels) > maxTriggerableChannels {
return fmt.Errorf("You cannot trigger on more than %d channels at once", maxTriggerableChannels)
}
if !channelsAreValid(channels) {
return errors.New("At least one of your channels' names are invalid")
}
if err := validateSocketID(socketID); err != nil {
return err
}
payload, err := encodeTriggerBody(channels, eventName, data, socketID)
if err != nil {
return err
}
path := fmt.Sprintf("/apps/%s/events", c.AppID)
triggerURL, err := createRequestURL("POST", c.Host, path, c.Key, c.Secret, authTimestamp(), c.Secure, payload, nil, c.Cluster)
if err != nil {
return err
}
_, err = c.request("POST", triggerURL, payload)
return err
}
/*
Event stores all the data for one Event that can be triggered.
*/
type Event struct {
Channel string
Name string
Data interface{}
SocketID *string
}
/*
TriggerBatch triggers multiple events on multiple channels in a single call:
client.TriggerBatch([]pusher.Event{
{ Channel: "donut-1", Name: "ev1", Data: "d1" },
})
*/
func (c *Client) TriggerBatch(batch []Event) error {
// validate every channel name and every sockedID (if present) in batch
for _, event := range batch {
if !validChannel(event.Channel) {
return fmt.Errorf("The channel named %s has a non-valid name", event.Channel)
}
if err := validateSocketID(event.SocketID); err != nil {
return err
}
}
payload, err := encodeTriggerBatchBody(batch)
if err != nil {
return err
}
path := fmt.Sprintf("/apps/%s/batch_events", c.AppID)
triggerURL, err := createRequestURL("POST", c.Host, path, c.Key, c.Secret, authTimestamp(), c.Secure, payload, nil, c.Cluster)
if err != nil {
return err
}
_, err = c.request("POST", triggerURL, payload)
return err
}
/*
Channels returns a list of all the channels in an application. The parameter
`additionalQueries` is a map with query options. A key with `"filter_by_prefix"`
will filter the returned channels. To get number of users subscribed to a
presence-channel, specify an `"info"` key with value `"user_count"`. Pass in
`nil` if you do not wish to specify any query attributes
channelsParams := map[string]string{
"filter_by_prefix": "presence-",
"info": "user_count",
}
channels, err := client.Channels(channelsParams)
//channels=> &{Channels:map[presence-chatroom:{UserCount:4} presence-notifications:{UserCount:31} ]}
*/
func (c *Client) Channels(additionalQueries map[string]string) (*ChannelsList, error) {
path := fmt.Sprintf("/apps/%s/channels", c.AppID)
u, err := createRequestURL("GET", c.Host, path, c.Key, c.Secret, authTimestamp(), c.Secure, nil, additionalQueries, c.Cluster)
if err != nil {
return nil, err
}
response, err := c.request("GET", u, nil)
if err != nil {
return nil, err
}
return unmarshalledChannelsList(response)
}
/*
Channel allows you to get the state of a single channel. The parameter
`additionalQueries` is a map with query options. An `"info"` key can have
comma-separated vales of `"user_count"`, for presence-channels, and
`"subscription_count"`, for all-channels. Note that the subscription count is
not allowed by default. Please contact us at http://support.pusher.com if you
wish to enable this. Pass in `nil` if you do not wish to specify any query
attributes.
channelParams := map[string]string{
"info": "user_count,subscription_count",
}
channel, err := client.Channel("presence-chatroom", channelParams)
//channel=> &{Name:presence-chatroom Occupied:true UserCount:42 SubscriptionCount:42}
*/
func (c *Client) Channel(name string, additionalQueries map[string]string) (*Channel, error) {
path := fmt.Sprintf("/apps/%s/channels/%s", c.AppID, name)
u, err := createRequestURL("GET", c.Host, path, c.Key, c.Secret, authTimestamp(), c.Secure, nil, additionalQueries, c.Cluster)
if err != nil {
return nil, err
}
response, err := c.request("GET", u, nil)
if err != nil {
return nil, err
}
return unmarshalledChannel(response, name)
}
/*
GetChannelUsers returns a list of users in a presence-channel by passing to this
method the channel name.
users, err := client.GetChannelUsers("presence-chatroom")
//users=> &{List:[{ID:13} {ID:90}]}
*/
func (c *Client) GetChannelUsers(name string) (*Users, error) {
path := fmt.Sprintf("/apps/%s/channels/%s/users", c.AppID, name)
u, err := createRequestURL("GET", c.Host, path, c.Key, c.Secret, authTimestamp(), c.Secure, nil, nil, c.Cluster)
if err != nil {
return nil, err
}
response, err := c.request("GET", u, nil)
if err != nil {
return nil, err
}
return unmarshalledChannelUsers(response)
}
/*
AuthenticatePrivateChannel allows you to authenticate a users subscription to a
private channel. It returns authentication signature to send back to the client
and authorize them.
For more information see our docs: http://pusher.com/docs/authenticating_users.
This is an example of authenticating a private-channel, using the built-in
Golang HTTP library to start a server.
In order to authorize a client, one must read the response into type `[]byte`
and pass it in. This will return a signature in the form of a `[]byte` for you
to send back to the client.
func pusherAuth(res http.ResponseWriter, req *http.Request) {
params, _ := ioutil.ReadAll(req.Body)
response, err := client.AuthenticatePrivateChannel(params)
if err != nil {
panic(err)
}
fmt.Fprintf(res, string(response))
}
func main() {
http.HandleFunc("/pusher/auth", pusherAuth)
http.ListenAndServe(":5000", nil)
}
*/
func (c *Client) AuthenticatePrivateChannel(params []byte) (response []byte, err error) {
return c.authenticateChannel(params, nil)
}
/*
AuthenticatePresenceChannel allows you to authenticate a users subscription to a
presence channel. It returns authentication signature to send back to the client
and authorize them. In order to identify a user, clients are sent a user_id and,
optionally, custom data.
In this library, one does this by passing a `pusher.MemberData` instance.
params, _ := ioutil.ReadAll(req.Body)
presenceData := pusher.MemberData{
UserID: "1",
UserInfo: map[string]string{
"twitter": "jamiepatel",
},
}
response, err := client.AuthenticatePresenceChannel(params, presenceData)
if err != nil {
panic(err)
}
fmt.Fprintf(res, response)
*/
func (c *Client) AuthenticatePresenceChannel(params []byte, member MemberData) (response []byte, err error) {
return c.authenticateChannel(params, &member)
}
func (c *Client) authenticateChannel(params []byte, member *MemberData) (response []byte, err error) {
channelName, socketID, err := parseAuthRequestParams(params)
if err != nil {
return
}
if err = validateSocketID(&socketID); err != nil {
return
}
stringToSign := strings.Join([]string{socketID, channelName}, ":")
var jsonUserData string
if member != nil {
var _jsonUserData []byte
_jsonUserData, err = json.Marshal(member)
if err != nil {
return
}
jsonUserData = string(_jsonUserData)
stringToSign = strings.Join([]string{stringToSign, jsonUserData}, ":")
}
_response := createAuthMap(c.Key, c.Secret, stringToSign)
if member != nil {
_response["channel_data"] = jsonUserData
}
response, err = json.Marshal(_response)
return
}
/*
Webhook allows you to check that a Webhook you receive is indeed from Pusher, by
checking the token and authentication signature in the header of the request. On
your dashboard at http://app.pusher.com, you can set up webhooks to POST a
payload to your server after certain events. Such events include channels being
occupied or vacated, members being added or removed in presence-channels, or
after client-originated events. For more information see
https://pusher.com/docs/webhooks.
If the webhook is valid, a `*pusher.Webhook* will be returned, and the `err`
value will be nil. If it is invalid, the first return value will be nil, and an
error will be passed.
func pusherWebhook(res http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body)
webhook, err := client.Webhook(req.Header, body)
if err != nil {
fmt.Println("Webhook is invalid :(")
} else {
fmt.Printf("%+v\n", webhook.Events)
}
}
*/
func (c *Client) Webhook(header http.Header, body []byte) (*Webhook, error) {
for _, token := range header["X-Pusher-Key"] {
if token == c.Key && checkSignature(header.Get("X-Pusher-Signature"), c.Secret, body) {
return unmarshalledWebhook(body)
}
}
return nil, errors.New("Invalid webhook")
}