This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
notifications.go
63 lines (53 loc) · 2.19 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
package aftership
import (
"context"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// Notification is the model describing an AfterShip notification
type Notification struct {
Emails []string `json:"emails"`
SMSes []string `json:"smses"`
}
// notificationWrapper is the notification wrapper.
type notificationWrapper struct {
Notification Notification `json:"notification"`
}
// GetNotification gets contact information for the users to notify when the tracking changes.
// Please note that only customer receivers will be returned.
// Any email, sms or webhook that belongs to the Store will not be returned.
func (client *Client) GetNotification(ctx context.Context, identifier TrackingIdentifier) (Notification, error) {
uriPath, err := identifier.URIPath()
if err != nil {
return Notification{}, errors.Wrap(err, "error getting notification")
}
uriPath = fmt.Sprintf("/notifications%s", uriPath)
var wrapper notificationWrapper
err = client.makeRequest(ctx, http.MethodGet, uriPath, nil, nil, &wrapper)
return wrapper.Notification, err
}
// AddNotification adds notifications to a single tracking.
func (client *Client) AddNotification(ctx context.Context, identifier TrackingIdentifier, notification Notification) (Notification, error) {
uriPath, err := identifier.URIPath()
if err != nil {
return Notification{}, errors.Wrap(err, "error adding notification")
}
uriPath = fmt.Sprintf("/notifications%s/add", uriPath)
var wrapper notificationWrapper
err = client.makeRequest(ctx, http.MethodPost, uriPath, nil,
¬ificationWrapper{Notification: notification}, &wrapper)
return wrapper.Notification, err
}
// RemoveNotification removes notifications from a single tracking.
func (client *Client) RemoveNotification(ctx context.Context, identifier TrackingIdentifier, notification Notification) (Notification, error) {
uriPath, err := identifier.URIPath()
if err != nil {
return Notification{}, errors.Wrap(err, "error removing notification")
}
uriPath = fmt.Sprintf("/notifications%s/remove", uriPath)
var wrapper notificationWrapper
err = client.makeRequest(ctx, http.MethodPost, uriPath, nil,
¬ificationWrapper{Notification: notification}, &wrapper)
return wrapper.Notification, err
}