// // Copyright 2010-2023 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 "AWSSageMakerRuntimeService.h" #import #import #import #import #import #import #import #import #import "AWSSageMakerRuntimeResources.h" static NSString *const AWSInfoSageMakerRuntime = @"SageMakerRuntime"; NSString *const AWSSageMakerRuntimeSDKVersion = @"2.33.3"; @interface AWSSageMakerRuntimeResponseSerializer : AWSJSONResponseSerializer @end @implementation AWSSageMakerRuntimeResponseSerializer #pragma mark - Service errors static NSDictionary *errorCodeDictionary = nil; + (void)initialize { errorCodeDictionary = @{ @"InternalDependencyException" : @(AWSSageMakerRuntimeErrorInternalDependency), @"InternalFailure" : @(AWSSageMakerRuntimeErrorInternalFailure), @"ModelError" : @(AWSSageMakerRuntimeErrorModel), @"ModelNotReadyException" : @(AWSSageMakerRuntimeErrorModelNotReady), @"ServiceUnavailable" : @(AWSSageMakerRuntimeErrorServiceUnavailable), @"ValidationError" : @(AWSSageMakerRuntimeErrorValidation), }; } #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:AWSSageMakerRuntimeErrorDomain 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:AWSSageMakerRuntimeErrorDomain code:AWSSageMakerRuntimeErrorUnknown userInfo:userInfo]; } return responseObject; } } } if (!*error && response.statusCode/100 != 2) { *error = [NSError errorWithDomain:AWSSageMakerRuntimeErrorDomain code:AWSSageMakerRuntimeErrorUnknown userInfo:nil]; } if (!*error && [responseObject isKindOfClass:[NSDictionary class]]) { if (self.outputClass) { responseObject = [AWSMTLJSONAdapter modelOfClass:self.outputClass fromJSONDictionary:responseObject error:error]; } } return responseObject; } @end @interface AWSSageMakerRuntimeRequestRetryHandler : AWSURLRequestRetryHandler @end @implementation AWSSageMakerRuntimeRequestRetryHandler @end @interface AWSRequest() @property (nonatomic, strong) AWSNetworkingRequest *internalRequest; @end @interface AWSSageMakerRuntime() @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 AWSSageMakerRuntime + (void)initialize { [super initialize]; if (![AWSiOSSDKVersion isEqualToString:AWSSageMakerRuntimeSDKVersion]) { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"AWSCore and AWSSageMakerRuntime versions need to match. Check your SDK installation. AWSCore: %@ AWSSageMakerRuntime: %@", AWSiOSSDKVersion, AWSSageMakerRuntimeSDKVersion] userInfo:nil]; } } #pragma mark - Setup static AWSSynchronizedMutableDictionary *_serviceClients = nil; + (instancetype)defaultSageMakerRuntime { static AWSSageMakerRuntime *_defaultSageMakerRuntime = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ AWSServiceConfiguration *serviceConfiguration = nil; AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoSageMakerRuntime]; 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]; } _defaultSageMakerRuntime = [[AWSSageMakerRuntime alloc] initWithConfiguration:serviceConfiguration]; }); return _defaultSageMakerRuntime; } + (void)registerSageMakerRuntimeWithConfiguration:(AWSServiceConfiguration *)configuration forKey:(NSString *)key { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _serviceClients = [AWSSynchronizedMutableDictionary new]; }); [_serviceClients setObject:[[AWSSageMakerRuntime alloc] initWithConfiguration:configuration] forKey:key]; } + (instancetype)SageMakerRuntimeForKey:(NSString *)key { @synchronized(self) { AWSSageMakerRuntime *serviceClient = [_serviceClients objectForKey:key]; if (serviceClient) { return serviceClient; } AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] serviceInfo:AWSInfoSageMakerRuntime forKey:key]; if (serviceInfo) { AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region credentialsProvider:serviceInfo.cognitoCredentialsProvider]; [AWSSageMakerRuntime registerSageMakerRuntimeWithConfiguration:serviceConfiguration forKey:key]; } return [_serviceClients objectForKey:key]; } } + (void)removeSageMakerRuntimeForKey:(NSString *)key { [_serviceClients removeObjectForKey:key]; } - (instancetype)init { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"`- init` is not a valid initializer. Use `+ defaultSageMakerRuntime` or `+ SageMakerRuntimeForKey:` 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:AWSServiceSageMakerRuntime useUnsafeURL:NO]; }else{ [_configuration.endpoint setRegion:_configuration.regionType service:AWSServiceSageMakerRuntime]; } AWSSignatureV4Signer *signer = [[AWSSignatureV4Signer 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 = [[AWSSageMakerRuntimeRequestRetryHandler 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:[[AWSSageMakerRuntimeResources sharedInstance] JSONObject] actionName:operationName]; networkingRequest.responseSerializer = [[AWSSageMakerRuntimeResponseSerializer alloc] initWithJSONDefinition:[[AWSSageMakerRuntimeResources sharedInstance] JSONObject] actionName:operationName outputClass:outputClass]; return [self.networking sendRequest:networkingRequest]; } } #pragma mark - Service method - (AWSTask *)invokeEndpoint:(AWSSageMakerRuntimeInvokeEndpointInput *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/endpoints/{EndpointName}/invocations" targetPrefix:@"" operationName:@"InvokeEndpoint" outputClass:[AWSSageMakerRuntimeInvokeEndpointOutput class]]; } - (void)invokeEndpoint:(AWSSageMakerRuntimeInvokeEndpointInput *)request completionHandler:(void (^)(AWSSageMakerRuntimeInvokeEndpointOutput *response, NSError *error))completionHandler { [[self invokeEndpoint:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSSageMakerRuntimeInvokeEndpointOutput *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)invokeEndpointAsync:(AWSSageMakerRuntimeInvokeEndpointAsyncInput *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/endpoints/{EndpointName}/async-invocations" targetPrefix:@"" operationName:@"InvokeEndpointAsync" outputClass:[AWSSageMakerRuntimeInvokeEndpointAsyncOutput class]]; } - (void)invokeEndpointAsync:(AWSSageMakerRuntimeInvokeEndpointAsyncInput *)request completionHandler:(void (^)(AWSSageMakerRuntimeInvokeEndpointAsyncOutput *response, NSError *error))completionHandler { [[self invokeEndpointAsync:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSSageMakerRuntimeInvokeEndpointAsyncOutput *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } #pragma mark - @end