From 8794cd6ee9ff3c043fe33dc8361e98e1fdf57c94 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Thu, 27 Aug 2020 17:57:01 -0400 Subject: [PATCH] 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; }