@@ -16,27 +16,31 @@ struct RequestCreator {
16
16
// MARK: - API REQUEST CALLS
17
17
18
18
func createUpdateEmailRequest( newEmail: String ) -> Result < IterableRequest , IterableError > {
19
- var body : [ String : Any ] = [ JsonKey . newEmail: newEmail]
19
+ if case . none = auth. emailOrUserId {
20
+ ITBError ( Self . authMissingMessage)
21
+ return . failure( IterableError . general ( description: Self . authMissingMessage) )
22
+ }
23
+
24
+ var body = [ String: Any] ( )
20
25
21
26
if let email = auth. email {
22
27
body [ JsonKey . currentEmail] = email
23
28
} else if let userId = auth. userId {
24
29
body [ JsonKey . currentUserId] = userId
25
- } else {
26
- ITBError ( " Both email and userId are nil " )
27
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
28
30
}
29
31
32
+ body [ JsonKey . newEmail] = newEmail
33
+
30
34
return . success( . post( createPostRequest ( path: Const . Path. updateEmail, body: body) ) )
31
35
}
32
36
33
37
func createRegisterTokenRequest( registerTokenInfo: RegisterTokenInfo ,
34
38
notificationsEnabled: Bool ) -> Result < IterableRequest , IterableError > {
35
39
if case . none = auth. emailOrUserId {
36
- ITBError ( " Both email and userId are nil " )
37
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
40
+ ITBError ( Self . authMissingMessage )
41
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
38
42
}
39
-
43
+
40
44
let dataFields = DataFieldsHelper . createDataFields ( sdkVersion: registerTokenInfo. sdkVersion,
41
45
deviceId: registerTokenInfo. deviceId,
42
46
device: UIDevice . current,
@@ -47,7 +51,7 @@ struct RequestCreator {
47
51
let deviceDictionary : [ String : Any ] = [
48
52
JsonKey . token: registerTokenInfo. hexToken,
49
53
JsonKey . platform: RequestCreator . pushServicePlatformToString ( registerTokenInfo. pushServicePlatform,
50
- apnsType: registerTokenInfo. apnsType) ,
54
+ apnsType: registerTokenInfo. apnsType) ,
51
55
JsonKey . applicationName: registerTokenInfo. appName,
52
56
JsonKey . dataFields: dataFields,
53
57
]
@@ -67,27 +71,29 @@ struct RequestCreator {
67
71
68
72
func createUpdateUserRequest( dataFields: [ AnyHashable : Any ] , mergeNestedObjects: Bool ) -> Result < IterableRequest , IterableError > {
69
73
if case . none = auth. emailOrUserId {
70
- ITBError ( " Both email and userId are nil " )
71
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
74
+ ITBError ( Self . authMissingMessage )
75
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
72
76
}
73
-
77
+
74
78
var body = [ AnyHashable: Any] ( )
75
79
76
- body [ JsonKey . dataFields] = dataFields
77
- body [ JsonKey . mergeNestedObjects] = NSNumber ( value: mergeNestedObjects)
78
80
setCurrentUser ( inDict: & body)
79
81
80
82
if auth. email == nil , auth. userId != nil {
81
83
body [ JsonKey . preferUserId] = true
82
84
}
83
85
86
+ body [ JsonKey . mergeNestedObjects] = NSNumber ( value: mergeNestedObjects)
87
+
88
+ body [ JsonKey . dataFields] = dataFields
89
+
84
90
return . success( . post( createPostRequest ( path: Const . Path. updateUser, body: body) ) )
85
91
}
86
92
87
93
func createUpdateCartRequest( items: [ CommerceItem ] , dataFields: [ AnyHashable : Any ] ? ) -> Result < IterableRequest , IterableError > {
88
94
if case . none = auth. emailOrUserId {
89
- ITBError ( " Both email and userId are nil " )
90
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
95
+ ITBError ( Self . authMissingMessage )
96
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
91
97
}
92
98
93
99
var apiUserDict = [ AnyHashable: Any] ( )
@@ -108,16 +114,16 @@ struct RequestCreator {
108
114
109
115
func createTrackPurchaseRequest( _ total: NSNumber , items: [ CommerceItem ] , dataFields: [ AnyHashable : Any ] ? ) -> Result < IterableRequest , IterableError > {
110
116
if case . none = auth. emailOrUserId {
111
- ITBError ( " Both email and userId are nil " )
112
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
117
+ ITBError ( Self . authMissingMessage )
118
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
113
119
}
114
120
115
- let itemsToSerialize = items. map { $0. toDictionary ( ) }
116
-
117
121
var apiUserDict = [ AnyHashable: Any] ( )
118
122
119
123
setCurrentUser ( inDict: & apiUserDict)
120
124
125
+ let itemsToSerialize = items. map { $0. toDictionary ( ) }
126
+
121
127
var body : [ String : Any ] = [ JsonKey . Commerce. user: apiUserDict,
122
128
JsonKey . Commerce. items: itemsToSerialize,
123
129
JsonKey . Commerce. total: total]
@@ -130,15 +136,12 @@ struct RequestCreator {
130
136
}
131
137
132
138
func createTrackPushOpenRequest( _ campaignId: NSNumber , templateId: NSNumber ? , messageId: String , appAlreadyRunning: Bool , dataFields: [ AnyHashable : Any ] ? ) -> Result < IterableRequest , IterableError > {
133
- var body = [ AnyHashable: Any] ( )
134
- var reqDataFields = [ AnyHashable: Any] ( )
135
-
136
- if let dataFields = dataFields {
137
- reqDataFields = dataFields
139
+ if case . none = auth. emailOrUserId {
140
+ ITBError ( Self . authMissingMessage)
141
+ return . failure( IterableError . general ( description: Self . authMissingMessage) )
138
142
}
139
143
140
- reqDataFields [ JsonKey . appAlreadyRunning] = appAlreadyRunning
141
- body [ JsonKey . dataFields] = reqDataFields
144
+ var body = [ AnyHashable: Any] ( )
142
145
143
146
setCurrentUser ( inDict: & body)
144
147
@@ -150,19 +153,29 @@ struct RequestCreator {
150
153
151
154
body. setValue ( for: JsonKey . messageId, value: messageId)
152
155
156
+ var compositeDataFields = [ AnyHashable: Any] ( )
157
+
158
+ if let dataFields = dataFields {
159
+ compositeDataFields = dataFields
160
+ }
161
+
162
+ compositeDataFields [ JsonKey . appAlreadyRunning] = appAlreadyRunning
163
+
164
+ body [ JsonKey . dataFields] = compositeDataFields
165
+
153
166
return . success( . post( createPostRequest ( path: Const . Path. trackPushOpen, body: body) ) )
154
167
}
155
168
156
169
func createTrackEventRequest( _ eventName: String , dataFields: [ AnyHashable : Any ] ? ) -> Result < IterableRequest , IterableError > {
157
170
if case . none = auth. emailOrUserId {
158
- ITBError ( " Both email and userId are nil " )
159
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
171
+ ITBError ( Self . authMissingMessage )
172
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
160
173
}
161
-
174
+
162
175
var body = [ AnyHashable: Any] ( )
163
176
164
177
setCurrentUser ( inDict: & body)
165
-
178
+
166
179
body. setValue ( for: JsonKey . eventName, value: eventName)
167
180
168
181
if let dataFields = dataFields {
@@ -179,10 +192,10 @@ struct RequestCreator {
179
192
campaignId: NSNumber ? = nil ,
180
193
templateId: NSNumber ? = nil ) -> Result < IterableRequest , IterableError > {
181
194
if case . none = auth. emailOrUserId {
182
- ITBError ( " Both email and userId are nil " )
183
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
195
+ ITBError ( Self . authMissingMessage )
196
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
184
197
}
185
-
198
+
186
199
var body = [ AnyHashable: Any] ( )
187
200
188
201
setCurrentUser ( inDict: & body)
@@ -216,10 +229,10 @@ struct RequestCreator {
216
229
217
230
func createGetInAppMessagesRequest( _ count: NSNumber ) -> Result < IterableRequest , IterableError > {
218
231
if case . none = auth. emailOrUserId {
219
- ITBError ( " Both email and userId are nil " )
220
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
232
+ ITBError ( Self . authMissingMessage )
233
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
221
234
}
222
-
235
+
223
236
var args : [ AnyHashable : Any ] = [ JsonKey . InApp. count: count. description,
224
237
JsonKey . platform: JsonValue . iOS,
225
238
JsonKey . systemVersion: UIDevice . current. systemVersion,
@@ -236,16 +249,15 @@ struct RequestCreator {
236
249
237
250
func createTrackInAppOpenRequest( inAppMessageContext: InAppMessageContext ) -> Result < IterableRequest , IterableError > {
238
251
if case . none = auth. emailOrUserId {
239
- ITBError ( " Both email and userId are nil " )
240
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
252
+ ITBError ( Self . authMissingMessage )
253
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
241
254
}
242
-
243
- var body = [ AnyHashable: Any] ( )
244
255
245
- body. setValue ( for : JsonKey . messageId , value : inAppMessageContext . messageId )
256
+ var body = [ AnyHashable : Any ] ( )
246
257
247
258
setCurrentUser ( inDict: & body)
248
259
260
+ body. setValue ( for: JsonKey . messageId, value: inAppMessageContext. messageId)
249
261
body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
250
262
body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
251
263
@@ -258,18 +270,16 @@ struct RequestCreator {
258
270
259
271
func createTrackInAppClickRequest( inAppMessageContext: InAppMessageContext , clickedUrl: String ) -> Result < IterableRequest , IterableError > {
260
272
if case . none = auth. emailOrUserId {
261
- ITBError ( " Both email and userId are nil " )
262
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
273
+ ITBError ( Self . authMissingMessage )
274
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
263
275
}
264
-
265
- var body = [ AnyHashable: Any] ( )
266
276
267
- body. setValue ( for : JsonKey . messageId , value : inAppMessageContext . messageId )
277
+ var body = [ AnyHashable : Any ] ( )
268
278
269
279
setCurrentUser ( inDict: & body)
270
280
281
+ body. setValue ( for: JsonKey . messageId, value: inAppMessageContext. messageId)
271
282
body. setValue ( for: JsonKey . clickedUrl, value: clickedUrl)
272
-
273
283
body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
274
284
body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
275
285
@@ -282,13 +292,17 @@ struct RequestCreator {
282
292
283
293
func createTrackInAppCloseRequest( inAppMessageContext: InAppMessageContext , source: InAppCloseSource ? , clickedUrl: String ? ) -> Result < IterableRequest , IterableError > {
284
294
if case . none = auth. emailOrUserId {
285
- ITBError ( " Both email and userId are nil " )
286
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
295
+ ITBError ( Self . authMissingMessage )
296
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
287
297
}
288
-
298
+
289
299
var body = [ AnyHashable: Any] ( )
290
300
301
+ setCurrentUser ( inDict: & body)
302
+
291
303
body. setValue ( for: JsonKey . messageId, value: inAppMessageContext. messageId)
304
+ body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
305
+ body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
292
306
293
307
if let source = source {
294
308
body. setValue ( for: JsonKey . closeAction, value: source)
@@ -298,30 +312,24 @@ struct RequestCreator {
298
312
body. setValue ( for: JsonKey . clickedUrl, value: clickedUrl)
299
313
}
300
314
301
- body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
302
- body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
303
-
304
315
if let inboxSessionId = inAppMessageContext. inboxSessionId {
305
316
body. setValue ( for: JsonKey . inboxSessionId, value: inboxSessionId)
306
317
}
307
318
308
- setCurrentUser ( inDict: & body)
309
-
310
319
return . success( . post( createPostRequest ( path: Const . Path. trackInAppClose, body: body) ) )
311
320
}
312
321
313
322
func createTrackInAppDeliveryRequest( inAppMessageContext: InAppMessageContext ) -> Result < IterableRequest , IterableError > {
314
323
if case . none = auth. emailOrUserId {
315
- ITBError ( " Both email and userId are nil " )
316
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
324
+ ITBError ( Self . authMissingMessage )
325
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
317
326
}
318
-
319
- var body = [ AnyHashable: Any] ( )
320
327
321
- body. setValue ( for : JsonKey . messageId , value : inAppMessageContext . messageId )
328
+ var body = [ AnyHashable : Any ] ( )
322
329
323
330
setCurrentUser ( inDict: & body)
324
331
332
+ body. setValue ( for: JsonKey . messageId, value: inAppMessageContext. messageId)
325
333
body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
326
334
body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
327
335
@@ -330,51 +338,50 @@ struct RequestCreator {
330
338
331
339
func createInAppConsumeRequest( _ messageId: String ) -> Result < IterableRequest , IterableError > {
332
340
if case . none = auth. emailOrUserId {
333
- ITBError ( " Both email and userId are nil " )
334
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
341
+ ITBError ( Self . authMissingMessage )
342
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
335
343
}
336
-
337
- var body = [ AnyHashable: Any] ( )
338
344
339
- body. setValue ( for : JsonKey . messageId , value : messageId )
345
+ var body = [ AnyHashable : Any ] ( )
340
346
341
347
setCurrentUser ( inDict: & body)
342
348
349
+ body. setValue ( for: JsonKey . messageId, value: messageId)
350
+
343
351
return . success( . post( createPostRequest ( path: Const . Path. inAppConsume, body: body) ) )
344
352
}
345
353
346
354
func createTrackInAppConsumeRequest( inAppMessageContext: InAppMessageContext , source: InAppDeleteSource ? ) -> Result < IterableRequest , IterableError > {
347
355
if case . none = auth. emailOrUserId {
348
- ITBError ( " Both email and userId are nil " )
349
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
356
+ ITBError ( Self . authMissingMessage )
357
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
350
358
}
351
-
359
+
352
360
var body = [ AnyHashable: Any] ( )
353
361
362
+ setCurrentUser ( inDict: & body)
363
+
354
364
body. setValue ( for: JsonKey . messageId, value: inAppMessageContext. messageId)
365
+ body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
366
+ body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
355
367
356
368
if let source = source {
357
369
body. setValue ( for: JsonKey . deleteAction, value: source)
358
370
}
359
371
360
- body. setValue ( for: JsonKey . inAppMessageContext, value: inAppMessageContext. toMessageContextDictionary ( ) )
361
- body. setValue ( for: JsonKey . deviceInfo, value: deviceMetadata. asDictionary ( ) )
362
-
363
372
if let inboxSessionId = inAppMessageContext. inboxSessionId {
364
373
body. setValue ( for: JsonKey . inboxSessionId, value: inboxSessionId)
365
374
}
366
375
367
- setCurrentUser ( inDict: & body)
368
-
369
376
return . success( . post( createPostRequest ( path: Const . Path. inAppConsume, body: body) ) )
370
377
}
371
378
372
379
func createTrackInboxSessionRequest( inboxSession: IterableInboxSession ) -> Result < IterableRequest , IterableError > {
373
380
if case . none = auth. emailOrUserId {
374
- ITBError ( " Both email and userId are nil " )
375
- return . failure( IterableError . general ( description: " Both email and userId are nil " ) )
381
+ ITBError ( Self . authMissingMessage )
382
+ return . failure( IterableError . general ( description: Self . authMissingMessage ) )
376
383
}
377
-
384
+
378
385
guard let inboxSessionId = inboxSession. id else {
379
386
return . failure( IterableError . general ( description: " expecting session UUID " ) )
380
387
}
@@ -388,7 +395,7 @@ struct RequestCreator {
388
395
}
389
396
390
397
var body = [ AnyHashable: Any] ( )
391
-
398
+
392
399
setCurrentUser ( inDict: & body)
393
400
394
401
body. setValue ( for: JsonKey . inboxSessionId, value: inboxSessionId)
@@ -425,12 +432,14 @@ struct RequestCreator {
425
432
if let packageName = Bundle . main. appPackageName {
426
433
args [ JsonKey . InApp. packageName] = packageName
427
434
}
428
-
435
+
429
436
return . success( . get( createGetRequest ( forPath: Const . Path. getRemoteConfiguration, withArgs: args as! [ String : String ] ) ) )
430
437
}
431
438
432
439
// MARK: - PRIVATE
433
440
441
+ private static let authMissingMessage = " Both email and userId are nil "
442
+
434
443
private func createPostRequest( path: String , body: [ AnyHashable : Any ] ? = nil ) -> PostRequest {
435
444
PostRequest ( path: path,
436
445
args: [ JsonKey . Header. apiKey: apiKey] ,
@@ -462,6 +471,5 @@ struct RequestCreator {
462
471
case . none:
463
472
ITBInfo ( " Current user is unavailable " )
464
473
}
465
-
466
474
}
467
475
}
0 commit comments