// // Copyright 2010-2021 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 "AWSLexService.h" #import #import #import #import #import #import #import #import #import "AWSLexResources.h" #import "AWSLexRequestRetryHandler.h" #import "AWSLexSignature.h" static NSString *const AWSInfoLex = @"Lex"; NSString *const AWSLexSDKVersion = @"2.33.3"; @interface AWSLexResponseSerializer : AWSJSONResponseSerializer @end @implementation AWSLexResponseSerializer #pragma mark - Service errors static NSDictionary *errorCodeDictionary = nil; + (void)initialize { errorCodeDictionary = @{ @"BadGatewayException" : @(AWSLexErrorBadGateway), @"BadRequestException" : @(AWSLexErrorBadRequest), @"ConflictException" : @(AWSLexErrorConflict), @"DependencyFailedException" : @(AWSLexErrorDependencyFailed), @"InternalFailureException" : @(AWSLexErrorInternalFailure), @"LimitExceededException" : @(AWSLexErrorLimitExceeded), @"LoopDetectedException" : @(AWSLexErrorLoopDetected), @"NotAcceptableException" : @(AWSLexErrorNotAcceptable), @"NotFoundException" : @(AWSLexErrorNotFound), @"RequestTimeoutException" : @(AWSLexErrorRequestTimeout), @"UnsupportedMediaTypeException" : @(AWSLexErrorUnsupportedMediaType), }; } #pragma mark - - (id)responseObjectForResponse:(NSHTTPURLResponse *)response originalRequest:(NSURLRequest *)originalRequest currentRequest:(NSURLRequest *)currentRequest data:(id)data error:(NSError *__autoreleasing *)error { id responseObject = [super responseObjectForResponse:response originalRequest:originalRequest currentRequest:currentRequest data:data error:error]; if (!*error && [responseObject isKindOfClass:[NSDictionary class]]) { NSString *errorTypeString = [[response allHeaderFields] objectForKey:@"x-amzn-ErrorType"]; NSString *errorTypeHeader = [[errorTypeString componentsSeparatedByString:@":"] firstObject]; if ([errorTypeString length] > 0 && errorTypeHeader) { if (errorCodeDictionary[errorTypeHeader]) { if (error) { NSDictionary *userInfo = @{NSLocalizedDescriptionKey : [responseObject objectForKey:@"message"]?[responseObject objectForKey:@"message"]:[NSNull null], NSLocalizedFailureReasonErrorKey: errorTypeString}; *error = [NSError errorWithDomain:AWSLexErrorDomain code:[[errorCodeDictionary objectForKey:errorTypeHeader] integerValue] userInfo:userInfo]; } return responseObject; } else if (errorTypeHeader) { if (error) { NSDictionary *userInfo = @{NSLocalizedDescriptionKey : [responseObject objectForKey:@"message"]?[responseObject objectForKey:@"message"]:[NSNull null], NSLocalizedFailureReasonErrorKey: errorTypeString}; *error = [NSError errorWithDomain:AWSLexErrorDomain code:AWSLexErrorUnknown userInfo:userInfo]; } return responseObject; } } } if (!*error && response.statusCode/100 != 2) { *error = [NSError errorWithDomain:AWSLexErrorDomain code:AWSLexErrorUnknown userInfo:nil]; } if (!*error && [responseObject isKindOfClass:[NSDictionary class]]) { if (self.outputClass) { responseObject = [AWSMTLJSONAdapter modelOfClass:self.outputClass fromJSONDictionary:responseObject error:error]; } } return responseObject; } @end @interface AWSRequest() @property (nonatomic, strong) AWSNetworkingRequest *internalRequest; @end @interface AWSLex() @property (nonatomic, strong) AWSNetworking *networking; @property (nonatomic, strong) AWSServiceConfiguration *configuration; @end @interface AWSServiceConfiguration() @property (nonatomic, strong) AWSEndpoint *endpoint; @end @interface AWSEndpoint() - (void) setRegion:(AWSRegionType)regionType service:(AWSServiceType)serviceType; @end @implementation AWSLex + (void)initialize { [super initialize]; if (![AWSiOSSDKVersion isEqualToString:AWSLexSDKVersion]) { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"AWSCore and AWSLex versions need to match. Check your SDK installation. AWSCore: %@ AWSLex: %@", AWSiOSSDKVersion, AWSLexSDKVersion] userInfo:nil]; } } #pragma mark - Setup static AWSSynchronizedMutableDictionary *_serviceClients = nil; + (instancetype)defaultLex { static AWSLex *_defaultLex = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ AWSServiceConfiguration *serviceConfiguration = nil; AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoLex]; if (serviceInfo) { serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region credentialsProvider:serviceInfo.cognitoCredentialsProvider]; } if (!serviceConfiguration) { serviceConfiguration = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration; } if (!serviceConfiguration) { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"The service configuration is `nil`. You need to configure `awsconfiguration.json`, `Info.plist` or set `defaultServiceConfiguration` before using this method." userInfo:nil]; } _defaultLex = [[AWSLex alloc] initWithConfiguration:serviceConfiguration]; }); return _defaultLex; } + (void)registerLexWithConfiguration:(AWSServiceConfiguration *)configuration forKey:(NSString *)key { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _serviceClients = [AWSSynchronizedMutableDictionary new]; }); [_serviceClients setObject:[[AWSLex alloc] initWithConfiguration:configuration] forKey:key]; } + (instancetype)LexForKey:(NSString *)key { @synchronized(self) { AWSLex *serviceClient = [_serviceClients objectForKey:key]; if (serviceClient) { return serviceClient; } AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] serviceInfo:AWSInfoLex forKey:key]; if (serviceInfo) { AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region credentialsProvider:serviceInfo.cognitoCredentialsProvider]; [AWSLex registerLexWithConfiguration:serviceConfiguration forKey:key]; } return [_serviceClients objectForKey:key]; } } + (void)removeLexForKey:(NSString *)key { [_serviceClients removeObjectForKey:key]; } - (instancetype)init { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"`- init` is not a valid initializer. Use `+ defaultLex` or `+ LexForKey:` instead." userInfo:nil]; return nil; } #pragma mark - - (instancetype)initWithConfiguration:(AWSServiceConfiguration *)configuration { if (self = [super init]) { _configuration = [configuration copy]; if(!configuration.endpoint){ _configuration.endpoint = [[AWSEndpoint alloc] initWithRegion:_configuration.regionType service:AWSServiceLexRuntime useUnsafeURL:NO]; }else{ [_configuration.endpoint setRegion:_configuration.regionType service:AWSServiceLexRuntime]; } AWSLexSignature *signer = [[AWSLexSignature alloc] initWithCredentialsProvider:_configuration.credentialsProvider endpoint:_configuration.endpoint]; AWSNetworkingRequestInterceptor *baseInterceptor = [[AWSNetworkingRequestInterceptor alloc] initWithUserAgent:_configuration.userAgent]; _configuration.requestInterceptors = @[baseInterceptor, signer]; _configuration.baseURL = _configuration.endpoint.URL; _configuration.retryHandler = [[AWSLexRequestRetryHandler alloc] initWithMaximumRetryCount:_configuration.maxRetryCount]; _configuration.headers = @{@"Content-Type" : @"application/x-amz-json-1.1"}; _networking = [[AWSNetworking alloc] initWithConfiguration:_configuration]; } return self; } - (AWSTask *)invokeRequest:(AWSRequest *)request HTTPMethod:(AWSHTTPMethod)HTTPMethod URLString:(NSString *) URLString targetPrefix:(NSString *)targetPrefix operationName:(NSString *)operationName outputClass:(Class)outputClass { @autoreleasepool { if (!request) { request = [AWSRequest new]; } AWSNetworkingRequest *networkingRequest = request.internalRequest; if (request) { networkingRequest.parameters = [[AWSMTLJSONAdapter JSONDictionaryFromModel:request] aws_removeNullValues]; } else { networkingRequest.parameters = @{}; } networkingRequest.HTTPMethod = HTTPMethod; networkingRequest.requestSerializer = [[AWSJSONRequestSerializer alloc] initWithJSONDefinition:[[AWSLexResources sharedInstance] JSONObject] actionName:operationName]; networkingRequest.responseSerializer = [[AWSLexResponseSerializer alloc] initWithJSONDefinition:[[AWSLexResources sharedInstance] JSONObject] actionName:operationName outputClass:outputClass]; return [self.networking sendRequest:networkingRequest]; } } #pragma mark - Service method - (AWSTask *)deleteSession:(AWSLexDeleteSessionRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodDELETE URLString:@"/bot/{botName}/alias/{botAlias}/user/{userId}/session" targetPrefix:@"" operationName:@"DeleteSession" outputClass:[AWSLexDeleteSessionResponse class]]; } - (void)deleteSession:(AWSLexDeleteSessionRequest *)request completionHandler:(void (^)(AWSLexDeleteSessionResponse *response, NSError *error))completionHandler { [[self deleteSession:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSLexDeleteSessionResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)getSession:(AWSLexGetSessionRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodGET URLString:@"/bot/{botName}/alias/{botAlias}/user/{userId}/session/" targetPrefix:@"" operationName:@"GetSession" outputClass:[AWSLexGetSessionResponse class]]; } - (void)getSession:(AWSLexGetSessionRequest *)request completionHandler:(void (^)(AWSLexGetSessionResponse *response, NSError *error))completionHandler { [[self getSession:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSLexGetSessionResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)postContent:(AWSLexPostContentRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/bot/{botName}/alias/{botAlias}/user/{userId}/content" targetPrefix:@"" operationName:@"PostContent" outputClass:[AWSLexPostContentResponse class]]; } - (void)postContent:(AWSLexPostContentRequest *)request completionHandler:(void (^)(AWSLexPostContentResponse *response, NSError *error))completionHandler { [[self postContent:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSLexPostContentResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)postText:(AWSLexPostTextRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/bot/{botName}/alias/{botAlias}/user/{userId}/text" targetPrefix:@"" operationName:@"PostText" outputClass:[AWSLexPostTextResponse class]]; } - (void)postText:(AWSLexPostTextRequest *)request completionHandler:(void (^)(AWSLexPostTextResponse *response, NSError *error))completionHandler { [[self postText:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSLexPostTextResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)putSession:(AWSLexPutSessionRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/bot/{botName}/alias/{botAlias}/user/{userId}/session" targetPrefix:@"" operationName:@"PutSession" outputClass:[AWSLexPutSessionResponse class]]; } - (void)putSession:(AWSLexPutSessionRequest *)request completionHandler:(void (^)(AWSLexPutSessionResponse *response, NSError *error))completionHandler { [[self putSession:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSLexPutSessionResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } #pragma mark - @end