AFURLResponseSerialization.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // AFURLResponseSerialization.h
  2. // Copyright (c) 2011–2015 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. /**
  24. 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.
  25. 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.
  26. */
  27. @protocol AFURLResponseSerialization <NSObject, NSSecureCoding, NSCopying>
  28. /**
  29. The response object decoded from the data associated with a specified response.
  30. @param response The response to be processed.
  31. @param data The response data to be decoded.
  32. @param error The error that occurred while attempting to decode the response data.
  33. @return The object decoded from the specified response data.
  34. */
  35. - (id)responseObjectForResponse:(NSURLResponse *)response
  36. data:(NSData *)data
  37. error:(NSError *__autoreleasing *)error;
  38. @end
  39. #pragma mark -
  40. /**
  41. `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.
  42. Any request or response serializer dealing with HTTP is encouraged to subclass `AFHTTPResponseSerializer` in order to ensure consistent default behavior.
  43. */
  44. @interface AFHTTPResponseSerializer : NSObject <AFURLResponseSerialization>
  45. - (instancetype) init;
  46. /**
  47. The string encoding used to serialize data received from the server, when no string encoding is specified by the response. `NSUTF8StringEncoding` by default.
  48. */
  49. @property (nonatomic, assign) NSStringEncoding stringEncoding;
  50. /**
  51. Creates and returns a serializer with default configuration.
  52. */
  53. + (instancetype)serializer;
  54. ///-----------------------------------------
  55. /// @name Configuring Response Serialization
  56. ///-----------------------------------------
  57. /**
  58. 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.
  59. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  60. */
  61. @property (nonatomic, copy) NSIndexSet *acceptableStatusCodes;
  62. /**
  63. 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.
  64. */
  65. @property (nonatomic, copy) NSSet *acceptableContentTypes;
  66. /**
  67. Validates the specified response and data.
  68. In its base implementation, this method checks for an acceptable status code and content type. Subclasses may wish to add other domain-specific checks.
  69. @param response The response to be validated.
  70. @param data The data associated with the response.
  71. @param error The error that occurred while attempting to validate the response.
  72. @return `YES` if the response is valid, otherwise `NO`.
  73. */
  74. - (BOOL)validateResponse:(NSHTTPURLResponse *)response
  75. data:(NSData *)data
  76. error:(NSError *__autoreleasing *)error;
  77. @end
  78. #pragma mark -
  79. /**
  80. `AFJSONResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes JSON responses.
  81. By default, `AFJSONResponseSerializer` accepts the following MIME types, which includes the official standard, `application/json`, as well as other commonly-used types:
  82. - `application/json`
  83. - `text/json`
  84. - `text/javascript`
  85. */
  86. @interface AFJSONResponseSerializer : AFHTTPResponseSerializer
  87. - (instancetype) init;
  88. /**
  89. Options for reading the response JSON data and creating the Foundation objects. For possible values, see the `NSJSONSerialization` documentation section "NSJSONReadingOptions". `0` by default.
  90. */
  91. @property (nonatomic, assign) NSJSONReadingOptions readingOptions;
  92. /**
  93. Whether to remove keys with `NSNull` values from response JSON. Defaults to `NO`.
  94. */
  95. @property (nonatomic, assign) BOOL removesKeysWithNullValues;
  96. /**
  97. Creates and returns a JSON serializer with specified reading and writing options.
  98. @param readingOptions The specified JSON reading options.
  99. */
  100. + (instancetype)serializerWithReadingOptions:(NSJSONReadingOptions)readingOptions;
  101. @end
  102. #pragma mark -
  103. /**
  104. `AFXMLParserResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLParser` objects.
  105. By default, `AFXMLParserResponseSerializer` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types:
  106. - `application/xml`
  107. - `text/xml`
  108. */
  109. @interface AFXMLParserResponseSerializer : AFHTTPResponseSerializer
  110. @end
  111. #pragma mark -
  112. #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  113. /**
  114. `AFXMLDocumentResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLDocument` objects.
  115. By default, `AFXMLDocumentResponseSerializer` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types:
  116. - `application/xml`
  117. - `text/xml`
  118. */
  119. @interface AFXMLDocumentResponseSerializer : AFHTTPResponseSerializer
  120. - (instancetype) init;
  121. /**
  122. Input and output options specifically intended for `NSXMLDocument` objects. For possible values, see the `NSJSONSerialization` documentation section "NSJSONReadingOptions". `0` by default.
  123. */
  124. @property (nonatomic, assign) NSUInteger options;
  125. /**
  126. Creates and returns an XML document serializer with the specified options.
  127. @param mask The XML document options.
  128. */
  129. + (instancetype)serializerWithXMLDocumentOptions:(NSUInteger)mask;
  130. @end
  131. #endif
  132. #pragma mark -
  133. /**
  134. `AFPropertyListResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLDocument` objects.
  135. By default, `AFPropertyListResponseSerializer` accepts the following MIME types:
  136. - `application/x-plist`
  137. */
  138. @interface AFPropertyListResponseSerializer : AFHTTPResponseSerializer
  139. - (instancetype) init;
  140. /**
  141. The property list format. Possible values are described in "NSPropertyListFormat".
  142. */
  143. @property (nonatomic, assign) NSPropertyListFormat format;
  144. /**
  145. The property list reading options. Possible values are described in "NSPropertyListMutabilityOptions."
  146. */
  147. @property (nonatomic, assign) NSPropertyListReadOptions readOptions;
  148. /**
  149. Creates and returns a property list serializer with a specified format, read options, and write options.
  150. @param format The property list format.
  151. @param readOptions The property list reading options.
  152. */
  153. + (instancetype)serializerWithFormat:(NSPropertyListFormat)format
  154. readOptions:(NSPropertyListReadOptions)readOptions;
  155. @end
  156. #pragma mark -
  157. /**
  158. `AFImageResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes image responses.
  159. By default, `AFImageResponseSerializer` accepts the following MIME types, which correspond to the image formats supported by UIImage or NSImage:
  160. - `image/tiff`
  161. - `image/jpeg`
  162. - `image/gif`
  163. - `image/png`
  164. - `image/ico`
  165. - `image/x-icon`
  166. - `image/bmp`
  167. - `image/x-bmp`
  168. - `image/x-xbitmap`
  169. - `image/x-win-bitmap`
  170. */
  171. @interface AFImageResponseSerializer : AFHTTPResponseSerializer
  172. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  173. /**
  174. 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.
  175. */
  176. @property (nonatomic, assign) CGFloat imageScale;
  177. /**
  178. 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.
  179. */
  180. @property (nonatomic, assign) BOOL automaticallyInflatesResponseImage;
  181. #endif
  182. @end
  183. #pragma mark -
  184. /**
  185. `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.
  186. */
  187. @interface AFCompoundResponseSerializer : AFHTTPResponseSerializer
  188. /**
  189. The component response serializers.
  190. */
  191. @property (readonly, nonatomic, copy) NSArray *responseSerializers;
  192. /**
  193. Creates and returns a compound serializer comprised of the specified response serializers.
  194. @warning Each response serializer specified must be a subclass of `AFHTTPResponseSerializer`, and response to `-validateResponse:data:error:`.
  195. */
  196. + (instancetype)compoundSerializerWithResponseSerializers:(NSArray *)responseSerializers;
  197. @end
  198. ///----------------
  199. /// @name Constants
  200. ///----------------
  201. /**
  202. ## Error Domains
  203. The following error domain is predefined.
  204. - `NSString * const AFURLResponseSerializationErrorDomain`
  205. ### Constants
  206. `AFURLResponseSerializationErrorDomain`
  207. AFURLResponseSerializer errors. Error codes for `AFURLResponseSerializationErrorDomain` correspond to codes in `NSURLErrorDomain`.
  208. */
  209. extern NSString * const AFURLResponseSerializationErrorDomain;
  210. /**
  211. ## User info dictionary keys
  212. These keys may exist in the user info dictionary, in addition to those defined for NSError.
  213. - `NSString * const AFNetworkingOperationFailingURLResponseErrorKey`
  214. - `NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey`
  215. ### Constants
  216. `AFNetworkingOperationFailingURLResponseErrorKey`
  217. The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`.
  218. `AFNetworkingOperationFailingURLResponseDataErrorKey`
  219. 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`.
  220. */
  221. extern NSString * const AFNetworkingOperationFailingURLResponseErrorKey;
  222. extern NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey;