-
Notifications
You must be signed in to change notification settings - Fork 63
/
add_channels_to_push_request.go
156 lines (122 loc) · 4.65 KB
/
add_channels_to_push_request.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package pubnub
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/pubnub/go/v7/utils"
)
const addChannelsToPushPath = "/v1/push/sub-key/%s/devices/%s"
const addChannelsToPushPathAPNS2 = "/v2/push/sub-key/%s/devices-apns2/%s"
var emptyAddPushNotificationsOnChannelsResponse *AddPushNotificationsOnChannelsResponse
// addPushNotificationsOnChannelsBuilder provides a builder to add Push Notifications on channels
type addPushNotificationsOnChannelsBuilder struct {
opts *addChannelsToPushOpts
}
func newAddPushNotificationsOnChannelsBuilder(pubnub *PubNub) *addPushNotificationsOnChannelsBuilder {
return newAddPushNotificationsOnChannelsBuilderWithContext(pubnub, pubnub.ctx)
}
func newAddPushNotificationsOnChannelsBuilderWithContext(pubnub *PubNub, context Context) *addPushNotificationsOnChannelsBuilder {
return &addPushNotificationsOnChannelsBuilder{
opts: newAddChannelsToPushOpts(pubnub, context, addChannelsToPushOpts{}),
}
}
// Channels sets the channels to enable Push Notifications
func (b *addPushNotificationsOnChannelsBuilder) Channels(channels []string) *addPushNotificationsOnChannelsBuilder {
b.opts.Channels = channels
return b
}
// PushType set the type of Push: GCM, APNS, MPNS
func (b *addPushNotificationsOnChannelsBuilder) PushType(pushType PNPushType) *addPushNotificationsOnChannelsBuilder {
b.opts.PushType = pushType
return b
}
// DeviceIDForPush sets the device of for Push Notifcataions
func (b *addPushNotificationsOnChannelsBuilder) DeviceIDForPush(deviceID string) *addPushNotificationsOnChannelsBuilder {
b.opts.DeviceIDForPush = deviceID
return b
}
// Topic sets the topic of for APNS2 Push Notifcataions
func (b *addPushNotificationsOnChannelsBuilder) Topic(topic string) *addPushNotificationsOnChannelsBuilder {
b.opts.Topic = topic
return b
}
// Environment sets the environment of for APNS2 Push Notifcataions
func (b *addPushNotificationsOnChannelsBuilder) Environment(env PNPushEnvironment) *addPushNotificationsOnChannelsBuilder {
b.opts.Environment = env
return b
}
// QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
func (b *addPushNotificationsOnChannelsBuilder) QueryParam(queryParam map[string]string) *addPushNotificationsOnChannelsBuilder {
b.opts.QueryParam = queryParam
return b
}
// Execute runs add Push Notifications on channels request
func (b *addPushNotificationsOnChannelsBuilder) Execute() (*AddPushNotificationsOnChannelsResponse, StatusResponse, error) {
_, status, err := executeRequest(b.opts)
if err != nil {
return emptyAddPushNotificationsOnChannelsResponse, status, err
}
return emptyAddPushNotificationsOnChannelsResponse, status, nil
}
func newAddChannelsToPushOpts(pubnub *PubNub, ctx context.Context, opts addChannelsToPushOpts) *addChannelsToPushOpts {
opts.endpointOpts = endpointOpts{pubnub: pubnub, ctx: ctx}
return &opts
}
type addChannelsToPushOpts struct {
endpointOpts
Channels []string
PushType PNPushType
DeviceIDForPush string
QueryParam map[string]string
Transport http.RoundTripper
Topic string
Environment PNPushEnvironment
}
func (o *addChannelsToPushOpts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if o.DeviceIDForPush == "" {
return newValidationError(o, StrMissingDeviceID)
}
if len(o.Channels) == 0 {
return newValidationError(o, StrMissingChannel)
}
if o.PushType == PNPushTypeNone {
return newValidationError(o, StrMissingPushType)
}
if o.PushType == PNPushTypeAPNS2 && (o.Topic == "") {
return newValidationError(o, StrMissingPushTopic)
}
return nil
}
// AddPushNotificationsOnChannelsResponse is response structure for AddPushNotificationsOnChannelsBuilder
type AddPushNotificationsOnChannelsResponse struct{}
func (o *addChannelsToPushOpts) buildPath() (string, error) {
if o.PushType == PNPushTypeAPNS2 {
return fmt.Sprintf(addChannelsToPushPathAPNS2,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.DeviceIDForPush)), nil
}
return fmt.Sprintf(addChannelsToPushPath,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.DeviceIDForPush)), nil
}
func (o *addChannelsToPushOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
var channels []string
for _, v := range o.Channels {
channels = append(channels, v)
}
q.Set("add", strings.Join(channels, ","))
q.Set("type", o.PushType.String())
SetPushEnvironment(q, o.Environment)
SetPushTopic(q, o.Topic)
SetQueryParam(q, o.QueryParam)
return q, nil
}
func (o *addChannelsToPushOpts) operationType() OperationType {
return PNRemoveGroupOperation
}