Skip to content

Commit

Permalink
Making NSURLSession extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
shahmharsh committed Oct 31, 2017
1 parent 96ea3e3 commit 287a07a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions Source/AppAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "OIDTokenRequest.h"
#import "OIDTokenResponse.h"
#import "OIDTokenUtilities.h"
#import "OIDURLSessionProvider.h"

#if TARGET_OS_TV
#elif TARGET_OS_WATCH
Expand Down
7 changes: 4 additions & 3 deletions Source/OIDAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#import "OIDTokenRequest.h"
#import "OIDTokenResponse.h"
#import "OIDURLQueryComponent.h"
#import "OIDURLSessionProvider.h"

/*! @brief Path appended to an OpenID Connect issuer for discovery
@see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig
Expand Down Expand Up @@ -191,7 +192,7 @@ + (void)discoverServiceConfigurationForIssuer:(NSURL *)issuerURL
+ (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL
completion:(OIDDiscoveryCallback)completion {

NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session = [OIDURLSessionProvider session];
NSURLSessionDataTask *task =
[session dataTaskWithURL:discoveryURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
Expand Down Expand Up @@ -261,7 +262,7 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL

+ (void)performTokenRequest:(OIDTokenRequest *)request callback:(OIDTokenCallback)callback {
NSURLRequest *URLRequest = [request URLRequest];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session = [OIDURLSessionProvider session];
[[session dataTaskWithRequest:URLRequest
completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
Expand Down Expand Up @@ -372,7 +373,7 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request
return;
}

NSURLSession *session = [NSURLSession sharedSession];
NSURLSession *session = [OIDURLSessionProvider session];
[[session dataTaskWithRequest:URLRequest
completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
Expand Down
24 changes: 24 additions & 0 deletions Source/OIDURLSessionProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/*! @brief A NSURLSession coordinator that allows clients to provide custom implementation for NSURLSession
*/
@interface OIDURLSessionProvider : NSObject

/*! @internal
@brief Unavailable. This class should not be initialized.
*/
-(instancetype)init NS_UNAVAILABLE;

/*! @brief Obtains the current @c NSURLSession; using the +[NSURLSession sharedSession] if no custom implementation provided
@return NSURLSession object to be used for making network requests
*/
+(NSURLSession *)session;

/*! @brief Allows library consumers to change the @c NSURLSession used to create make network requests
@param session The @c NSURLSession instance that should be used for making network requests
*/
+(void)setURLSession:(NSURLSession *)session;
@end
NS_ASSUME_NONNULL_END
21 changes: 21 additions & 0 deletions Source/OIDURLSessionProvider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#import "OIDURLSessionProvider.h"

NS_ASSUME_NONNULL_BEGIN

static NSURLSession * __nullable gURLSession;

@implementation OIDURLSessionProvider

+(NSURLSession *)session {
if (!gURLSession) {
gURLSession = [NSURLSession sharedSession];
}
return gURLSession;
}

+(void)setURLSession:(NSURLSession *)session {
NSAssert(session, @"Parameter: |session| must be non-nil.");
gURLSession = session;
}
@end
NS_ASSUME_NONNULL_END

0 comments on commit 287a07a

Please sign in to comment.