No Description

AWSNetworking.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License").
  5. // You may not use this file except in compliance with the License.
  6. // A copy of the License is located at
  7. //
  8. // http://aws.amazon.com/apache2.0
  9. //
  10. // or in the "license" file accompanying this file. This file is distributed
  11. // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. // express or implied. See the License for the specific language governing
  13. // permissions and limitations under the License.
  14. //
  15. #import <Foundation/Foundation.h>
  16. #import "AWSModel.h"
  17. FOUNDATION_EXPORT NSString *const AWSNetworkingErrorDomain;
  18. typedef NS_ENUM(NSInteger, AWSNetworkingErrorType) {
  19. AWSNetworkingErrorUnknown,
  20. AWSNetworkingErrorCancelled
  21. };
  22. typedef NS_ENUM(NSInteger, AWSNetworkingRetryType) {
  23. AWSNetworkingRetryTypeUnknown,
  24. AWSNetworkingRetryTypeShouldNotRetry,
  25. AWSNetworkingRetryTypeShouldRetry,
  26. AWSNetworkingRetryTypeShouldRefreshCredentialsAndRetry,
  27. AWSNetworkingRetryTypeShouldCorrectClockSkewAndRetry,
  28. AWSNetworkingRetryTypeResetStreamAndRetry
  29. };
  30. @class AWSNetworkingConfiguration;
  31. @class AWSNetworkingRequest;
  32. @class AWSTask<__covariant ResultType>;
  33. typedef void (^AWSNetworkingUploadProgressBlock) (int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend);
  34. typedef void (^AWSNetworkingDownloadProgressBlock) (int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
  35. #pragma mark - AWSHTTPMethod
  36. typedef NS_ENUM(NSInteger, AWSHTTPMethod) {
  37. AWSHTTPMethodUnknown,
  38. AWSHTTPMethodGET,
  39. AWSHTTPMethodHEAD,
  40. AWSHTTPMethodPOST,
  41. AWSHTTPMethodPUT,
  42. AWSHTTPMethodPATCH,
  43. AWSHTTPMethodDELETE
  44. };
  45. @interface NSString (AWSHTTPMethod)
  46. + (instancetype)aws_stringWithHTTPMethod:(AWSHTTPMethod)HTTPMethod;
  47. @end
  48. #pragma mark - AWSNetworking
  49. @interface AWSNetworking : NSObject
  50. - (instancetype)initWithConfiguration:(AWSNetworkingConfiguration *)configuration;
  51. - (AWSTask *)sendRequest:(AWSNetworkingRequest *)request;
  52. @end
  53. #pragma mark - Protocols
  54. @protocol AWSURLRequestSerializer <NSObject>
  55. @required
  56. - (AWSTask *)validateRequest:(NSURLRequest *)request;
  57. - (AWSTask *)serializeRequest:(NSMutableURLRequest *)request
  58. headers:(NSDictionary *)headers
  59. parameters:(NSDictionary *)parameters;
  60. @end
  61. @protocol AWSNetworkingRequestInterceptor <NSObject>
  62. @required
  63. - (AWSTask *)interceptRequest:(NSMutableURLRequest *)request;
  64. @end
  65. @protocol AWSNetworkingHTTPResponseInterceptor <NSObject>
  66. @required
  67. - (AWSTask *)interceptResponse:(NSHTTPURLResponse *)response
  68. data:(id)data
  69. originalRequest:(NSURLRequest *)originalRequest
  70. currentRequest:(NSURLRequest *)currentRequest;
  71. @end
  72. @protocol AWSHTTPURLResponseSerializer <NSObject>
  73. @required
  74. - (BOOL)validateResponse:(NSHTTPURLResponse *)response
  75. fromRequest:(NSURLRequest *)request
  76. data:(id)data
  77. error:(NSError *__autoreleasing *)error;
  78. - (id)responseObjectForResponse:(NSHTTPURLResponse *)response
  79. originalRequest:(NSURLRequest *)originalRequest
  80. currentRequest:(NSURLRequest *)currentRequest
  81. data:(id)data
  82. error:(NSError *__autoreleasing *)error;
  83. @end
  84. @protocol AWSURLRequestRetryHandler <NSObject>
  85. @required
  86. @property (nonatomic, assign) uint32_t maxRetryCount;
  87. - (AWSNetworkingRetryType)shouldRetry:(uint32_t)currentRetryCount
  88. originalRequest:(AWSNetworkingRequest *)originalRequest
  89. response:(NSHTTPURLResponse *)response
  90. data:(NSData *)data
  91. error:(NSError *)error;
  92. - (NSTimeInterval)timeIntervalForRetry:(uint32_t)currentRetryCount
  93. response:(NSHTTPURLResponse *)response
  94. data:(NSData *)data
  95. error:(NSError *)error;
  96. @optional
  97. - (NSDictionary *)resetParameters:(NSDictionary *)parameters;
  98. @end
  99. #pragma mark - AWSNetworkingConfiguration
  100. @interface AWSNetworkingConfiguration : NSObject <NSCopying>
  101. @property (nonatomic, readonly) NSURL *URL;
  102. @property (nonatomic, strong) NSURL *baseURL;
  103. @property (nonatomic, strong) NSString *URLString;
  104. @property (nonatomic, assign) AWSHTTPMethod HTTPMethod;
  105. @property (nonatomic, strong) NSDictionary *headers;
  106. @property (nonatomic, assign) BOOL allowsCellularAccess;
  107. @property (nonatomic, strong) NSString *sharedContainerIdentifier;
  108. @property (nonatomic, strong) id<AWSURLRequestSerializer> requestSerializer;
  109. @property (nonatomic, strong) NSArray<id<AWSNetworkingRequestInterceptor>> *requestInterceptors;
  110. @property (nonatomic, strong) id<AWSHTTPURLResponseSerializer> responseSerializer;
  111. @property (nonatomic, strong) NSArray<id<AWSNetworkingHTTPResponseInterceptor>> *responseInterceptors;
  112. @property (nonatomic, strong) id<AWSURLRequestRetryHandler> retryHandler;
  113. /**
  114. The maximum number of retries for failed requests. The value needs to be between 0 and 10 inclusive. If set to higher than 10, it becomes 10.
  115. */
  116. @property (nonatomic, assign) uint32_t maxRetryCount;
  117. /**
  118. The timeout interval to use when waiting for additional data.
  119. */
  120. @property (nonatomic, assign) NSTimeInterval timeoutIntervalForRequest;
  121. /**
  122. The maximum amount of time that a resource request should be allowed to take.
  123. */
  124. @property (nonatomic, assign) NSTimeInterval timeoutIntervalForResource;
  125. @end
  126. #pragma mark - AWSNetworkingRequest
  127. @interface AWSNetworkingRequest : AWSNetworkingConfiguration
  128. @property (nonatomic, strong) NSDictionary *parameters;
  129. @property (nonatomic, strong) NSURL *uploadingFileURL;
  130. @property (nonatomic, strong) NSURL *downloadingFileURL;
  131. @property (nonatomic, assign) BOOL shouldWriteDirectly;
  132. @property (nonatomic, copy) AWSNetworkingUploadProgressBlock uploadProgress;
  133. @property (nonatomic, copy) AWSNetworkingDownloadProgressBlock downloadProgress;
  134. @property (readonly, nonatomic, strong) NSURLSessionTask *task;
  135. @property (readonly, nonatomic, assign, getter = isCancelled) BOOL cancelled;
  136. - (void)assignProperties:(AWSNetworkingConfiguration *)configuration;
  137. - (void)cancel;
  138. - (void)pause;
  139. @end
  140. @interface AWSRequest : AWSModel
  141. @property (nonatomic, copy) AWSNetworkingUploadProgressBlock uploadProgress;
  142. @property (nonatomic, copy) AWSNetworkingDownloadProgressBlock downloadProgress;
  143. @property (nonatomic, assign, readonly, getter = isCancelled) BOOL cancelled;
  144. @property (nonatomic, strong) NSURL *downloadingFileURL;
  145. - (AWSTask *)cancel;
  146. - (AWSTask *)pause;
  147. @end
  148. @interface AWSNetworkingRequestInterceptor : NSObject <AWSNetworkingRequestInterceptor>
  149. @property (nonatomic, readonly) NSString *userAgent;
  150. - (instancetype)initWithUserAgent:(NSString *)userAgent;
  151. @end