Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit 1b1d74b

Browse files
author
Vikas Dadheech
committed
Add method to create HTTPClient with custom session configuration and 3 more base URLs for different sovereign cloud.
1 parent 14246b0 commit 1b1d74b

File tree

8 files changed

+83
-5
lines changed

8 files changed

+83
-5
lines changed

MSGraphClientSDK/MSGraphClientSDK/Common/MSConstants.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ typedef NS_ENUM(NSInteger, MSMiddlewareOptionsType)
5353
};
5454

5555
extern NSString *const MSGraphBaseURL;
56+
extern NSString *const MSGraphChinaBaseURL;
57+
extern NSString *const MSGraphUSBaseURL;
58+
extern NSString *const MSGraphGermanyBaseURL;
59+
5660
extern NSString *const MSHeaderSdkVersion;
5761
extern NSString *const MSGraphiOSSdkVersionHeaderPrefix;
5862
extern NSString *const MSGraphMacSdkVersionHeaderPrefix;

MSGraphClientSDK/MSGraphClientSDK/Common/MSConstants.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
#import "MSConstants.h"
66

7-
NSString *const MSGraphBaseURL = @"https://graph.microsoft.com/v1.0";
7+
NSString *const MSGraphBaseURL = @"https://graph.microsoft.com/v1.0";
8+
NSString *const MSGraphChinaBaseURL = @"https://microsoftgraph.chinacloudapi.cn/v1.0";
9+
NSString *const MSGraphUSBaseURL = @"https://graph.microsoft.us/v1.0";
10+
NSString *const MSGraphGermanyBaseURL = @"https://graph.microsoft.de/v1.0";
11+
812
NSString *const MSHeaderSdkVersion = @"SdkVersion";
913
NSString *const MSGraphiOSSdkVersionHeaderPrefix = @"graph-objc-ios-";
1014
NSString *const MSGraphMacSdkVersionHeaderPrefix = @"graph-objc-mac-";

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSClientFactory.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@
1212
Initializes and returns an instance of MSHTTPClient class with a default chain of middleware to handle the HTTP calls.
1313
1414
@param authenticationProvider Instance of the class which implements the methods of MSAuthenticationProvider
15-
@param baseUrl Base URL of all the network calls which will be made my this client.
15+
@return MSHTTPClient instance
1616
*/
1717

1818
+(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthenticationProvider>)authenticationProvider;
1919

20+
/*
21+
Initializes and returns an instance of MSHTTPClient class with a default chain of middleware to handle the HTTP calls.
22+
23+
@param authenticationProvider Instance of the class which implements the methods of MSAuthenticationProvider
24+
@param sessionConfiguration Instance of NSURLSessionConfiguration which will be used to create the NSURLSession for this client Instance.
25+
@return MSHTTPClient instance
26+
*/
27+
+(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthenticationProvider>)authenticationProvider andSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration;
2028
/*
2129
Initializes and returns an instance of MSHTTPClient class with a custom chain of middleware to handle the HTTP calls.
2230
2331
@param middleware Instance of a class which will be the first node in the custom chain of middleware.
32+
@return MSHTTPClient instance
2433
*/
2534

2635
+(MSHTTPClient *)createHTTPClientWithMiddleware:(id<MSGraphMiddleware>)middleware;

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSClientFactory.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ +(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthentication
3131
return [MSClientFactory createHTTPClientWithMiddleware:authenticationHandler];
3232
}
3333

34+
+(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthenticationProvider>)authenticationProvider andSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration
35+
{
36+
NSParameterAssert(authenticationProvider);
37+
NSParameterAssert(sessionConfiguration);
38+
39+
//Creating a default chain of middlewares starting from Authentication
40+
41+
//Initializing different default middlewares
42+
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
43+
authenticationHandler.authProvider = authenticationProvider;
44+
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
45+
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
46+
47+
//Create session manager with custom session configuration
48+
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
49+
50+
//Creating a default chain
51+
[authenticationHandler setNext:redirectHandler];
52+
[redirectHandler setNext:retryHandler];
53+
[retryHandler setNext:sessionManager];
54+
55+
return [MSClientFactory createHTTPClientWithMiddleware:authenticationHandler];
56+
}
57+
3458
+(MSHTTPClient *)createHTTPClientWithMiddleware:(id<MSGraphMiddleware>)middleware
3559
{
3660
NSParameterAssert(middleware);

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSMiddlewareFactory.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ @implementation MSMiddlewareFactory
1616
{
1717
case MSMiddlewareTypeHTTP:
1818
{
19-
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
19+
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] init];
2020
return sessionManager;
2121
}
2222
case MSMiddlewareTypeRedirect:

MSGraphClientSDK/MSGraphClientSDK/Middleware/Implementations/HTTPProvider/MSURLSessionManager.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ @interface MSURLSessionManager()
4242

4343
@implementation MSURLSessionManager
4444

45+
- (instancetype)init
46+
{
47+
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
48+
sessionConfiguration.timeoutIntervalForRequest = 100;
49+
return [self initWithSessionConfiguration:sessionConfiguration];
50+
}
51+
4552
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)urlSessionConfiguration
4653
{
4754
self = [super init];

MSGraphClientSDK/MSGraphClientSDKTests/HTTPClient/MSClientFactoryTests.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,25 @@ - (void)tearDown {
2121
[super tearDown];
2222
}
2323

24-
- (void)testCreationOfHTTPClient{
24+
- (void)testCreationOfHTTPClientWithNilValue {
2525
XCTAssertThrows([MSClientFactory createHTTPClientWithAuthenticationProvider:nil]);
2626
XCTAssertThrows([MSClientFactory createHTTPClientWithMiddleware:nil]);
27+
XCTAssertThrows([MSClientFactory createHTTPClientWithAuthenticationProvider:self.mockAuthProvider andSessionConfiguration:nil]);
28+
}
2729

30+
- (void)testCreationOfHTTPClientWithAuthProvider {
2831
MSHTTPClient *defaulClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:self.mockAuthProvider];
2932
XCTAssertNotNil(defaulClient);
33+
}
3034

35+
- (void)testCreationOfHTTPClientWithMiddleware {
3136
MSHTTPClient *customClient = [MSClientFactory createHTTPClientWithMiddleware:OCMProtocolMock(@protocol(MSGraphMiddleware))];
3237
XCTAssertNotNil(customClient);
3338
}
39+
40+
- (void)testCreationOfHTTPClientWithAuthProviderAndSessionConfiguration {
41+
MSHTTPClient *defaulClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:self.mockAuthProvider andSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
42+
XCTAssertNotNil(defaulClient);
43+
}
44+
3445
@end

MSGraphClientSDK/MSGraphClientSDKTests/Middleware/Implementations/MSURLSessionManagerTests.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,36 @@ - (void)tearDown {
5656

5757
}
5858

59+
- (void)testMSURLSessionManagerInit {
60+
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] init];
61+
XCTAssertEqual(sessionManager.urlSessionConfiguration.timeoutIntervalForRequest,100);
62+
}
63+
5964
- (void)testMSURLSessionManagerInitWithNilconfig{
6065
MSURLSessionManager * sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:nil];
6166
XCTAssertNotNil(sessionManager);
6267
XCTAssertNotNil(sessionManager.urlSession.configuration);
6368
XCTAssertEqualObjects(sessionManager.urlSession.delegate, sessionManager);
6469
}
65-
- (void)testMSURLSessionManagerInit{
70+
- (void)testMSURLSessionManagerInitWithCustomConfig{
71+
NSString* proxyHost = @"127.0.0.1";
72+
NSNumber* proxyPort = [NSNumber numberWithInt: 8888];
73+
// Create an NSURLSessionConfiguration that uses the proxy
74+
NSDictionary *proxyDict = @{
75+
@"HTTPSEnable":@1,
76+
@"HTTPSProxy":proxyHost,
77+
@"HTTPSPort":proxyPort
78+
};
6679
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
80+
config.connectionProxyDictionary = proxyDict;
81+
config.timeoutIntervalForRequest = 10;
6782
MSURLSessionManager * sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:config];
6883
XCTAssertNotNil(sessionManager);
6984
XCTAssertNotNil(sessionManager.urlSessionConfiguration);
85+
XCTAssertEqual([sessionManager.urlSessionConfiguration.connectionProxyDictionary objectForKey:@"HTTPSProxy"], proxyHost);
86+
XCTAssertEqual([sessionManager.urlSessionConfiguration.connectionProxyDictionary objectForKey:@"HTTPSPort"], proxyPort);
87+
XCTAssertEqual([sessionManager.urlSessionConfiguration.connectionProxyDictionary objectForKey:@"HTTPSEnable"], @1);
88+
XCTAssertEqual([sessionManager.urlSessionConfiguration timeoutIntervalForRequest], 10);
7089
XCTAssertEqualObjects(sessionManager.urlSession.delegate, sessionManager);
7190
}
7291

0 commit comments

Comments
 (0)