forked from jon4hz/go-btcpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
103 lines (95 loc) · 3.33 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package btcpay
import (
"context"
"encoding/json"
"fmt"
)
func (c *Client) GetNotifications(ctx context.Context, seen ...bool) ([]*NotificationResponse, int, error) {
var endpoint string
var dataRes []*NotificationResponse
if len(seen) > 0 {
endpoint = fmt.Sprintf("%s/api/v1/users/me/notifications?%t", c.URL, seen[0])
} else {
endpoint = fmt.Sprintf("%s/api/v1/users/me/notifications", c.URL)
}
statusCode, err := c.doRequest(ctx, endpoint, "GET", &dataRes, nil)
if err != nil {
return nil, statusCode, err
}
return dataRes, statusCode, nil
}
// View information about the specified notification
func (c *Client) GetNotification(ctx context.Context, notificationID *NotificationID) (*NotificationResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/users/me/notifications/%s", c.URL, *notificationID)
var dataRes NotificationResponse
statusCode, err := c.doRequest(ctx, endpoint, "GET", &dataRes, nil)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
func (n *Notification) GetNotification(ctx context.Context) (*NotificationResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/users/me/notifications/%s", n.Client.URL, n.ID)
var dataRes NotificationResponse
statusCode, err := n.Client.doRequest(ctx, endpoint, "GET", &dataRes, nil)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
// Updates the notification
func (c *Client) UpdateNotification(ctx context.Context, notificationID *NotificationID, updateNotification ...*UpdateNotification) (*NotificationResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/users/me/notifications/%s", c.URL, *notificationID)
var dataReq []byte
var err error
if len(updateNotification) > 0 {
dataReq, err = json.Marshal(updateNotification[0])
} else {
dataReq = []byte(`{"seen":null}`)
}
if err != nil {
return nil, 0, err
}
var dataRes NotificationResponse
statusCode, err := c.doRequest(ctx, endpoint, "PUT", &dataRes, dataReq)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
func (n *Notification) UpdateNotification(ctx context.Context, updateNotification ...*UpdateNotification) (*NotificationResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/users/me/notifications/%s", n.Client.URL, n.ID)
var dataReq []byte
var err error
if len(updateNotification) > 0 {
dataReq, err = json.Marshal(updateNotification[0])
} else {
dataReq = []byte(`{"seen":null}`)
}
if err != nil {
return nil, 0, err
}
var dataRes NotificationResponse
statusCode, err := n.Client.doRequest(ctx, endpoint, "PUT", &dataRes, dataReq)
if err != nil {
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
// Removes the specified notification.
func (c *Client) RemoveNotification(ctx context.Context, notificationID *NotificationID) (int, error) {
endpoint := fmt.Sprintf("%s/api/v1/users/me/notifications/%s", c.URL, *notificationID)
statusCode, err := c.doRequest(ctx, endpoint, "DELETE", nil, nil)
if err != nil {
return statusCode, err
}
return statusCode, nil
}
func (n *Notification) RemoveNotification(ctx context.Context) (int, error) {
endpoint := fmt.Sprintf("%s/api/v1/users/me/notifications/%s", n.Client.URL, n.ID)
statusCode, err := n.Client.doRequest(ctx, endpoint, "DELETE", nil, nil)
if err != nil {
return statusCode, err
}
return statusCode, nil
}