Skip to content

Commit

Permalink
Custom header support. (parse-community#1250)
Browse files Browse the repository at this point in the history
* Added custom URL Session configuration

* Set the URLSesssionConfiguration to the default one.
  • Loading branch information
mt81 authored and flovilmart committed Feb 1, 2018
1 parent 61f3320 commit 5d0341c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ + (instancetype)constructorWithDataSource:(id<PFInstallationIdentifierStoreProvi
path:command.httpPath
query:nil];
NSDictionary *headers = task.result;
NSURLSessionConfiguration *configuration = Parse._currentManager.configuration.URLSessionConfiguration;
if (configuration && [configuration.HTTPAdditionalHeaders count]) {
NSMutableDictionary *sessionConfigurationHeaders = [configuration.HTTPAdditionalHeaders mutableCopy];
[sessionConfigurationHeaders addEntriesFromDictionary:headers];
headers = sessionConfigurationHeaders;
}

NSString *requestMethod = command.httpMethod;
NSDictionary *requestParameters = nil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "PFRESTCommand.h"
#import "PFURLConstructor.h"
#import "PFURLSession.h"
#import "Parse_Private.h"

@interface PFURLSessionCommandRunner () <PFURLSessionDelegate>

Expand Down Expand Up @@ -261,7 +262,7 @@ - (BFTask *)_performCommandRunningBlock:(nonnull id (^)(void))block

+ (NSURLSessionConfiguration *)_urlSessionConfigurationForApplicationId:(NSString *)applicationId
clientKey:(nullable NSString *)clientKey {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSessionConfiguration *configuration = Parse._currentManager.configuration.URLSessionConfiguration;

// No cookies, they are bad for you.
configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
Expand All @@ -276,6 +277,12 @@ + (NSURLSessionConfiguration *)_urlSessionConfigurationForApplicationId:(NSStrin
NSDictionary *headers = [PFCommandURLRequestConstructor defaultURLRequestHeadersForApplicationId:applicationId
clientKey:clientKey
bundle:bundle];
if (configuration && [configuration.HTTPAdditionalHeaders count]) {
NSMutableDictionary *sessionConfigurationHeaders = [configuration.HTTPAdditionalHeaders mutableCopy];
[sessionConfigurationHeaders addEntriesFromDictionary:headers];
headers = sessionConfigurationHeaders;
}

configuration.HTTPAdditionalHeaders = headers;

return configuration;
Expand Down
1 change: 1 addition & 0 deletions Parse/Parse/Internal/ParseClientConfiguration_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern NSString *const _ParseDefaultServerURLString;

@property (nullable, nonatomic, copy, readwrite) NSString *applicationGroupIdentifier;
@property (nullable, nonatomic, copy, readwrite) NSString *containingApplicationBundleIdentifier;
@property (nonatomic, strong, readwrite) NSURLSessionConfiguration *URLSessionConfiguration;

@property (nonatomic, assign, readwrite) NSUInteger networkRetryAttempts;

Expand Down
16 changes: 14 additions & 2 deletions Parse/Parse/ParseClientConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, copy) NSString *containingApplicationBundleIdentifier PF_TV_UNAVAILABLE PF_WATCH_UNAVAILABLE;

///--------------------------------------
#pragma mark - Other Properties
#pragma mark - Network Properties
///--------------------------------------

/**
A custom NSURLSessionConfiguration configuration that will be used from the SDK.
*/
@property (nonatomic, strong) NSURLSessionConfiguration *URLSessionConfiguration;

/**
The maximum number of retry attempts to make upon a failed network request.
*/
Expand Down Expand Up @@ -163,9 +168,16 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, copy, readonly) NSString *containingApplicationBundleIdentifier;

///--------------------------------------
#pragma mark - Other Properties
#pragma mark - Network Properties
///--------------------------------------

/**
The NSURLSessionConfiguration configuration used by the SDK.
The default value is NSURLSessionConfiguration.defaultSessionConfiguration
*/
@property (nonatomic, strong, readonly) NSURLSessionConfiguration *URLSessionConfiguration;

/**
The maximum number of retry attempts to make upon a failed network request.
*/
Expand Down
3 changes: 3 additions & 0 deletions Parse/Parse/ParseClientConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (instancetype)initEmpty {
if (!self) return nil;

_networkRetryAttempts = PFCommandRunningDefaultMaxAttemptsCount;
_URLSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
_server = [_ParseDefaultServerURLString copy];

return self;
Expand Down Expand Up @@ -116,6 +117,7 @@ - (BOOL)isEqual:(id)object {
self.localDatastoreEnabled == other.localDatastoreEnabled &&
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
[PFObjectUtilities isObject:self.URLSessionConfiguration equalToObject:other.URLSessionConfiguration] &&
self.networkRetryAttempts == other.networkRetryAttempts);
}

Expand All @@ -134,6 +136,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];
configuration->_networkRetryAttempts = self->_networkRetryAttempts;
configuration->_URLSessionConfiguration = self->_URLSessionConfiguration;
}];
}

Expand Down

0 comments on commit 5d0341c

Please sign in to comment.