/* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0 or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #import "AWSLambdaMicroserviceClient.h" #import #import #import #import "AWSStringValue.h" @interface AWSAPIGatewayClient() // Networking @property (nonatomic, strong) NSURLSession *session; // For requests @property (nonatomic, strong) NSURL *baseURL; // For responses @property (nonatomic, strong) NSDictionary *HTTPHeaderFields; @property (nonatomic, assign) NSInteger HTTPStatusCode; - (AWSTask *)invokeHTTPRequest:(NSString *)HTTPMethod URLString:(NSString *)URLString pathParameters:(NSDictionary *)pathParameters queryParameters:(NSDictionary *)queryParameters headerParameters:(NSDictionary *)headerParameters body:(id)body responseClass:(Class)responseClass; @end @interface AWSLambdaMicroserviceClient() @property (nonatomic, strong) AWSServiceConfiguration *configuration; @end @interface AWSServiceConfiguration() @property (nonatomic, strong) AWSEndpoint *endpoint; @end @implementation AWSLambdaMicroserviceClient static NSString *const AWSInfoClientKey = @"AWSLambdaMicroserviceClient"; @synthesize configuration = _configuration; static AWSSynchronizedMutableDictionary *_serviceClients = nil; + (instancetype)defaultClient:(NSString *)endpointURL { AWSServiceConfiguration *serviceConfiguration = nil; AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoClientKey]; if (serviceInfo) { serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region credentialsProvider:serviceInfo.cognitoCredentialsProvider]; } else if ([AWSServiceManager defaultServiceManager].defaultServiceConfiguration) { serviceConfiguration = AWSServiceManager.defaultServiceManager.defaultServiceConfiguration; } else { serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUnknown credentialsProvider:nil]; } if ([endpointURL hasSuffix:@"/"]) { endpointURL = [endpointURL substringToIndex:[endpointURL length] - 1]; } serviceConfiguration.endpoint = [[AWSEndpoint alloc] initWithRegion:serviceConfiguration.regionType service:AWSServiceAPIGateway URL:[NSURL URLWithString:endpointURL]]; static AWSLambdaMicroserviceClient *_defaultClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _defaultClient = [[AWSLambdaMicroserviceClient alloc] initWithConfiguration:serviceConfiguration]; }); return _defaultClient; } + (void)registerClientWithConfiguration:(AWSServiceConfiguration *)configuration forKey:(NSString *)key { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _serviceClients = [AWSSynchronizedMutableDictionary new]; }); [_serviceClients setObject:[[AWSLambdaMicroserviceClient alloc] initWithConfiguration:configuration] forKey:key]; } + (instancetype)clientForKey:(NSString *)key { @synchronized(self) { AWSLambdaMicroserviceClient *serviceClient = [_serviceClients objectForKey:key]; if (serviceClient) { return serviceClient; } AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] serviceInfo:AWSInfoClientKey forKey:key]; if (serviceInfo) { AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region credentialsProvider:serviceInfo.cognitoCredentialsProvider]; [AWSLambdaMicroserviceClient registerClientWithConfiguration:serviceConfiguration forKey:key]; } return [_serviceClients objectForKey:key]; } } + (void)removeClientForKey:(NSString *)key { [_serviceClients removeObjectForKey:key]; } - (instancetype)init { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"`- init` is not a valid initializer. Use `+ defaultClient` or `+ clientForKey:` instead." userInfo:nil]; return nil; } - (instancetype)initWithConfiguration:(AWSServiceConfiguration *)configuration { if (self = [super init]) { _configuration = [configuration copy]; AWSSignatureV4Signer *signer = [[AWSSignatureV4Signer alloc] initWithCredentialsProvider:_configuration.credentialsProvider endpoint:_configuration.endpoint]; _configuration.baseURL = _configuration.endpoint.URL; _configuration.requestInterceptors = @[[AWSNetworkingRequestInterceptor new], signer]; } return self; } - (AWSTask *)helloWorldGet { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"GET" URLString:@"/HelloWorld" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:nil responseClass:nil]; } - (AWSTask *)testFunctionGet:(NSString *)key1 { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ @"key1": key1 }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"GET" URLString:@"/TestFunction" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:nil responseClass:[AWSStringValue class]]; } - (AWSTask *)testFunctionPost { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"POST" URLString:@"/TestFunction" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:nil responseClass:nil]; } - (AWSTask *)userUsernameGet:(NSString *)username { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ @"username": username }; return [self invokeHTTPRequest:@"GET" URLString:@"/user/{username}" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:nil responseClass:[AWSStringValue class]]; } @end