refactor for clarity and removing repetition.
This commit is contained in:
parent
589fa3112c
commit
8794cd6ee9
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user