This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
73 lines (58 loc) · 1.71 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
73
package stream_chat
import (
"errors"
"net/http"
)
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"`
}
// Get 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 := map[string][]string{
"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)
}
// Delete a device from the user
func (c *Client) DeleteDevice(userID string, deviceID string) error {
switch {
case userID == "":
return errors.New("user ID is empty")
case deviceID == "":
return errors.New("device ID is empty")
}
params := map[string][]string{
"id": {deviceID},
"user_id": {userID},
}
return c.makeRequest(http.MethodDelete, "devices", params, nil, nil)
}