Move URL request resolution to MVMCoreRequestParameters.

This commit is contained in:
Kyle Matthew Hedden 2023-04-19 20:03:47 -04:00
parent 5ee5c39ff8
commit 14af9f2d5f
4 changed files with 63 additions and 25 deletions

View File

@ -134,31 +134,7 @@
- (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error {
NSURL *url = requestParameters.URL;
if (!url) {
if (requestParameters.alternateBaseURL) {
url = requestParameters.alternateBaseURL;
} else {
url = [MVMCoreSessionObject sharedGlobal].baseURL ?: [NSURL URLWithString:URLProdPostpayBase];
}
// Appends the context root.
if (requestParameters.contextRoot) {
url = [url URLByAppendingPathComponent:requestParameters.contextRoot];
} else if ([MVMCoreSessionObject sharedGlobal].contextRoot) {
url = [url URLByAppendingPathComponent:[MVMCoreSessionObject sharedGlobal].contextRoot];
}
// Appends the page type
if (requestParameters.pageType) {
url = [url URLByAppendingPathComponent:requestParameters.pageType];
}
// This has changed since the initial agreement. Seems server always needs page type now.
/* else if (requestParameters.modules) {
url = [url URLByAppendingPathComponent:KeyModuleMap];
}*/
}
NSURL *url = [requestParameters resolveURL:[MVMCoreSessionObject sharedGlobal]];
// Adds modules needed to the request parameters.
if (requestParameters.modules.count > 0) {

View File

@ -111,6 +111,8 @@
- (void)main {
MVMCoreLog(@"Load Operation begun for page type %@, background load %@, delegate %@", self.requestParameters.pageType, @(self.backgroundLoad),self.delegateObject.loadDelegate);
[self.requestParameters resolveURL:[MVMCoreSessionObject sharedGlobal]];
// Always check for cancellation before launching the task.
if ([self checkAndHandleForCancellation]) {
return;

View File

@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MVMCore/MVMCoreSessionObject.h>
// The loading style.
// MFLoadStyleDefault: This means it has not been explicitely set by the developer. Standard push.
@ -124,4 +125,7 @@ typedef NS_ENUM(NSInteger, MFLoadStyle) {
/// Returns optional and required modules
- (nullable NSArray<NSString *> *)allModules;
/// Resolves the URL given the session object and the current request parameters.
- (nonnull NSURL *)resolveURL:(nonnull MVMCoreSessionObject *)sessionObject;
@end

View File

@ -10,15 +10,20 @@
#import "NSDictionary+MFConvenience.h"
#import "MVMCoreJSONConstants.h"
#import "MVMCoreViewControllerMappingObject.h"
#import "MVMCoreConstants.h"
@interface MVMCoreRequestParameters ()
@property (nonatomic, strong, nullable) NSURL *determinedURL;
- (nullable instancetype)initWithExtraParameters:(nullable NSDictionary *)extraParameters;
@end
@implementation MVMCoreRequestParameters
@synthesize URL = _url;
- (nullable instancetype)initWithExtraParameters:(nullable NSDictionary *)extraParameters {
if (self = [super init]) {
self.parameters = extraParameters;
@ -86,6 +91,56 @@
return self;
}
- (void)setAlternateBaseURL:(NSURL *)alternateBaseURL {
_alternateBaseURL = alternateBaseURL;
_url = self.determinedURL; // Reset resolution when changed.
}
- (void)setPageType:(NSString *)pageType {
_pageType = pageType;
_url = self.determinedURL; // Reset resolution when changed.
}
- (void)setContextRoot:(NSString *)contextRoot {
_contextRoot = contextRoot;
_url = self.determinedURL; // Reset resolution when changed.
}
- (void)setURL:(NSURL *)URL {
self.determinedURL = URL; // If set directly, this becomes the ultimate URL.
_url = URL;
}
- (NSURL *)resolveURL:(MVMCoreSessionObject *)sessionObject {
if (self.URL) {
// Previously resovled.
return self.URL;
}
NSURL *url;
if (self.alternateBaseURL) {
url = self.alternateBaseURL;
} else {
url = sessionObject.baseURL ?: [NSURL URLWithString:URLProdPostpayBase];
}
// Appends the context root.
if (self.contextRoot) {
url = [url URLByAppendingPathComponent:self.contextRoot];
} else if (sessionObject.contextRoot) {
url = [url URLByAppendingPathComponent:[MVMCoreSessionObject sharedGlobal].contextRoot];
}
// Appends the page type
if (self.pageType) {
url = [url URLByAppendingPathComponent:self.pageType];
}
_url = url;
return url;
}
- (void)addRequestParameters:(nonnull NSDictionary *)parameters {
if ([parameters count] > 0) {
@ -152,6 +207,7 @@
copyObject.customTimeoutTime = self.customTimeoutTime;
copyObject.backgroundRequest = self.backgroundRequest;
copyObject.URL = self.URL;
copyObject.determinedURL = self.determinedURL;
copyObject.actionMap = self.actionMap;
return copyObject;
}