From bf7d811510eb50b27f48ff63601200f14f1a3db9 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Fri, 21 Aug 2020 10:40:31 -0400 Subject: [PATCH 1/5] support background flag from action --- MVMCore/MVMCore/LoadHandling/MVMCoreRequestParameters.m | 1 + MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreRequestParameters.m b/MVMCore/MVMCore/LoadHandling/MVMCoreRequestParameters.m index 9986bd7..f2e689f 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreRequestParameters.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreRequestParameters.m @@ -69,6 +69,7 @@ self.actionMap = actionMap; self.customTimeoutTime = [actionMap optionalNumberForKey:@"customTimeoutTime"]; self.openSupportPanel = [actionMap boolForKey:KeyOpenSupport]; + self.backgroundRequest = [actionMap boolForKey:@"background"]; // Right now server is sending default.... can't uncomment this until they remove default //self.replaceViewIfOnStackElseLoadWithStyle = [actionMap boolForKey:@"tryToReplaceFirst"]; diff --git a/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift b/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift index 94b7a3d..40e7312 100644 --- a/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift +++ b/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift @@ -19,16 +19,18 @@ public var analyticsData: JSONValueDictionary? public var presentationStyle: String? public var tabBarIndex: Int? - + public var background: Bool? + //-------------------------------------------------- // MARK: - Initialzier //-------------------------------------------------- - public init(pageType: String, presentationStyle: String? = nil, extraParameters: JSONValueDictionary? = nil, analyticsData: JSONValueDictionary? = nil, tabBarIndex: Int? = nil) { + public init(pageType: String, presentationStyle: String? = nil, extraParameters: JSONValueDictionary? = nil, analyticsData: JSONValueDictionary? = nil, tabBarIndex: Int? = nil, background: Bool? = nil) { self.pageType = pageType self.presentationStyle = presentationStyle self.extraParameters = extraParameters self.analyticsData = analyticsData self.tabBarIndex = tabBarIndex + self.background = background } } From 8794cd6ee9ff3c043fe33dc8361e98e1fdf57c94 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Thu, 27 Aug 2020 17:57:01 -0400 Subject: [PATCH 2/5] refactor for clarity and removing repetition. --- .../MVMCore/LoadHandling/MVMCoreLoadHandler.h | 2 + .../MVMCore/LoadHandling/MVMCoreLoadHandler.m | 126 +++++++++--------- 2 files changed, 68 insertions(+), 60 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h index 05089cf..c534baa 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h @@ -40,6 +40,8 @@ // Creates a request object with the given parameters. - (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error; +- (nullable NSData *)convertToJSON:(nonnull MVMCoreRequestParameters *)requestParameters forUrl:(nonnull NSURL *)url error:(MVMCoreErrorObject *_Nullable *_Nullable)error; + // Sends a given request to the server. When it is finished, it calls request finished, passing along the json object or nil if there is an error. - (nullable NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject * _Nullable error))requestFinished; diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m index f108113..e253d1c 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m @@ -154,37 +154,13 @@ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:timeOutInterval]; [self setHeadersForRequest:request requestParameters:requestParameters]; - NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; - - // Sets up the Initial parameters. - if (requestParameters.addInitialRequestParameters) { - NSDictionary *initialParameters = [[MVMCoreSessionObject sharedGlobal] getInitialParametersExcludingSections:requestParameters.excludedInitialParameters]; - if (initialParameters) { - [parameters setObject:initialParameters forKey:@"InitialParams"]; - } - } - - // Adds request specific parameters - if (requestParameters.parameters.count) { - [parameters setObject:requestParameters.parameters forKey:@"RequestParams"]; - } - - // Ensure the parameters are valid json. - if (![NSJSONSerialization isValidJSONObject:parameters]) { - *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + MVMCoreErrorObject *jsonError; + NSData *jsonData = [self convertToJSON:requestParameters forUrl:url error:&jsonError]; + if (!jsonData) { + *error = jsonError; return nil; } - // Logs the request parameters. - NSError *jsonError = nil; - NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; - if (!data) { - *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; - return nil; - } - NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - MVMCoreLog(@"Request Parameters for URL %@:\n%@",[request.URL absoluteString], jsonString); - //adding Free Bee header if ([[MFFreebeeHandler sharedHandler] isFreeBeeEnabled] && [[MFFreebeeHandler sharedHandler] isValidCampaign]) { @@ -205,7 +181,10 @@ } NSError *bodyError; - NSData *body = [self createBodyForRequestUsingParameters:parameters requestParameters:requestParameters error:&bodyError]; + NSData *body = jsonData; + if (requestParameters.imageData) { + body = [self createMultipartFormBodyForRequestJsonData:jsonData imageData:requestParameters.imageData error:&bodyError]; + } if (body) { [request setHTTPBody:body]; } else { @@ -244,40 +223,67 @@ } } -- (NSData *)createBodyForRequestUsingParameters:(NSDictionary *)parameters requestParameters:(MVMCoreRequestParameters *)requestParameters error:(NSError **)error { +- (nullable NSData *)convertToJSON:(nonnull MVMCoreRequestParameters *)requestParameters forUrl:(nonnull NSURL *)url error:(MVMCoreErrorObject *_Nullable *_Nullable)error { + NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; + + // Sets up the Initial parameters. + if (requestParameters.addInitialRequestParameters) { + NSDictionary *initialParameters = [[MVMCoreSessionObject sharedGlobal] getInitialParametersExcludingSections:requestParameters.excludedInitialParameters]; + if (initialParameters) { + [parameters setObject:initialParameters forKey:@"InitialParams"]; + } + } + + // Adds request specific parameters + if (requestParameters.parameters.count) { + [parameters setObject:requestParameters.parameters forKey:@"RequestParams"]; + } + + // Ensure the parameters are valid json. + if (![NSJSONSerialization isValidJSONObject:parameters]) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + return nil; + } + +#if DEBUG + // Pretty print for logging the request parameters. + NSError *jsonError = nil; + NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; + if (!data) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + return nil; + } + NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + MVMCoreLog(@"Request Parameters for URL %@:\n%@", [url absoluteString], jsonString); +#endif + + // Standard condensed to send to the server. + data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&jsonError]; + if (!data) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + return nil; + } + + return data; +} + +- (nonnull NSData *)createMultipartFormBodyForRequestJsonData:(nonnull NSData *)jsonData imageData:(nonnull NSData *)imageData error:(NSError *_Nullable *_Nullable)error { NSMutableData *body = [[NSMutableData alloc] init]; - if (requestParameters.imageData) { - - NSData *imageData = requestParameters.imageData; - NSString *boundary = [self boundaryForMultipartRequest]; - [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", @"mfRequest"] dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[@"Content-Type: application/json\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; - - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:error]; - if (jsonData) { - [body appendData:jsonData]; - } else { - return nil; - } - [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"profileImage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[@"Content-Transfer-Encoding: base64\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:imageData]; - [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; - [body appendData:[[NSString stringWithFormat:@"--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; - } else { - NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:error]; - if (data) { - [body appendData:data]; - } else { - return nil; - } - } + NSString *boundary = [self boundaryForMultipartRequest]; + [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", @"mfRequest"] dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[@"Content-Type: application/json\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:jsonData]; + [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"profileImage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[@"Content-Transfer-Encoding: base64\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:imageData]; + [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; + [body appendData:[[NSString stringWithFormat:@"--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; return body; } From eec32c860eb20f204c9c1867150ae6b96911d724 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Mon, 31 Aug 2020 09:02:08 -0400 Subject: [PATCH 3/5] move var declaration outside debug scope. --- MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m index e253d1c..d1d9228 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m @@ -245,9 +245,9 @@ return nil; } + NSError *jsonError = nil; #if DEBUG // Pretty print for logging the request parameters. - NSError *jsonError = nil; NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; if (!data) { *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; From 26d1af94483d3c2c9d7a72c9688fd3d790afadcd Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Mon, 31 Aug 2020 14:08:30 -0400 Subject: [PATCH 4/5] code review --- MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h | 1 + MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h index c534baa..ea7c6eb 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h @@ -40,6 +40,7 @@ // Creates a request object with the given parameters. - (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error; +// Converts the core request parameters into the JSON body. - (nullable NSData *)convertToJSON:(nonnull MVMCoreRequestParameters *)requestParameters forUrl:(nonnull NSURL *)url error:(MVMCoreErrorObject *_Nullable *_Nullable)error; // Sends a given request to the server. When it is finished, it calls request finished, passing along the json object or nil if there is an error. diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m index d1d9228..efb3f83 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m @@ -241,7 +241,9 @@ // Ensure the parameters are valid json. if (![NSJSONSerialization isValidJSONObject:parameters]) { - *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + if (error) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + } return nil; } @@ -250,7 +252,9 @@ // Pretty print for logging the request parameters. NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; if (!data) { - *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + if (error) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + } return nil; } NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; @@ -260,7 +264,9 @@ // Standard condensed to send to the server. data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&jsonError]; if (!data) { - *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + if (error) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + } return nil; } From 257e7c238f6f3965e96087eec9c48c8376686729 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Tue, 1 Sep 2020 08:59:47 -0400 Subject: [PATCH 5/5] shift var outside debug block --- MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m index efb3f83..0392904 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m @@ -248,9 +248,10 @@ } NSError *jsonError = nil; + NSData *data = nil; #if DEBUG // Pretty print for logging the request parameters. - NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; + data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; if (!data) { if (error) { *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]];