-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifications.go
78 lines (66 loc) · 2.63 KB
/
notifications.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
package client
import (
"fmt"
"net/http"
)
// Notification describes a Notification schema object
type Notification struct {
ID int `json:"id"`
Type string `json:"type"`
Always bool `json:"always"`
Web bool `json:"web"`
Mail bool `json:"mail"`
SMS bool `json:"sms"`
CalendarID int `json:"calendarId"`
Attributes Attributes `json:"attributes"`
}
// NotificationType describes a NotificationType schema object
type NotificationType struct {
Type string `json:"type"`
}
// ListNotifications fetches a list of Notifications
// Without params, it returns a list of Notifications the user has access to
//
// Params:
// WithAll - Can only be used by admins or managers to fetch all entities
// WithUserID - Standard users can use this only with their own userId
// WithDeviceID - Standard users can use this only with _deviceId_s, they have access to
// WithGroupID - Standard users can use this only with _groupId_s, they have access to
// Refresh
func (c *Client) ListNotifications(params ...QueryParameter) (notifications []Notification, err error) {
err = c.doRequest(http.MethodGet, "notifications"+query(params), nil, ¬ifications)
return
}
// CreateNotification creates a Notification
func (c *Client) CreateNotification(notification Notification) (Notification, error) {
return c.doNotification(http.MethodPost, "notifications", notification)
}
// TestNotification sends test notification to current user via Email and SMS
//
// Errors:
// 400 Bad Request - Could happen if sending has failed
func (c Client) TestNotification() error {
return c.doRequest(http.MethodPost, "notifications/test", nil, nil)
}
// ListNotificationTypes fetches a list of available Notification types
func (c Client) ListNotificationTypes() (notificationTypes []NotificationType, err error) {
err = c.doRequest(http.MethodGet, "notifications/types", nil, ¬ificationTypes)
return
}
// DeleteNotification deletes a notification
func (c *Client) DeleteNotification(id int) error {
return c.doRequest(http.MethodDelete, fmt.Sprintf("notifications/%d", id), nil, nil)
}
// UpdateNotification updates a notification
func (c *Client) UpdateNotification(id int, notification Notification) (Notification, error) {
notification.ID = id
return c.doNotification(http.MethodPut, fmt.Sprintf("notifications/%d", id), notification)
}
func (c *Client) doNotification(method, path string, notification Notification) (Notification, error) {
body, err := jsonBody(notification)
if err != nil {
return notification, err
}
err = c.doRequest(method, path, body, ¬ification)
return notification, err
}