/* 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 "APINotesApiClient.h" #import #import #import #import "APICreateNoteRequest.h" #import "APICreateNoteResponse.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 APINotesApiClient() @property (nonatomic, strong) AWSServiceConfiguration *configuration; @end @interface AWSServiceConfiguration() @property (nonatomic, strong) AWSEndpoint *endpoint; @end @implementation APINotesApiClient @synthesize configuration = _configuration; static AWSSynchronizedMutableDictionary *_serviceClients = nil; + (void)registerClientWithConfiguration:(AWSServiceConfiguration *)configuration forKey:(NSString *)key withUrl:(NSString*) url { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _serviceClients = [AWSSynchronizedMutableDictionary new]; }); [_serviceClients setObject:[[APINotesApiClient alloc] initWithConfiguration:configuration withUrl:url] 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 withUrl:(NSString *)URLString { if (self = [super init]) { _configuration = [configuration copy]; 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 *)notesPost:(APICreateNoteRequest *)body { NSDictionary *headerParameters = @{ @"Content-Type": @"application/json", @"Accept": @"application/json", }; NSDictionary *queryParameters = @{ }; NSDictionary *pathParameters = @{ }; return [self invokeHTTPRequest:@"POST" URLString:@"/notes" pathParameters:pathParameters queryParameters:queryParameters headerParameters:headerParameters body:body responseClass:[APICreateNoteResponse class]]; } @end