AFURLResponseSerialization.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // AFURLResponseSerialization.h
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import <Foundation/Foundation.h>
  22. #import <CoreGraphics/CoreGraphics.h>
  23. NS_ASSUME_NONNULL_BEGIN
  24. /**
  25. Recursively removes `NSNull` values from a JSON object.
  26. */
  27. FOUNDATION_EXPORT id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions);
  28. /**
  29. The `AFURLResponseSerialization` protocol is adopted by an object that decodes data into a more useful object representation, according to details in the server response. Response serializers may additionally perform validation on the incoming response and data.
  30. For example, a JSON response serializer may check for an acceptable status code (`2XX` range) and content type (`application/json`), decoding a valid JSON response into an object.
  31. */
  32. @protocol AFURLResponseSerialization <NSObject, NSSecureCoding, NSCopying>
  33. /**
  34. The response object decoded from the data associated with a specified response.
  35. @param response The response to be processed.
  36. @param data The response data to be decoded.
  37. @param error The error that occurred while attempting to decode the response data.
  38. @return The object decoded from the specified response data.
  39. */
  40. - (nullable id)responseObjectForResponse:(nullable NSURLResponse *)response
  41. data:(nullable NSData *)data
  42. error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW;
  43. @end
  44. #pragma mark -
  45. /**
  46. `AFHTTPResponseSerializer` conforms to the `AFURLRequestSerialization` & `AFURLResponseSerialization` protocols, offering a concrete base implementation of query string / URL form-encoded parameter serialization and default request headers, as well as response status code and content type validation.
  47. Any request or response serializer dealing with HTTP is encouraged to subclass `AFHTTPResponseSerializer` in order to ensure consistent default behavior.
  48. */
  49. @interface AFHTTPResponseSerializer : NSObject <AFURLResponseSerialization>
  50. - (instancetype)init;
  51. /**
  52. Creates and returns a serializer with default configuration.
  53. */
  54. + (instancetype)serializer;
  55. ///-----------------------------------------
  56. /// @name Configuring Response Serialization
  57. ///-----------------------------------------
  58. /**
  59. The acceptable HTTP status codes for responses. When non-`nil`, responses with status codes not contained by the set will result in an error during validation.
  60. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  61. */
  62. @property (nonatomic, copy, nullable) NSIndexSet *acceptableStatusCodes;
  63. /**
  64. The acceptable MIME types for responses. When non-`nil`, responses with a `Content-Type` with MIME types that do not intersect with the set will result in an error during validation.
  65. */
  66. @property (nonatomic, copy, nullable) NSSet <NSString *> *acceptableContentTypes;
  67. /**
  68. Validates the specified response and data.
  69. In its base implementation, this method checks for an acceptable status code and content type. Subclasses may wish to add other domain-specific checks.
  70. @param response The response to be validated.
  71. @param data The data associated with the response.
  72. @param error The error that occurred while attempting to validate the response.
  73. @return `YES` if the response is valid, otherwise `NO`.
  74. */
  75. - (BOOL)validateResponse:(nullable NSHTTPURLResponse *)response
  76. data:(nullable NSData *)data
  77. error:(NSError * _Nullable __autoreleasing *)error;
  78. @end
  79. #pragma mark -
  80. /**
  81. `AFJSONResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes JSON responses.
  82. By default, `AFJSONResponseSerializer` accepts the following MIME types, which includes the official standard, `application/json`, as well as other commonly-used types:
  83. - `application/json`
  84. - `text/json`
  85. - `text/javascript`
  86. In RFC 7159 - Section 8.1, it states that JSON text is required to be encoded in UTF-8, UTF-16, or UTF-32, and the default encoding is UTF-8. NSJSONSerialization provides support for all the encodings listed in the specification, and recommends UTF-8 for efficiency. Using an unsupported encoding will result in serialization error. See the `NSJSONSerialization` documentation for more details.
  87. */
  88. @interface AFJSONResponseSerializer : AFHTTPResponseSerializer
  89. - (instancetype)init;
  90. /**
  91. Options for reading the response JSON data and creating the Foundation objects. For possible values, see the `NSJSONSerialization` documentation section "NSJSONReadingOptions". `0` by default.
  92. */
  93. @property (nonatomic, assign) NSJSONReadingOptions readingOptions;
  94. /**
  95. Whether to remove keys with `NSNull` values from response JSON. Defaults to `NO`.
  96. */
  97. @property (nonatomic, assign) BOOL removesKeysWithNullValues;
  98. /**
  99. Creates and returns a JSON serializer with specified reading and writing options.
  100. @param readingOptions The specified JSON reading options.
  101. */
  102. + (instancetype)serializerWithReadingOptions:(NSJSONReadingOptions)readingOptions;
  103. @end
  104. #pragma mark -
  105. /**
  106. `AFXMLParserResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLParser` objects.
  107. By default, `AFXMLParserResponseSerializer` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types:
  108. - `application/xml`
  109. - `text/xml`
  110. */
  111. @interface AFXMLParserResponseSerializer : AFHTTPResponseSerializer
  112. @end
  113. #pragma mark -
  114. #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  115. /**
  116. `AFXMLDocumentResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLDocument` objects.
  117. By default, `AFXMLDocumentResponseSerializer` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types:
  118. - `application/xml`
  119. - `text/xml`
  120. */
  121. @interface AFXMLDocumentResponseSerializer : AFHTTPResponseSerializer
  122. - (instancetype)init;
  123. /**
  124. Input and output options specifically intended for `NSXMLDocument` objects. For possible values, see the `NSXMLDocument` documentation section "Input and Output Options". `0` by default.
  125. */
  126. @property (nonatomic, assign) NSUInteger options;
  127. /**
  128. Creates and returns an XML document serializer with the specified options.
  129. @param mask The XML document options.
  130. */
  131. + (instancetype)serializerWithXMLDocumentOptions:(NSUInteger)mask;
  132. @end
  133. #endif
  134. #pragma mark -
  135. /**
  136. `AFPropertyListResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLDocument` objects.
  137. By default, `AFPropertyListResponseSerializer` accepts the following MIME types:
  138. - `application/x-plist`
  139. */
  140. @interface AFPropertyListResponseSerializer : AFHTTPResponseSerializer
  141. - (instancetype)init;
  142. /**
  143. The property list format. Possible values are described in "NSPropertyListFormat".
  144. */
  145. @property (nonatomic, assign) NSPropertyListFormat format;
  146. /**
  147. The property list reading options. Possible values are described in "NSPropertyListMutabilityOptions."
  148. */
  149. @property (nonatomic, assign) NSPropertyListReadOptions readOptions;
  150. /**
  151. Creates and returns a property list serializer with a specified format, read options, and write options.
  152. @param format The property list format.
  153. @param readOptions The property list reading options.
  154. */
  155. + (instancetype)serializerWithFormat:(NSPropertyListFormat)format
  156. readOptions:(NSPropertyListReadOptions)readOptions;
  157. @end
  158. #pragma mark -
  159. /**
  160. `AFImageResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes image responses.
  161. By default, `AFImageResponseSerializer` accepts the following MIME types, which correspond to the image formats supported by UIImage or NSImage:
  162. - `image/tiff`
  163. - `image/jpeg`
  164. - `image/gif`
  165. - `image/png`
  166. - `image/ico`
  167. - `image/x-icon`
  168. - `image/bmp`
  169. - `image/x-bmp`
  170. - `image/x-xbitmap`
  171. - `image/x-win-bitmap`
  172. */
  173. @interface AFImageResponseSerializer : AFHTTPResponseSerializer
  174. #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH
  175. /**
  176. The scale factor used when interpreting the image data to construct `responseImage`. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property. This is set to the value of scale of the main screen by default, which automatically scales images for retina displays, for instance.
  177. */
  178. @property (nonatomic, assign) CGFloat imageScale;
  179. /**
  180. Whether to automatically inflate response image data for compressed formats (such as PNG or JPEG). Enabling this can significantly improve drawing performance on iOS when used with `setCompletionBlockWithSuccess:failure:`, as it allows a bitmap representation to be constructed in the background rather than on the main thread. `YES` by default.
  181. */
  182. @property (nonatomic, assign) BOOL automaticallyInflatesResponseImage;
  183. #endif
  184. @end
  185. #pragma mark -
  186. /**
  187. `AFCompoundSerializer` is a subclass of `AFHTTPResponseSerializer` that delegates the response serialization to the first `AFHTTPResponseSerializer` object that returns an object for `responseObjectForResponse:data:error:`, falling back on the default behavior of `AFHTTPResponseSerializer`. This is useful for supporting multiple potential types and structures of server responses with a single serializer.
  188. */
  189. @interface AFCompoundResponseSerializer : AFHTTPResponseSerializer
  190. /**
  191. The component response serializers.
  192. */
  193. @property (readonly, nonatomic, copy) NSArray <id<AFURLResponseSerialization>> *responseSerializers;
  194. /**
  195. Creates and returns a compound serializer comprised of the specified response serializers.
  196. @warning Each response serializer specified must be a subclass of `AFHTTPResponseSerializer`, and response to `-validateResponse:data:error:`.
  197. */
  198. + (instancetype)compoundSerializerWithResponseSerializers:(NSArray <id<AFURLResponseSerialization>> *)responseSerializers;
  199. @end
  200. ///----------------
  201. /// @name Constants
  202. ///----------------
  203. /**
  204. ## Error Domains
  205. The following error domain is predefined.
  206. - `NSString * const AFURLResponseSerializationErrorDomain`
  207. ### Constants
  208. `AFURLResponseSerializationErrorDomain`
  209. AFURLResponseSerializer errors. Error codes for `AFURLResponseSerializationErrorDomain` correspond to codes in `NSURLErrorDomain`.
  210. */
  211. FOUNDATION_EXPORT NSString * const AFURLResponseSerializationErrorDomain;
  212. /**
  213. ## User info dictionary keys
  214. These keys may exist in the user info dictionary, in addition to those defined for NSError.
  215. - `NSString * const AFNetworkingOperationFailingURLResponseErrorKey`
  216. - `NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey`
  217. ### Constants
  218. `AFNetworkingOperationFailingURLResponseErrorKey`
  219. The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`.
  220. `AFNetworkingOperationFailingURLResponseDataErrorKey`
  221. The corresponding value is an `NSData` containing the original data of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`.
  222. */
  223. FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseErrorKey;
  224. FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey;
  225. NS_ASSUME_NONNULL_END