forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_parsing.go
75 lines (59 loc) · 1.64 KB
/
response_parsing.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
package pusher
import (
"encoding/json"
)
// Channel represents the information about a channel from the Pusher API.
type Channel struct {
Name string
Occupied bool `json:"occupied,omitempty"`
UserCount int `json:"user_count,omitempty"`
SubscriptionCount int `json:"subscription_count,omitempty"`
}
// ChannelsList represents a list of channels received by the Pusher API.
type ChannelsList struct {
Channels map[string]ChannelListItem `json:"channels"`
}
// ChannelListItem represents an item within ChannelsList
type ChannelListItem struct {
UserCount int `json:"user_count"`
}
// Users represents a list of users in a presence-channel
type Users struct {
List []User `json:"users"`
}
// User represents a user and contains their ID.
type User struct {
ID string `json:"id"`
}
/*
MemberData represents what to assign to a channel member, consisting of a
`UserID` and any custom `UserInfo`.
*/
type MemberData struct {
UserID string `json:"user_id"`
UserInfo map[string]string `json:"user_info,omitempty"`
}
func unmarshalledChannelsList(response []byte) (*ChannelsList, error) {
channels := &ChannelsList{}
err := json.Unmarshal(response, &channels)
if err != nil {
return nil, err
}
return channels, nil
}
func unmarshalledChannel(response []byte, name string) (*Channel, error) {
channel := &Channel{Name: name}
err := json.Unmarshal(response, &channel)
if err != nil {
return nil, err
}
return channel, nil
}
func unmarshalledChannelUsers(response []byte) (*Users, error) {
users := &Users{}
err := json.Unmarshal(response, &users)
if err != nil {
return nil, err
}
return users, nil
}