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
|
package packngo
import "fmt"
const notificationBasePath = "/notifications"
// Notification struct
type Notification struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Body string `json:"body,omitempty"`
Severity string `json:"severity,omitempty"`
Read bool `json:"read,omitempty"`
Context string `json:"context,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
User Href `json:"user,omitempty"`
Href string `json:"href,omitempty"`
}
type notificationsRoot struct {
Notifications []Notification `json:"notifications,omitempty"`
Meta meta `json:"meta,omitempty"`
}
// NotificationService interface defines available event functions
type NotificationService interface {
List(*ListOptions) ([]Notification, *Response, error)
Get(string, *GetOptions) (*Notification, *Response, error)
MarkAsRead(string) (*Notification, *Response, error)
}
// NotificationServiceOp implements NotificationService
type NotificationServiceOp struct {
client *Client
}
// List returns all notifications
func (s *NotificationServiceOp) List(listOpt *ListOptions) ([]Notification, *Response, error) {
return listNotifications(s.client, notificationBasePath, listOpt)
}
// Get returns a notification by ID
func (s *NotificationServiceOp) Get(notificationID string, getOpt *GetOptions) (*Notification, *Response, error) {
params := createGetOptionsURL(getOpt)
path := fmt.Sprintf("%s/%s?%s", notificationBasePath, notificationID, params)
return getNotifications(s.client, path)
}
// Marks notification as read by ID
func (s *NotificationServiceOp) MarkAsRead(notificationID string) (*Notification, *Response, error) {
path := fmt.Sprintf("%s/%s", notificationBasePath, notificationID)
return markAsRead(s.client, path)
}
// list helper function for all notification functions
func listNotifications(client *Client, path string, listOpt *ListOptions) ([]Notification, *Response, error) {
params := createListOptionsURL(listOpt)
root := new(notificationsRoot)
path = fmt.Sprintf("%s?%s", path, params)
resp, err := client.DoRequest("GET", path, nil, root)
if err != nil {
return nil, resp, err
}
return root.Notifications, resp, err
}
func getNotifications(client *Client, path string) (*Notification, *Response, error) {
notification := new(Notification)
resp, err := client.DoRequest("GET", path, nil, notification)
if err != nil {
return nil, resp, err
}
return notification, resp, err
}
func markAsRead(client *Client, path string) (*Notification, *Response, error) {
notification := new(Notification)
resp, err := client.DoRequest("PUT", path, nil, notification)
if err != nil {
return nil, resp, err
}
return notification, resp, err
}
|