/* Copyright 2010-2015 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 "PETLambdaMicroserviceClient.h" #import #import #import #import "PETLoginUserResponse.h" #import "PETRegisterUserRequest.h" #import "PETListPetsResponse.h" #import "PETCreatePetResponse.h" #import "PETCreatePetRequest.h" #import "PETGetPetResponse.h" #import "PETRegisterUserResponse.h" #import "PETError.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 PETLambdaMicroserviceClient() @property (nonatomic, strong) AWSServiceConfiguration *configuration; @end @interface AWSServiceConfiguration() @property (nonatomic, strong) AWSEndpoint *endpoint; @end @implementation PETLambdaMicroserviceClient @synthesize configuration = _configuration; static AWSSynchronizedMutableDictionary *_serviceClients = nil; + (instancetype)defaultClient { AWSServiceConfiguration *serviceConfiguration = nil; if ([AWSServiceManager defaultServiceManager].defaultServiceConfiguration) { serviceConfiguration = AWSServiceManager.defaultServiceManager.defaultServiceConfiguration; } else { serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUnknown credentialsProvider:nil]; } static PETLambdaMicroserviceClient *_defaultClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _defaultClient = [[PETLambdaMicroserviceClient 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:[[PETLambdaMicroserviceClient alloc] initWithConfiguration:configuration] forKey:key]; } + (instancetype)clientForKey:(NSString *)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]; // TODO: Change this to match your API deployment in Amazon API Gateway NSString *URLString = @"https://xxxxxxxxxxx-api.us-east-1.amazonaws.com/xxxx"; if ([URLString hasSuffix:@"/"]) { URLString = [URLString substringToIndex:[URLString length] - 1]; } _configuration.endpoint = [[AWSEndpoint alloc] initWithRegion:_configuration.regionType service:AWSServiceAPIGateway URL:[NSURL URLWithString:URLString]]; AWSSignatureV4Signer *signer = [AWSSignatureV4Signer signerWithCredentialsProvider:_configuration.credentialsProvider endpoint:_configuration.endpoint]; _configuration.baseURL = _configuration.endpoint.URL; _configuration.requestInterceptors = @[[AWSNetworkingRequestInterceptor new], signer]; } return self; } - (AWSTask *)loginPost:(PETRegisterUserRequest *)body { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"POST" URLString:@"/login" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:body responseClass:[PETLoginUserResponse class]]; } - (AWSTask *)petsGet { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"GET" URLString:@"/pets" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:nil responseClass:[PETListPetsResponse class]]; } - (AWSTask *)petsPost:(PETCreatePetRequest *)body { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"POST" URLString:@"/pets" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:body responseClass:[PETCreatePetResponse class]]; } - (AWSTask *)petsPetIdGet:(NSString *)petId { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ @"petId": petId }; return [self invokeHTTPRequest:@"GET" URLString:@"/pets/{petId}" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:nil responseClass:[PETGetPetResponse class]]; } - (AWSTask *)usersPost:(PETRegisterUserRequest *)body { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"POST" URLString:@"/users" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:body responseClass:[PETRegisterUserResponse class]]; } @end