Fallback response JSON handling.

This commit is contained in:
Hedden, Kyle Matthew 2024-02-15 22:15:22 -05:00
parent 1efd261de3
commit c59dd70348
5 changed files with 37 additions and 2 deletions

View File

@ -34,12 +34,35 @@ open class ActionOpenPageHandler: MVMCoreJSONActionHandlerProtocol {
continuation.resume()
}
}
if operation.error != nil, let fallbackResponseJson = model.fallbackResponse?.toJSON() {
await runFallback(response: fallbackResponseJson, requestParameters: requestParameters, delegateObject: delegateObject)
}
}
} catch {
try handle(error: error, model: model, delegateObject: delegateObject)
}
}
/// Given backup JSON data, run it through the load handler.
fileprivate func runFallback(response: JSONDictionary, requestParameters: MVMCoreRequestParameters, delegateObject: DelegateObject?) async {
guard let loadHandler = MVMCoreLoadHandler.sharedGlobal(), let fallbackLoadObject = MVMCoreLoadObject(requestParameters: requestParameters, dataForPage: nil, delegateObject: delegateObject)
else { return }
let (loadObject, errorObject) = await MVMCoreLoadRequestOperation.processJSON(fromServer: response, loadObject: fallbackLoadObject)
if let errorObject {
MVMCoreLoggingHandler.shared()?.addError(toLog: errorObject)
} else {
let operation = loadHandler.loadObject(loadObject)
await withCheckedContinuation { continuation in
operation.completionBlock = {
continuation.resume()
}
}
}
}
open func execute(with model: ActionModelProtocol, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) async throws {
guard let model = model as? ActionOpenPageModel else { return }
do {

View File

@ -27,6 +27,7 @@ public struct ActionOpenPageModel: ActionModelProtocol, ActionOpenPageProtocol,
public var background: Bool?
public var clientParameters: ClientParameterModel?
public var customTimeoutTime: TimeInterval?
public var fallbackResponse: JSONValueDictionary?
public var requestParameters: MVMCoreRequestParameters
@ -67,6 +68,7 @@ public struct ActionOpenPageModel: ActionModelProtocol, ActionOpenPageProtocol,
case background
case clientParameters
case customTimeoutTime
case fallbackResponse
}
public init(from decoder: Decoder) throws {
@ -83,6 +85,7 @@ public struct ActionOpenPageModel: ActionModelProtocol, ActionOpenPageProtocol,
customTimeoutTime = try typeContainer.decodeIfPresent(TimeInterval.self, forKey: .customTimeoutTime)
extraParameters = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .extraParameters)
analyticsData = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .analyticsData)
fallbackResponse = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .fallbackResponse)
requestParameters = MVMCoreRequestParameters(pageType: pageType, additionalModules: modules ?? [], extraParameters: extraParameters.toJSON())!
requestParameters.contextRoot = appContext
@ -110,5 +113,6 @@ public struct ActionOpenPageModel: ActionModelProtocol, ActionOpenPageProtocol,
try container.encodeIfPresent(customTimeoutTime, forKey: .customTimeoutTime)
try container.encodeIfPresent(extraParameters, forKey: .extraParameters)
try container.encodeIfPresent(analyticsData, forKey: .analyticsData)
try container.encodeIfPresent(fallbackResponse, forKey: .fallbackResponse)
}
}

View File

@ -11,6 +11,7 @@
#import <MVMCore/MVMCoreLoadDelegateProtocol.h>
#import <MVMCore/MVMCorePresentationDelegateProtocol.h>
#import <MVMCore/MVMCoreActionDelegateProtocol.h>
#import <MVMCore/MVMCoreErrorObject.h>
@class MVMCoreLoadRequestOperation;
@class MVMCoreRequestParameters;
@ -58,9 +59,12 @@
// The full response json
@property (nullable, strong, nonatomic) NSDictionary *responseJSON;
//Unique Identifier for event tracking
// Unique Identifier for event tracking
@property (nullable, strong, nonatomic) NSString *identifier;
// If any error happened during the load.
@property (nullable, strong, nonatomic) MVMCoreErrorObject *error;
- (nullable instancetype)initWithPageJSON:(nullable NSDictionary *)pageJSON modulesJSON:(nullable NSDictionary *)modulesJSON requestParameters:(nullable MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegateObject:(nullable DelegateObject *)delegateObject;
- (nullable instancetype)initWithRequestParameters:(nullable MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegateObject:(nullable DelegateObject *)delegateObject;

View File

@ -19,6 +19,7 @@
@interface MVMCoreLoadRequestOperation : MVMCoreOperation
@property (nullable, strong, nonatomic) MVMCoreRequestParameters *requestParameters;
/// For load objects as in input parameter. Does not attach self generated load objects.
@property (nullable, strong, nonatomic) MVMCoreLoadObject *loadObject;
@property (nullable, strong, nonatomic) NSDictionary *dataForPage;
@property (nullable, strong, nonatomic) DelegateObject *delegateObject;
@ -27,6 +28,8 @@
@property (nonatomic) BOOL backgroundLoad;
@property (nonatomic, getter=areDependenciesAdded) BOOL dependenciesAdded;
@property (nonnull, nonatomic, strong) NSString *identifier;
/// If any error happened during the operation.
@property (nullable, strong, nonatomic) MVMCoreErrorObject *error;
/// Legacy flag for if this operation will have an alert to show when finished.
@property (nonatomic, readonly) BOOL alertToShow;

View File

@ -856,7 +856,8 @@
//make post load calls function
[MVMCoreLoadRequestOperation loadPostCallActions:loadObject];
[MVMCoreLoadRequestOperation loadPostAction:loadObject delegateObject:delegateObject];
loadObject.error = errorObject;
loadObject.operation.error = errorObject;
[loadObject.operation markAsFinished];
}