-
Notifications
You must be signed in to change notification settings - Fork 31
/
device.go
72 lines (57 loc) · 1.77 KB
/
device.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
package stream_chat // nolint: golint
import (
"errors"
"net/http"
"net/url"
)
const (
PushProviderAPNS = pushProvider("apn")
PushProviderFirebase = pushProvider("firebase")
)
type pushProvider = string
type Device struct {
ID string `json:"id"` // The device ID.
UserID string `json:"user_id"` // The user ID for this device.
PushProvider pushProvider `json:"push_provider"` // The push provider for this device. One of constants PushProvider*
}
type devicesResponse struct {
Devices []*Device `json:"devices"`
}
// GetDevices retrieves the list of devices for user.
func (c *Client) GetDevices(userID string) (devices []*Device, err error) {
if userID == "" {
return nil, errors.New("user ID is empty")
}
params := url.Values{}
params.Set("user_id", userID)
var resp devicesResponse
err = c.makeRequest(http.MethodGet, "devices", params, nil, &resp)
return resp.Devices, err
}
// AddDevice adds new device.
func (c *Client) AddDevice(device *Device) error {
switch {
case device == nil:
return errors.New("device is nil")
case device.ID == "":
return errors.New("device ID is empty")
case device.UserID == "":
return errors.New("device user ID is empty")
case device.PushProvider == "":
return errors.New("device push provider is empty")
}
return c.makeRequest(http.MethodPost, "devices", nil, device, nil)
}
// DeleteDevice deletes a device from the user.
func (c *Client) DeleteDevice(userID, deviceID string) error {
switch {
case userID == "":
return errors.New("user ID is empty")
case deviceID == "":
return errors.New("device ID is empty")
}
params := url.Values{}
params.Set("id", deviceID)
params.Set("user_id", userID)
return c.makeRequest(http.MethodDelete, "devices", params, nil, nil)
}