@@ -116,9 +116,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
116116 return self . register ( token: token, appName: appName, pushServicePlatform: self . config. pushPlatform)
117117 } ) . onSuccess { ( json) in
118118 onSuccess ? ( json)
119- } . onFailure { ( error) in
119+ } . onError { ( error) in
120120 if let sendError = error as? SendRequestError {
121- onFailure ? ( sendError. errorMessage , sendError. data)
121+ onFailure ? ( sendError. reason , sendError. data)
122122 } else {
123123 onFailure ? ( " failed to create user " , nil )
124124 }
@@ -132,7 +132,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
132132 guard email != nil || userId != nil else {
133133 ITBError ( " Both email and userId are nil " )
134134 onFailure ? ( " Both email and userId are nil " , nil )
135- return SendRequestError . createFailedFuture ( reason: " Both email and userId are nil " )
135+ return SendRequestError . createErroredFuture ( reason: " Both email and userId are nil " )
136136 }
137137
138138 hexToken = ( token as NSData ) . iteHexadecimalString ( )
@@ -178,7 +178,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
178178 ITBInfo ( " sending registerToken request with args \( args) " )
179179 return
180180 createPostRequest ( forPath: . ITBL_PATH_REGISTER_DEVICE_TOKEN, withBody: args)
181- . map { sendRequest ( $0, onSuccess: onSuccess, onFailure: onFailure) } ?? SendRequestError . createFailedFuture ( reason: " Couldn't create register request " )
181+ . map { sendRequest ( $0, onSuccess: onSuccess, onFailure: onFailure) } ?? SendRequestError . createErroredFuture ( reason: " Couldn't create register request " )
182182 }
183183
184184 func disableDeviceForCurrentUser( ) {
@@ -242,7 +242,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
242242
243243 return createPostRequest ( forPath: . ITBL_PATH_CREATE_USER, withBody: args) . map {
244244 sendRequest ( $0)
245- } ?? SendRequestError . createFailedFuture ( reason: " Could not create createUser Reqeust " )
245+ } ?? SendRequestError . createErroredFuture ( reason: " Could not create createUser Reqeust " )
246246 }
247247
248248 func trackPurchase( _ total: NSNumber , items: [ CommerceItem ] ) {
@@ -399,56 +399,34 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
399399 }
400400 }
401401
402- func spawn( inAppNotification callbackBlock: ITEActionBlock ? ) {
403- let onSuccess : OnSuccessHandler = { payload in
404- guard let payload = payload else {
405- return
406- }
407- guard let dialogOptions = IterableInAppManager . getNextMessageFromPayload ( payload) else {
408- ITBError ( " No notifications found for inApp payload \( payload) " )
409- return
410- }
411- guard let message = dialogOptions [ . ITBL_IN_APP_CONTENT] as? [ AnyHashable : Any ] else {
412- return
413- }
414- guard let messageId = dialogOptions [ . ITBL_KEY_MESSAGE_ID] as? String else {
415- return
416- }
417- guard let html = message [ . ITBL_IN_APP_HTML] as? String else {
418- return
419- }
420- guard html. range ( of: AnyHashable . ITBL_IN_APP_HREF, options: [ . caseInsensitive] ) != nil else {
421- ITBError ( " No href tag found in in-app html payload \( html) " )
422- self . inAppConsume ( messageId)
423- return
424- }
425-
426- let inAppDisplaySettings = message [ . ITBL_IN_APP_DISPLAY_SETTINGS] as? [ AnyHashable : Any ]
427- let backgroundAlpha = IterableInAppManager . getBackgroundAlpha ( fromInAppSettings: inAppDisplaySettings)
428- let edgeInsets = IterableInAppManager . getPaddingFromPayload ( inAppDisplaySettings)
429-
430- let notificationMetadata = IterableNotificationMetadata . metadata ( fromInAppOptions: messageId)
431-
432- DispatchQueue . main. async {
433- let opened = IterableInAppManager . showIterableNotificationHTML ( html, trackParams: notificationMetadata, backgroundAlpha: backgroundAlpha, padding: edgeInsets, callbackBlock: callbackBlock)
434- if opened {
435- self . inAppConsume ( messageId)
402+ @discardableResult func spawn( inAppNotification callbackBlock: ITEActionBlock ? ) -> Future < IterableInAppManager . ShowInAppResult > {
403+ return getInAppMessages ( 1 )
404+ . map { IterableInAppManager . parseInApp ( fromPayload: $0) }
405+ . flatMap { IterableInAppManager . showInApp ( parseResult: $0, callbackBlock: callbackBlock) }
406+ . onSuccess {
407+ switch $0 {
408+ case . success( opened: let opened, messageId: let messageId) :
409+ if opened {
410+ self . inAppConsume ( messageId)
411+ }
412+ case . failure( reason: let reason, messageId: let messageId) :
413+ if let messageId = messageId {
414+ self . inAppConsume ( messageId)
415+ }
416+ ITBError ( reason)
436417 }
437418 }
438- }
439-
440- getInAppMessages ( 1 , onSuccess: onSuccess, onFailure: IterableAPIInternal . defaultOnFailure ( identifier: " getInAppMessages " ) )
441419 }
442420
443- func getInAppMessages( _ count: NSNumber ) {
444- getInAppMessages ( count, onSuccess: IterableAPIInternal . defaultOnSucess ( identifier: " getMessages " ) , onFailure: IterableAPIInternal . defaultOnFailure ( identifier: " getMessages " ) )
421+ @ discardableResult func getInAppMessages( _ count: NSNumber ) -> Future < SendRequestValue > {
422+ return getInAppMessages ( count, onSuccess: IterableAPIInternal . defaultOnSucess ( identifier: " getMessages " ) , onFailure: IterableAPIInternal . defaultOnFailure ( identifier: " getMessages " ) )
445423 }
446424
447- func getInAppMessages( _ count: NSNumber , onSuccess: OnSuccessHandler ? , onFailure: OnFailureHandler ? ) {
425+ @ discardableResult func getInAppMessages( _ count: NSNumber , onSuccess: OnSuccessHandler ? , onFailure: OnFailureHandler ? ) -> Future < SendRequestValue > {
448426 guard email != nil || userId != nil else {
449427 ITBError ( " Both email and userId are nil " )
450428 onFailure ? ( " Both email and userId are nil " , nil )
451- return
429+ return SendRequestError . createErroredFuture ( reason : " Both email and userId are nil " )
452430 }
453431
454432 var args : [ AnyHashable : Any ] = [
@@ -459,9 +437,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
459437
460438 addEmailOrUserId ( args: & args)
461439
462- if let request = createGetRequest ( forPath: . ITBL_PATH_GET_INAPP_MESSAGES, withArgs: args as! [ String : String ] ) {
463- sendRequest ( request , onSuccess: onSuccess, onFailure: onFailure)
464- }
440+ return createGetRequest ( forPath: . ITBL_PATH_GET_INAPP_MESSAGES, withArgs: args as! [ String : String ] ) . map {
441+ sendRequest ( $0 , onSuccess: onSuccess, onFailure: onFailure)
442+ } ?? SendRequestError . createErroredFuture ( reason : " Could not create get request for getInApp " )
465443 }
466444
467445 func trackInAppOpen( _ messageId: String ) {
@@ -533,9 +511,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
533511 @discardableResult func sendRequest( _ request: URLRequest , onSuccess: OnSuccessHandler ? = nil , onFailure: OnFailureHandler ? = nil ) -> Future < SendRequestValue > {
534512 return NetworkHelper . sendRequest ( request, usingSession: networkSession) . onSuccess { ( json) in
535513 onSuccess ? ( json)
536- } . onFailure { ( failureInfo ) in
537- if let sendError = failureInfo as? SendRequestError {
538- onFailure ? ( sendError. errorMessage , sendError. data)
514+ } . onError { ( error ) in
515+ if let sendError = error as? SendRequestError {
516+ onFailure ? ( sendError. reason , sendError. data)
539517 } else {
540518 onFailure ? ( " send request failed " , nil )
541519 }
@@ -796,9 +774,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
796774
797775 NetworkHelper . sendRequest ( request, usingSession: networkSession) . onSuccess { ( json) in
798776 self . handleDDL ( json: json)
799- } . onFailure { ( failureInfo ) in
800- if let sendError = failureInfo as? SendRequestError , let errorMessage = sendError. errorMessage {
801- ITBError ( errorMessage )
777+ } . onError { ( error ) in
778+ if let sendError = error as? SendRequestError , let reason = sendError. reason {
779+ ITBError ( reason )
802780 } else {
803781 ITBError ( " failed to send handleDDl request " )
804782 }
0 commit comments