forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
80 lines (66 loc) · 2.22 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
74
75
76
77
78
79
80
package stream_chat
import (
"context"
"errors"
"net/http"
"net/url"
)
const (
PushProviderAPNS = PushProviderType("apn")
PushProviderFirebase = PushProviderType("firebase")
PushProviderXiaomi = PushProviderType("xiaomi")
PushProviderHuawei = PushProviderType("huawei")
)
type PushProviderType = string
type Device struct {
ID string `json:"id"` // The device ID.
UserID string `json:"user_id"` // The user ID for this device.
PushProvider PushProviderType `json:"push_provider"` // The push provider for this device. One of constants PushProvider*
PushProviderName string `json:"push_provider_name"` // The push provider name for this device.
}
type DevicesResponse struct {
Devices []*Device `json:"devices"`
Response
}
// GetDevices retrieves the list of devices for user.
func (c *Client) GetDevices(ctx context.Context, userID string) (*DevicesResponse, 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(ctx, http.MethodGet, "devices", params, nil, &resp)
return &resp, err
}
// AddDevice adds new device.
func (c *Client) AddDevice(ctx context.Context, device *Device) (*Response, error) {
switch {
case device == nil:
return nil, errors.New("device is nil")
case device.ID == "":
return nil, errors.New("device ID is empty")
case device.UserID == "":
return nil, errors.New("device user ID is empty")
case device.PushProvider == "":
return nil, errors.New("device push provider is empty")
}
var resp Response
err := c.makeRequest(ctx, http.MethodPost, "devices", nil, device, &resp)
return &resp, err
}
// DeleteDevice deletes a device from the user.
func (c *Client) DeleteDevice(ctx context.Context, userID, deviceID string) (*Response, error) {
switch {
case userID == "":
return nil, errors.New("user ID is empty")
case deviceID == "":
return nil, errors.New("device ID is empty")
}
params := url.Values{}
params.Set("id", deviceID)
params.Set("user_id", userID)
var resp Response
err := c.makeRequest(ctx, http.MethodDelete, "devices", params, nil, &resp)
return &resp, err
}