@@ -32,11 +32,11 @@ struct RequestCreator {
32
32
33
33
func createRegisterTokenRequest( registerTokenInfo: RegisterTokenInfo ,
34
34
notificationsEnabled: Bool ) -> Result < IterableRequest , IterableError > {
35
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
35
+ if case . none = auth . emailOrUserId {
36
36
ITBError ( " Both email and userId are nil " )
37
37
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
38
38
}
39
-
39
+
40
40
let dataFields = DataFieldsHelper . createDataFields ( sdkVersion: registerTokenInfo. sdkVersion,
41
41
deviceId: registerTokenInfo. deviceId,
42
42
device: UIDevice . current,
@@ -56,7 +56,7 @@ struct RequestCreator {
56
56
57
57
body [ JsonKey . device. jsonKey] = deviceDictionary
58
58
59
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
59
+ setCurrentUser ( inDict : & body )
60
60
61
61
if auth. email == nil , auth. userId != nil {
62
62
body [ JsonKey . preferUserId. jsonKey] = true
@@ -66,16 +66,16 @@ struct RequestCreator {
66
66
}
67
67
68
68
func createUpdateUserRequest( dataFields: [ AnyHashable : Any ] , mergeNestedObjects: Bool ) -> Result < IterableRequest , IterableError > {
69
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
69
+ if case . none = auth . emailOrUserId {
70
70
ITBError ( " Both email and userId are nil " )
71
71
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
72
72
}
73
-
73
+
74
74
var body = [ AnyHashable: Any] ( )
75
75
76
76
body [ JsonKey . dataFields. jsonKey] = dataFields
77
77
body [ JsonKey . mergeNestedObjects. jsonKey] = NSNumber ( value: mergeNestedObjects)
78
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
78
+ setCurrentUser ( inDict : & body )
79
79
80
80
if auth. email == nil , auth. userId != nil {
81
81
body [ JsonKey . preferUserId. jsonKey] = true
@@ -85,11 +85,11 @@ struct RequestCreator {
85
85
}
86
86
87
87
func createTrackPurchaseRequest( _ total: NSNumber , items: [ CommerceItem ] , dataFields: [ AnyHashable : Any ] ? ) -> Result < IterableRequest , IterableError > {
88
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
88
+ if case . none = auth . emailOrUserId {
89
89
ITBError ( " Both email and userId are nil " )
90
90
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
91
91
}
92
-
92
+
93
93
var itemsToSerialize = [ [ AnyHashable: Any] ] ( )
94
94
95
95
for item in items {
@@ -98,7 +98,7 @@ struct RequestCreator {
98
98
99
99
var apiUserDict = [ AnyHashable: Any] ( )
100
100
101
- apiUserDict . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
101
+ setCurrentUser ( inDict : & apiUserDict )
102
102
103
103
var body : [ String : Any ] = [ JsonKey . Commerce. user: apiUserDict,
104
104
JsonKey . Commerce. items: itemsToSerialize,
@@ -122,9 +122,7 @@ struct RequestCreator {
122
122
reqDataFields [ JsonKey . appAlreadyRunning. jsonKey] = appAlreadyRunning
123
123
body [ JsonKey . dataFields. jsonKey] = reqDataFields
124
124
125
- if let keyValueForCurrentUser = keyValueForCurrentUser {
126
- body. setValue ( for: keyValueForCurrentUser. key, value: keyValueForCurrentUser. value)
127
- }
125
+ setCurrentUser ( inDict: & body)
128
126
129
127
body [ JsonKey . campaignId. jsonKey] = campaignId
130
128
@@ -138,14 +136,15 @@ struct RequestCreator {
138
136
}
139
137
140
138
func createTrackEventRequest( _ eventName: String , dataFields: [ AnyHashable : Any ] ? ) -> Result < IterableRequest , IterableError > {
141
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
139
+ if case . none = auth . emailOrUserId {
142
140
ITBError ( " Both email and userId are nil " )
143
141
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
144
142
}
145
-
143
+
146
144
var body = [ AnyHashable: Any] ( )
147
145
148
- body. setValue ( for: keyValueForCurrentUser. key, value: keyValueForCurrentUser. value)
146
+ setCurrentUser ( inDict: & body)
147
+
149
148
body. setValue ( for: . eventName, value: eventName)
150
149
151
150
if let dataFields = dataFields {
@@ -161,14 +160,14 @@ struct RequestCreator {
161
160
subscribedMessageTypeIds: [ NSNumber ] ? = nil ,
162
161
campaignId: NSNumber ? = nil ,
163
162
templateId: NSNumber ? = nil ) -> Result < IterableRequest , IterableError > {
164
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
163
+ if case . none = auth . emailOrUserId {
165
164
ITBError ( " Both email and userId are nil " )
166
165
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
167
166
}
168
-
167
+
169
168
var body = [ AnyHashable: Any] ( )
170
169
171
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
170
+ setCurrentUser ( inDict : & body )
172
171
173
172
if let emailListIds = emailListIds {
174
173
body [ JsonKey . emailListIds. jsonKey] = emailListIds
@@ -198,11 +197,11 @@ struct RequestCreator {
198
197
}
199
198
200
199
func createGetInAppMessagesRequest( _ count: NSNumber ) -> Result < IterableRequest , IterableError > {
201
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
200
+ if case . none = auth . emailOrUserId {
202
201
ITBError ( " Both email and userId are nil " )
203
202
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
204
203
}
205
-
204
+
206
205
var args : [ AnyHashable : Any ] = [ JsonKey . InApp. count: count. description,
207
206
JsonKey . platform. jsonKey: JsonValue . iOS. jsonStringValue,
208
207
JsonKey . systemVersion. jsonKey: UIDevice . current. systemVersion,
@@ -212,22 +211,22 @@ struct RequestCreator {
212
211
args [ JsonKey . InApp. packageName] = packageName
213
212
}
214
213
215
- args . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
214
+ setCurrentUser ( inDict : & args )
216
215
217
216
return . success( . get( createGetRequest ( forPath: Const . Path. getInAppMessages, withArgs: args as! [ String : String ] ) ) )
218
217
}
219
218
220
219
func createTrackInAppOpenRequest( inAppMessageContext: InAppMessageContext ) -> Result < IterableRequest , IterableError > {
221
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
220
+ if case . none = auth . emailOrUserId {
222
221
ITBError ( " Both email and userId are nil " )
223
222
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
224
223
}
225
-
224
+
226
225
var body = [ AnyHashable: Any] ( )
227
226
228
227
body. setValue ( for: . messageId, value: inAppMessageContext. messageId)
229
228
230
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
229
+ setCurrentUser ( inDict : & body )
231
230
232
231
body. setValue ( for: . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
233
232
body. setValue ( for: . deviceInfo, value: deviceMetadata. asDictionary ( ) )
@@ -240,16 +239,16 @@ struct RequestCreator {
240
239
}
241
240
242
241
func createTrackInAppClickRequest( inAppMessageContext: InAppMessageContext , clickedUrl: String ) -> Result < IterableRequest , IterableError > {
243
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
242
+ if case . none = auth . emailOrUserId {
244
243
ITBError ( " Both email and userId are nil " )
245
244
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
246
245
}
247
-
246
+
248
247
var body = [ AnyHashable: Any] ( )
249
248
250
249
body. setValue ( for: . messageId, value: inAppMessageContext. messageId)
251
250
252
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
251
+ setCurrentUser ( inDict : & body )
253
252
254
253
body. setValue ( for: . clickedUrl, value: clickedUrl)
255
254
@@ -264,11 +263,11 @@ struct RequestCreator {
264
263
}
265
264
266
265
func createTrackInAppCloseRequest( inAppMessageContext: InAppMessageContext , source: InAppCloseSource ? , clickedUrl: String ? ) -> Result < IterableRequest , IterableError > {
267
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
266
+ if case . none = auth . emailOrUserId {
268
267
ITBError ( " Both email and userId are nil " )
269
268
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
270
269
}
271
-
270
+
272
271
var body = [ AnyHashable: Any] ( )
273
272
274
273
body. setValue ( for: . messageId, value: inAppMessageContext. messageId)
@@ -288,22 +287,22 @@ struct RequestCreator {
288
287
body. setValue ( for: . inboxSessionId, value: inboxSessionId)
289
288
}
290
289
291
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
290
+ setCurrentUser ( inDict : & body )
292
291
293
292
return . success( . post( createPostRequest ( path: Const . Path. trackInAppClose, body: body) ) )
294
293
}
295
294
296
295
func createTrackInAppDeliveryRequest( inAppMessageContext: InAppMessageContext ) -> Result < IterableRequest , IterableError > {
297
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
296
+ if case . none = auth . emailOrUserId {
298
297
ITBError ( " Both email and userId are nil " )
299
298
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
300
299
}
301
-
300
+
302
301
var body = [ AnyHashable: Any] ( )
303
302
304
303
body. setValue ( for: . messageId, value: inAppMessageContext. messageId)
305
304
306
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
305
+ setCurrentUser ( inDict : & body )
307
306
308
307
body. setValue ( for: . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
309
308
body. setValue ( for: . deviceInfo, value: deviceMetadata. asDictionary ( ) )
@@ -312,26 +311,26 @@ struct RequestCreator {
312
311
}
313
312
314
313
func createInAppConsumeRequest( _ messageId: String ) -> Result < IterableRequest , IterableError > {
315
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
314
+ if case . none = auth . emailOrUserId {
316
315
ITBError ( " Both email and userId are nil " )
317
316
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
318
317
}
319
-
318
+
320
319
var body = [ AnyHashable: Any] ( )
321
320
322
321
body. setValue ( for: . messageId, value: messageId)
323
322
324
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
323
+ setCurrentUser ( inDict : & body )
325
324
326
325
return . success( . post( createPostRequest ( path: Const . Path. inAppConsume, body: body) ) )
327
326
}
328
327
329
328
func createTrackInAppConsumeRequest( inAppMessageContext: InAppMessageContext , source: InAppDeleteSource ? ) -> Result < IterableRequest , IterableError > {
330
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
329
+ if case . none = auth . emailOrUserId {
331
330
ITBError ( " Both email and userId are nil " )
332
331
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
333
332
}
334
-
333
+
335
334
var body = [ AnyHashable: Any] ( )
336
335
337
336
body. setValue ( for: . messageId, value: inAppMessageContext. messageId)
@@ -347,17 +346,17 @@ struct RequestCreator {
347
346
body. setValue ( for: . inboxSessionId, value: inboxSessionId)
348
347
}
349
348
350
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
349
+ setCurrentUser ( inDict : & body )
351
350
352
351
return . success( . post( createPostRequest ( path: Const . Path. inAppConsume, body: body) ) )
353
352
}
354
353
355
354
func createTrackInboxSessionRequest( inboxSession: IterableInboxSession ) -> Result < IterableRequest , IterableError > {
356
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
355
+ if case . none = auth . emailOrUserId {
357
356
ITBError ( " Both email and userId are nil " )
358
357
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
359
358
}
360
-
359
+
361
360
guard let inboxSessionId = inboxSession. id else {
362
361
return . failure( IterableError . general ( description: " expecting session UUID " ) )
363
362
}
@@ -371,8 +370,8 @@ struct RequestCreator {
371
370
}
372
371
373
372
var body = [ AnyHashable: Any] ( )
374
-
375
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
373
+
374
+ setCurrentUser ( inDict : & body )
376
375
377
376
body. setValue ( for: . inboxSessionId, value: inboxSessionId)
378
377
body. setValue ( for: . inboxSessionStart, value: IterableUtil . int ( fromDate: sessionStartTime) )
@@ -394,9 +393,7 @@ struct RequestCreator {
394
393
body. setValue ( for: . token, value: hexToken)
395
394
396
395
if !allUsers {
397
- if let keyValueForCurrentUser = keyValueForCurrentUser {
398
- body. setValue ( for: keyValueForCurrentUser. key, value: keyValueForCurrentUser. value)
399
- }
396
+ setCurrentUser ( inDict: & body)
400
397
}
401
398
402
399
return . success( . post( createPostRequest ( path: Const . Path. disableDevice, body: body) ) )
@@ -416,17 +413,6 @@ struct RequestCreator {
416
413
417
414
// MARK: - PRIVATE
418
415
419
- private var keyValueForCurrentUser : JsonKeyValueRepresentable ? {
420
- switch auth. emailOrUserId {
421
- case let . email( email) :
422
- return JsonKeyValue ( key: JsonKey . email, value: email)
423
- case let . userId( userId) :
424
- return JsonKeyValue ( key: JsonKey . userId, value: userId)
425
- case . none:
426
- return nil
427
- }
428
- }
429
-
430
416
private func createPostRequest( path: String , body: [ AnyHashable : Any ] ? = nil ) -> PostRequest {
431
417
PostRequest ( path: path,
432
418
args: [ JsonKey . Header. apiKey: apiKey] ,
@@ -448,14 +434,26 @@ struct RequestCreator {
448
434
return apnsType == . sandbox ? JsonValue . apnsSandbox. jsonStringValue : JsonValue . apnsProduction. jsonStringValue
449
435
}
450
436
}
437
+
438
+ private func setCurrentUser( inDict dict: inout [ AnyHashable : Any ] ) {
439
+ switch auth. emailOrUserId {
440
+ case let . email( email) :
441
+ dict. setValue ( for: . email, value: email)
442
+ case let . userId( userId) :
443
+ dict. setValue ( for: . userId, value: userId)
444
+ case . none:
445
+ ITBInfo ( " Current user is unavailable " )
446
+ }
447
+
448
+ }
451
449
}
452
450
453
451
// MARK: - DEPRECATED
454
452
455
453
extension RequestCreator {
456
454
// deprecated - will be removed in version 6.3.x or above
457
455
func createTrackInAppOpenRequest( _ messageId: String ) -> Result < IterableRequest , IterableError > {
458
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
456
+ if case . none = auth . emailOrUserId {
459
457
ITBError ( " Both email and userId are nil " )
460
458
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
461
459
}
@@ -464,7 +462,7 @@ extension RequestCreator {
464
462
465
463
body. setValue ( for: . messageId, value: messageId)
466
464
467
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
465
+ setCurrentUser ( inDict : & body )
468
466
469
467
let inAppMessageContext = InAppMessageContext . from ( messageId: messageId, deviceMetadata: deviceMetadata)
470
468
body. setValue ( for: . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
@@ -475,17 +473,17 @@ extension RequestCreator {
475
473
476
474
// deprecated - will be removed in version 6.3.x or above
477
475
func createTrackInAppClickRequest( _ messageId: String , clickedUrl: String ) -> Result < IterableRequest , IterableError > {
478
- guard let keyValueForCurrentUser = keyValueForCurrentUser else {
476
+ if case . none = auth . emailOrUserId {
479
477
ITBError ( " Both email and userId are nil " )
480
478
return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
481
479
}
482
-
480
+
483
481
var body = [ AnyHashable: Any] ( )
484
482
485
483
body. setValue ( for: . messageId, value: messageId)
486
484
body. setValue ( for: . clickedUrl, value: clickedUrl)
487
485
488
- body . setValue ( for : keyValueForCurrentUser . key , value : keyValueForCurrentUser . value )
486
+ setCurrentUser ( inDict : & body )
489
487
490
488
let inAppMessageContext = InAppMessageContext . from ( messageId: messageId, deviceMetadata: deviceMetadata)
491
489
body. setValue ( for: . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
0 commit comments