// // 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 "AWSIoTDataService.h" #import #import #import #import #import #import #import #import #import "AWSIoTDataResources.h" static NSString *const AWSInfoIoTData = @"IoTData"; NSString *const AWSIoTDataSDKVersion = @"2.33.3"; @interface AWSIoTDataResponseSerializer : AWSJSONResponseSerializer @end @implementation AWSIoTDataResponseSerializer #pragma mark - Service errors static NSDictionary *errorCodeDictionary = nil; + (void)initialize { errorCodeDictionary = @{ @"ConflictException" : @(AWSIoTDataErrorConflict), @"InternalFailureException" : @(AWSIoTDataErrorInternalFailure), @"InvalidRequestException" : @(AWSIoTDataErrorInvalidRequest), @"MethodNotAllowedException" : @(AWSIoTDataErrorMethodNotAllowed), @"RequestEntityTooLargeException" : @(AWSIoTDataErrorRequestEntityTooLarge), @"ResourceNotFoundException" : @(AWSIoTDataErrorResourceNotFound), @"ServiceUnavailableException" : @(AWSIoTDataErrorServiceUnavailable), @"ThrottlingException" : @(AWSIoTDataErrorThrottling), @"UnauthorizedException" : @(AWSIoTDataErrorUnauthorized), @"UnsupportedDocumentEncodingException" : @(AWSIoTDataErrorUnsupportedDocumentEncoding), }; } #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:AWSIoTDataErrorDomain 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:AWSIoTDataErrorDomain code:AWSIoTDataErrorUnknown userInfo:userInfo]; } return responseObject; } } } if (!*error && response.statusCode/100 != 2) { *error = [NSError errorWithDomain:AWSIoTDataErrorDomain code:AWSIoTDataErrorUnknown userInfo:nil]; } if (!*error && [responseObject isKindOfClass:[NSDictionary class]]) { if (self.outputClass) { responseObject = [AWSMTLJSONAdapter modelOfClass:self.outputClass fromJSONDictionary:responseObject error:error]; } } return responseObject; } @end @interface AWSIoTDataRequestRetryHandler : AWSURLRequestRetryHandler @end @implementation AWSIoTDataRequestRetryHandler @end @interface AWSRequest() @property (nonatomic, strong) AWSNetworkingRequest *internalRequest; @end @interface AWSIoTData() @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 AWSIoTData + (void)initialize { [super initialize]; if (![AWSiOSSDKVersion isEqualToString:AWSIoTDataSDKVersion]) { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"AWSCore and AWSIoTData versions need to match. Check your SDK installation. AWSCore: %@ AWSIoTData: %@", AWSiOSSDKVersion, AWSIoTDataSDKVersion] userInfo:nil]; } } #pragma mark - Setup static AWSSynchronizedMutableDictionary *_serviceClients = nil; + (instancetype)defaultIoTData { static AWSIoTData *_defaultIoTData = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ AWSServiceConfiguration *serviceConfiguration = nil; AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoIoTData]; 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]; } _defaultIoTData = [[AWSIoTData alloc] initWithConfiguration:serviceConfiguration]; }); return _defaultIoTData; } + (void)registerIoTDataWithConfiguration:(AWSServiceConfiguration *)configuration forKey:(NSString *)key { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _serviceClients = [AWSSynchronizedMutableDictionary new]; }); [_serviceClients setObject:[[AWSIoTData alloc] initWithConfiguration:configuration] forKey:key]; } + (instancetype)IoTDataForKey:(NSString *)key { @synchronized(self) { AWSIoTData *serviceClient = [_serviceClients objectForKey:key]; if (serviceClient) { return serviceClient; } AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] serviceInfo:AWSInfoIoTData forKey:key]; if (serviceInfo) { AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region credentialsProvider:serviceInfo.cognitoCredentialsProvider]; [AWSIoTData registerIoTDataWithConfiguration:serviceConfiguration forKey:key]; } return [_serviceClients objectForKey:key]; } } + (void)removeIoTDataForKey:(NSString *)key { [_serviceClients removeObjectForKey:key]; } - (instancetype)init { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"`- init` is not a valid initializer. Use `+ defaultIoTData` or `+ IoTDataForKey:` 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:AWSServiceIoTData useUnsafeURL:NO]; }else{ [_configuration.endpoint setRegion:_configuration.regionType service:AWSServiceIoTData]; } 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 = [[AWSIoTDataRequestRetryHandler alloc] initWithMaximumRetryCount:_configuration.maxRetryCount]; _configuration.headers = @{@"Content-Type" : @"application/x-amz-json-1.0"}; _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:[[AWSIoTDataResources sharedInstance] JSONObject] actionName:operationName]; networkingRequest.responseSerializer = [[AWSIoTDataResponseSerializer alloc] initWithJSONDefinition:[[AWSIoTDataResources sharedInstance] JSONObject] actionName:operationName outputClass:outputClass]; return [self.networking sendRequest:networkingRequest]; } } #pragma mark - Service method - (AWSTask *)deleteThingShadow:(AWSIoTDataDeleteThingShadowRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodDELETE URLString:@"/things/{thingName}/shadow" targetPrefix:@"" operationName:@"DeleteThingShadow" outputClass:[AWSIoTDataDeleteThingShadowResponse class]]; } - (void)deleteThingShadow:(AWSIoTDataDeleteThingShadowRequest *)request completionHandler:(void (^)(AWSIoTDataDeleteThingShadowResponse *response, NSError *error))completionHandler { [[self deleteThingShadow:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSIoTDataDeleteThingShadowResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)getRetainedMessage:(AWSIoTDataGetRetainedMessageRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodGET URLString:@"/retainedMessage/{topic}" targetPrefix:@"" operationName:@"GetRetainedMessage" outputClass:[AWSIoTDataGetRetainedMessageResponse class]]; } - (void)getRetainedMessage:(AWSIoTDataGetRetainedMessageRequest *)request completionHandler:(void (^)(AWSIoTDataGetRetainedMessageResponse *response, NSError *error))completionHandler { [[self getRetainedMessage:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSIoTDataGetRetainedMessageResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)getThingShadow:(AWSIoTDataGetThingShadowRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodGET URLString:@"/things/{thingName}/shadow" targetPrefix:@"" operationName:@"GetThingShadow" outputClass:[AWSIoTDataGetThingShadowResponse class]]; } - (void)getThingShadow:(AWSIoTDataGetThingShadowRequest *)request completionHandler:(void (^)(AWSIoTDataGetThingShadowResponse *response, NSError *error))completionHandler { [[self getThingShadow:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSIoTDataGetThingShadowResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)listNamedShadowsForThing:(AWSIoTDataListNamedShadowsForThingRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodGET URLString:@"/api/things/shadow/ListNamedShadowsForThing/{thingName}" targetPrefix:@"" operationName:@"ListNamedShadowsForThing" outputClass:[AWSIoTDataListNamedShadowsForThingResponse class]]; } - (void)listNamedShadowsForThing:(AWSIoTDataListNamedShadowsForThingRequest *)request completionHandler:(void (^)(AWSIoTDataListNamedShadowsForThingResponse *response, NSError *error))completionHandler { [[self listNamedShadowsForThing:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSIoTDataListNamedShadowsForThingResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)listRetainedMessages:(AWSIoTDataListRetainedMessagesRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodGET URLString:@"/retainedMessage" targetPrefix:@"" operationName:@"ListRetainedMessages" outputClass:[AWSIoTDataListRetainedMessagesResponse class]]; } - (void)listRetainedMessages:(AWSIoTDataListRetainedMessagesRequest *)request completionHandler:(void (^)(AWSIoTDataListRetainedMessagesResponse *response, NSError *error))completionHandler { [[self listRetainedMessages:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSIoTDataListRetainedMessagesResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } - (AWSTask *)publish:(AWSIoTDataPublishRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/topics/{topic}" targetPrefix:@"" operationName:@"Publish" outputClass:nil]; } - (void)publish:(AWSIoTDataPublishRequest *)request completionHandler:(void (^)(NSError *error))completionHandler { [[self publish:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { NSError *error = task.error; if (completionHandler) { completionHandler(error); } return nil; }]; } - (AWSTask *)updateThingShadow:(AWSIoTDataUpdateThingShadowRequest *)request { return [self invokeRequest:request HTTPMethod:AWSHTTPMethodPOST URLString:@"/things/{thingName}/shadow" targetPrefix:@"" operationName:@"UpdateThingShadow" outputClass:[AWSIoTDataUpdateThingShadowResponse class]]; } - (void)updateThingShadow:(AWSIoTDataUpdateThingShadowRequest *)request completionHandler:(void (^)(AWSIoTDataUpdateThingShadowResponse *response, NSError *error))completionHandler { [[self updateThingShadow:request] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) { AWSIoTDataUpdateThingShadowResponse *result = task.result; NSError *error = task.error; if (completionHandler) { completionHandler(result, error); } return nil; }]; } #pragma mark - @end