@@ -156,6 +156,22 @@ import Foundation
156
156
addSubscriptionChangeToTaskQueue ( interestName: interestName, change: . subscribe)
157
157
}
158
158
159
+ /**
160
+ Subscribe to interests with Pusher's Push Notification Service
161
+
162
+ - parameter interests: the name of the interests you want to subscribe to
163
+ */
164
+ open func setSubscriptions( interests: Array < String > ) {
165
+ requestQueue. tasks += { _, next in
166
+ self . replaceSubscriptionSet (
167
+ interests: interests,
168
+ successCallback: next
169
+ )
170
+ }
171
+
172
+ requestQueue. run ( )
173
+ }
174
+
159
175
/**
160
176
Unsubscribe from an interest with Pusher's Push Notification Service
161
177
@@ -176,7 +192,7 @@ import Foundation
176
192
*/
177
193
private func addSubscriptionChangeToTaskQueue( interestName: String , change: SubscriptionChange ) {
178
194
requestQueue. tasks += { _, next in
179
- self . modifySubscription (
195
+ self . subscribeOrUnsubscribeInterest (
180
196
interest: interestName,
181
197
change: change,
182
198
successCallback: next
@@ -188,98 +204,125 @@ import Foundation
188
204
189
205
/**
190
206
Makes either a POST or DELETE request for a given interest
191
-
192
- - parameter pusherAppKey: The app key for the Pusher app
193
- - parameter clientId: The clientId returned by Pusher's server
194
- - parameter interest: The name of the interest to be subscribed to /
195
- unsunscribed from
207
+ - parameter interest: The name of the interest to be subscribed to / unsunscribed from
196
208
- parameter change: Whether to subscribe or unsubscribe
197
209
- parameter callback: Callback to be called upon success
198
210
*/
199
- private func modifySubscription( interest: String , change: SubscriptionChange , successCallback: @escaping ( Any ? ) -> Void ) {
200
- guard pusherAppKey != nil && clientId != nil else {
201
- self . delegate? . debugLog ? ( message: " pusherAppKey \( String ( describing: pusherAppKey) ) or clientId \( String ( describing: clientId) ) not set - waiting for both to be set " )
211
+ private func subscribeOrUnsubscribeInterest( interest: String , change: SubscriptionChange , successCallback: @escaping ( Any ? ) -> Void ) {
212
+ guard
213
+ let clientId = clientId,
214
+ let pusherAppKey = pusherAppKey
215
+ else {
216
+ self . delegate? . debugLog ? ( message: " pusherAppKey or clientId not set - waiting for both to be set " )
202
217
self . requestQueue. pauseAndResetCurrentTask ( )
203
218
return
204
219
}
205
220
206
- self . delegate? . debugLog ? ( message: " Attempt number: \( self . failedRequestAttempts + 1 ) of \( maxFailedRequestAttempts) " )
221
+ let url = " \( CLIENT_API_V1_ENDPOINT) /clients/ \( clientId) /interests/ \( interest) "
222
+ let params : [ String : Any ] = [ " app_key " : pusherAppKey]
223
+ let request = self . setRequest ( url: url, params: params, change: change)
224
+ self . doURLRequest ( interests: [ interest] , request: request, change: change, successCallback: successCallback)
225
+ }
207
226
208
- let url = " \( CLIENT_API_V1_ENDPOINT) /clients/ \( clientId!) /interests/ \( interest) "
209
- var request = URLRequest ( url: URL ( string: url) !)
210
- request. httpMethod = change. httpMethod ( )
227
+ /**
228
+ Makes a PUT request for given interests
229
+ - parameter interests: The name of the interests to be subscribed to
230
+ - parameter callback: Callback to be called upon success
231
+ */
232
+ private func replaceSubscriptionSet( interests: Array < String > , successCallback: @escaping ( Any ? ) -> Void ) {
233
+ guard
234
+ let clientId = clientId,
235
+ let pusherAppKey = pusherAppKey
236
+ else {
237
+ self . delegate? . debugLog ? ( message: " pusherAppKey or clientId not set - waiting for both to be set " )
238
+ self . requestQueue. pauseAndResetCurrentTask ( )
239
+ return
240
+ }
211
241
212
- let params : [ String : Any ] = [ " app_key " : pusherAppKey!]
213
- try ! request. httpBody = JSONSerialization . data ( withJSONObject: params, options: [ ] )
242
+ let url = " \( CLIENT_API_V1_ENDPOINT) /clients/ \( clientId) /interests/ "
243
+ let params : [ String : Any ] = [ " app_key " : pusherAppKey, " interests " : interests]
244
+ let request = self . setRequest ( url: url, params: params, change: . setSubscriptions)
245
+ self . doURLRequest ( interests: interests, request: request, change: . setSubscriptions, successCallback: successCallback)
246
+ }
214
247
215
- request. addValue ( " application/json " , forHTTPHeaderField : " Content-Type " )
216
- request . addValue ( LIBRARY_NAME_AND_VERSION , forHTTPHeaderField : " X-Pusher-Library " )
248
+ private func doURLRequest ( interests : Array < String > , request: URLRequest , change : SubscriptionChange , successCallback : @escaping ( Any ? ) -> Void ) {
249
+ self . delegate ? . debugLog ? ( message : " Attempt number: \( self . failedRequestAttempts + 1 ) of \( maxFailedRequestAttempts ) " )
217
250
218
251
let task = URLSession . dataTask (
219
252
with: request,
220
253
completionHandler: { data, response, error in
221
254
guard let httpResponse = response as? HTTPURLResponse ,
222
- ( 200 <= httpResponse. statusCode && httpResponse. statusCode < 300 ) &&
223
- error == nil
224
- else {
225
- self . failedRequestAttempts += 1
226
-
227
- if error != nil {
228
- self . delegate? . debugLog ? ( message: " Error when trying to modify subscription to interest: \( String ( describing: error? . localizedDescription) ) " )
229
- } else if data != nil && response != nil {
230
- let responseBody = String ( data: data!, encoding: . utf8)
231
- self . delegate? . debugLog ? ( message: " Bad response from server: \( response!) with body: \( String ( describing: responseBody) ) " )
232
- } else {
233
- self . delegate? . debugLog ? ( message: " Bad response from server when trying to modify subscription to interest: \( interest ) " )
234
- }
235
-
236
- if self . failedRequestAttempts >= self . maxFailedRequestAttempts {
237
- self . delegate? . debugLog ? ( message: " Max number of failed native service requests reached " )
238
-
239
- self . requestQueue. paused = true
240
- } else {
241
- self . delegate? . debugLog ? ( message: " Retrying subscription modification request for interest: \( interest ) " )
242
- self . requestQueue. retry ( Double ( self . failedRequestAttempts * self . failedRequestAttempts) )
243
- }
244
-
245
- return
255
+ ( 200 <= httpResponse. statusCode && httpResponse. statusCode < 300 ) &&
256
+ error == nil
257
+ else {
258
+ self . failedRequestAttempts += 1
259
+
260
+ if error != nil {
261
+ self . delegate? . debugLog ? ( message: " Error when trying to modify subscription to interest(s) : \( String ( describing: error? . localizedDescription) ) " )
262
+ } else if data != nil && response != nil {
263
+ let responseBody = String ( data: data!, encoding: . utf8)
264
+ self . delegate? . debugLog ? ( message: " Bad response from server: \( response!) with body: \( String ( describing: responseBody) ) " )
265
+ } else {
266
+ self . delegate? . debugLog ? ( message: " Bad response from server when trying to modify subscription to interest(s) : \( interests ) " )
267
+ }
268
+
269
+ if self . failedRequestAttempts >= self . maxFailedRequestAttempts {
270
+ self . delegate? . debugLog ? ( message: " Max number of failed native service requests reached " )
271
+
272
+ self . requestQueue. paused = true
273
+ } else {
274
+ self . delegate? . debugLog ? ( message: " Retrying subscription modification request for interest(s) : \( interests ) " )
275
+ self . requestQueue. retry ( Double ( self . failedRequestAttempts * self . failedRequestAttempts) )
276
+ }
277
+
278
+ return
246
279
}
247
280
248
281
switch change {
249
282
case . subscribe:
283
+ guard let interest = interests. first else { return }
250
284
self . delegate? . subscribedToInterest ? ( name: interest)
285
+ case . setSubscriptions:
286
+ self . delegate? . subscribedToInterests ? ( interests: interests)
251
287
case . unsubscribe:
288
+ guard let interest = interests. first else { return }
252
289
self . delegate? . unsubscribedFromInterest ? ( name: interest)
253
290
}
254
291
255
- self . delegate? . debugLog ? ( message: " Success making \( change. stringValue ) to \( interest ) " )
292
+ self . delegate? . debugLog ? ( message: " Success making \( change. rawValue ) to \( interests ) " )
256
293
257
294
self . failedRequestAttempts = 0
258
295
successCallback ( nil )
259
- }
296
+ }
260
297
)
261
298
262
299
task. resume ( )
263
300
}
301
+
302
+ private func setRequest( url: String , params: [ String : Any ] , change: SubscriptionChange ) -> URLRequest {
303
+ var request = URLRequest ( url: URL ( string: url) !)
304
+ request. httpMethod = change. httpMethod ( )
305
+
306
+ try ! request. httpBody = JSONSerialization . data ( withJSONObject: params, options: [ ] )
307
+
308
+ request. addValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
309
+ request. addValue ( LIBRARY_NAME_AND_VERSION, forHTTPHeaderField: " X-Pusher-Library " )
310
+
311
+ return request
312
+ }
264
313
}
265
314
266
- internal enum SubscriptionChange {
315
+ internal enum SubscriptionChange : String {
267
316
case subscribe
317
+ case setSubscriptions
268
318
case unsubscribe
269
319
270
- internal func stringValue( ) -> String {
271
- switch self {
272
- case . subscribe:
273
- return " subscribe "
274
- case . unsubscribe:
275
- return " unsubscribe "
276
- }
277
- }
278
-
279
320
internal func httpMethod( ) -> String {
280
321
switch self {
281
322
case . subscribe:
282
323
return " POST "
324
+ case . setSubscriptions:
325
+ return " PUT "
283
326
case . unsubscribe:
284
327
return " DELETE "
285
328
}
0 commit comments