commit | author | age
|
6e1425
|
1 |
// AFURLResponseSerialization.h |
633752
|
2 |
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) |
6e1425
|
3 |
// |
H |
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 |
|
|
22 |
#import <Foundation/Foundation.h> |
|
23 |
#import <CoreGraphics/CoreGraphics.h> |
|
24 |
|
|
25 |
NS_ASSUME_NONNULL_BEGIN |
|
26 |
|
|
27 |
/** |
633752
|
28 |
Recursively removes `NSNull` values from a JSON object. |
L |
29 |
*/ |
bf3f86
|
30 |
FOUNDATION_EXPORT id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions); |
633752
|
31 |
|
L |
32 |
/** |
6e1425
|
33 |
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. |
H |
34 |
|
|
35 |
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. |
|
36 |
*/ |
|
37 |
@protocol AFURLResponseSerialization <NSObject, NSSecureCoding, NSCopying> |
|
38 |
|
|
39 |
/** |
|
40 |
The response object decoded from the data associated with a specified response. |
|
41 |
|
|
42 |
@param response The response to be processed. |
|
43 |
@param data The response data to be decoded. |
|
44 |
@param error The error that occurred while attempting to decode the response data. |
|
45 |
|
|
46 |
@return The object decoded from the specified response data. |
|
47 |
*/ |
|
48 |
- (nullable id)responseObjectForResponse:(nullable NSURLResponse *)response |
|
49 |
data:(nullable NSData *)data |
633752
|
50 |
error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW; |
6e1425
|
51 |
|
H |
52 |
@end |
|
53 |
|
|
54 |
#pragma mark - |
|
55 |
|
|
56 |
/** |
|
57 |
`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. |
|
58 |
|
|
59 |
Any request or response serializer dealing with HTTP is encouraged to subclass `AFHTTPResponseSerializer` in order to ensure consistent default behavior. |
|
60 |
*/ |
|
61 |
@interface AFHTTPResponseSerializer : NSObject <AFURLResponseSerialization> |
|
62 |
|
|
63 |
- (instancetype)init; |
|
64 |
|
|
65 |
/** |
|
66 |
Creates and returns a serializer with default configuration. |
|
67 |
*/ |
|
68 |
+ (instancetype)serializer; |
|
69 |
|
|
70 |
///----------------------------------------- |
|
71 |
/// @name Configuring Response Serialization |
|
72 |
///----------------------------------------- |
|
73 |
|
|
74 |
/** |
|
75 |
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. |
|
76 |
|
|
77 |
See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
|
78 |
*/ |
|
79 |
@property (nonatomic, copy, nullable) NSIndexSet *acceptableStatusCodes; |
|
80 |
|
|
81 |
/** |
|
82 |
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. |
|
83 |
*/ |
633752
|
84 |
@property (nonatomic, copy, nullable) NSSet <NSString *> *acceptableContentTypes; |
6e1425
|
85 |
|
H |
86 |
/** |
|
87 |
Validates the specified response and data. |
|
88 |
|
|
89 |
In its base implementation, this method checks for an acceptable status code and content type. Subclasses may wish to add other domain-specific checks. |
|
90 |
|
|
91 |
@param response The response to be validated. |
|
92 |
@param data The data associated with the response. |
|
93 |
@param error The error that occurred while attempting to validate the response. |
|
94 |
|
|
95 |
@return `YES` if the response is valid, otherwise `NO`. |
|
96 |
*/ |
|
97 |
- (BOOL)validateResponse:(nullable NSHTTPURLResponse *)response |
|
98 |
data:(nullable NSData *)data |
633752
|
99 |
error:(NSError * _Nullable __autoreleasing *)error; |
6e1425
|
100 |
|
H |
101 |
@end |
|
102 |
|
|
103 |
#pragma mark - |
|
104 |
|
|
105 |
|
|
106 |
/** |
|
107 |
`AFJSONResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes JSON responses. |
|
108 |
|
|
109 |
By default, `AFJSONResponseSerializer` accepts the following MIME types, which includes the official standard, `application/json`, as well as other commonly-used types: |
|
110 |
|
|
111 |
- `application/json` |
|
112 |
- `text/json` |
|
113 |
- `text/javascript` |
633752
|
114 |
|
L |
115 |
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. |
6e1425
|
116 |
*/ |
H |
117 |
@interface AFJSONResponseSerializer : AFHTTPResponseSerializer |
|
118 |
|
|
119 |
- (instancetype)init; |
|
120 |
|
|
121 |
/** |
|
122 |
Options for reading the response JSON data and creating the Foundation objects. For possible values, see the `NSJSONSerialization` documentation section "NSJSONReadingOptions". `0` by default. |
|
123 |
*/ |
|
124 |
@property (nonatomic, assign) NSJSONReadingOptions readingOptions; |
|
125 |
|
|
126 |
/** |
|
127 |
Whether to remove keys with `NSNull` values from response JSON. Defaults to `NO`. |
|
128 |
*/ |
|
129 |
@property (nonatomic, assign) BOOL removesKeysWithNullValues; |
|
130 |
|
|
131 |
/** |
|
132 |
Creates and returns a JSON serializer with specified reading and writing options. |
|
133 |
|
|
134 |
@param readingOptions The specified JSON reading options. |
|
135 |
*/ |
|
136 |
+ (instancetype)serializerWithReadingOptions:(NSJSONReadingOptions)readingOptions; |
|
137 |
|
|
138 |
@end |
|
139 |
|
|
140 |
#pragma mark - |
|
141 |
|
|
142 |
/** |
|
143 |
`AFXMLParserResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLParser` objects. |
|
144 |
|
|
145 |
By default, `AFXMLParserResponseSerializer` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types: |
|
146 |
|
|
147 |
- `application/xml` |
|
148 |
- `text/xml` |
|
149 |
*/ |
|
150 |
@interface AFXMLParserResponseSerializer : AFHTTPResponseSerializer |
|
151 |
|
|
152 |
@end |
|
153 |
|
|
154 |
#pragma mark - |
|
155 |
|
|
156 |
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED |
|
157 |
|
|
158 |
/** |
|
159 |
`AFXMLDocumentResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLDocument` objects. |
|
160 |
|
|
161 |
By default, `AFXMLDocumentResponseSerializer` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types: |
|
162 |
|
|
163 |
- `application/xml` |
|
164 |
- `text/xml` |
|
165 |
*/ |
|
166 |
@interface AFXMLDocumentResponseSerializer : AFHTTPResponseSerializer |
|
167 |
|
|
168 |
- (instancetype)init; |
|
169 |
|
|
170 |
/** |
633752
|
171 |
Input and output options specifically intended for `NSXMLDocument` objects. For possible values, see the `NSXMLDocument` documentation section "Input and Output Options". `0` by default. |
6e1425
|
172 |
*/ |
H |
173 |
@property (nonatomic, assign) NSUInteger options; |
|
174 |
|
|
175 |
/** |
|
176 |
Creates and returns an XML document serializer with the specified options. |
|
177 |
|
|
178 |
@param mask The XML document options. |
|
179 |
*/ |
|
180 |
+ (instancetype)serializerWithXMLDocumentOptions:(NSUInteger)mask; |
|
181 |
|
|
182 |
@end |
|
183 |
|
|
184 |
#endif |
|
185 |
|
|
186 |
#pragma mark - |
|
187 |
|
|
188 |
/** |
|
189 |
`AFPropertyListResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes XML responses as an `NSXMLDocument` objects. |
|
190 |
|
|
191 |
By default, `AFPropertyListResponseSerializer` accepts the following MIME types: |
|
192 |
|
|
193 |
- `application/x-plist` |
|
194 |
*/ |
|
195 |
@interface AFPropertyListResponseSerializer : AFHTTPResponseSerializer |
|
196 |
|
|
197 |
- (instancetype)init; |
|
198 |
|
|
199 |
/** |
|
200 |
The property list format. Possible values are described in "NSPropertyListFormat". |
|
201 |
*/ |
|
202 |
@property (nonatomic, assign) NSPropertyListFormat format; |
|
203 |
|
|
204 |
/** |
|
205 |
The property list reading options. Possible values are described in "NSPropertyListMutabilityOptions." |
|
206 |
*/ |
|
207 |
@property (nonatomic, assign) NSPropertyListReadOptions readOptions; |
|
208 |
|
|
209 |
/** |
|
210 |
Creates and returns a property list serializer with a specified format, read options, and write options. |
|
211 |
|
|
212 |
@param format The property list format. |
|
213 |
@param readOptions The property list reading options. |
|
214 |
*/ |
|
215 |
+ (instancetype)serializerWithFormat:(NSPropertyListFormat)format |
|
216 |
readOptions:(NSPropertyListReadOptions)readOptions; |
|
217 |
|
|
218 |
@end |
|
219 |
|
|
220 |
#pragma mark - |
|
221 |
|
|
222 |
/** |
|
223 |
`AFImageResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes image responses. |
|
224 |
|
|
225 |
By default, `AFImageResponseSerializer` accepts the following MIME types, which correspond to the image formats supported by UIImage or NSImage: |
|
226 |
|
|
227 |
- `image/tiff` |
|
228 |
- `image/jpeg` |
|
229 |
- `image/gif` |
|
230 |
- `image/png` |
|
231 |
- `image/ico` |
|
232 |
- `image/x-icon` |
|
233 |
- `image/bmp` |
|
234 |
- `image/x-bmp` |
|
235 |
- `image/x-xbitmap` |
|
236 |
- `image/x-win-bitmap` |
|
237 |
*/ |
|
238 |
@interface AFImageResponseSerializer : AFHTTPResponseSerializer |
|
239 |
|
633752
|
240 |
#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH |
6e1425
|
241 |
/** |
H |
242 |
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. |
|
243 |
*/ |
|
244 |
@property (nonatomic, assign) CGFloat imageScale; |
|
245 |
|
|
246 |
/** |
|
247 |
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. |
|
248 |
*/ |
|
249 |
@property (nonatomic, assign) BOOL automaticallyInflatesResponseImage; |
|
250 |
#endif |
|
251 |
|
|
252 |
@end |
|
253 |
|
|
254 |
#pragma mark - |
|
255 |
|
|
256 |
/** |
|
257 |
`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. |
|
258 |
*/ |
|
259 |
@interface AFCompoundResponseSerializer : AFHTTPResponseSerializer |
|
260 |
|
|
261 |
/** |
|
262 |
The component response serializers. |
|
263 |
*/ |
633752
|
264 |
@property (readonly, nonatomic, copy) NSArray <id<AFURLResponseSerialization>> *responseSerializers; |
6e1425
|
265 |
|
H |
266 |
/** |
|
267 |
Creates and returns a compound serializer comprised of the specified response serializers. |
|
268 |
|
|
269 |
@warning Each response serializer specified must be a subclass of `AFHTTPResponseSerializer`, and response to `-validateResponse:data:error:`. |
|
270 |
*/ |
633752
|
271 |
+ (instancetype)compoundSerializerWithResponseSerializers:(NSArray <id<AFURLResponseSerialization>> *)responseSerializers; |
6e1425
|
272 |
|
H |
273 |
@end |
|
274 |
|
|
275 |
///---------------- |
|
276 |
/// @name Constants |
|
277 |
///---------------- |
|
278 |
|
|
279 |
/** |
|
280 |
## Error Domains |
|
281 |
|
|
282 |
The following error domain is predefined. |
|
283 |
|
|
284 |
- `NSString * const AFURLResponseSerializationErrorDomain` |
|
285 |
|
|
286 |
### Constants |
|
287 |
|
|
288 |
`AFURLResponseSerializationErrorDomain` |
|
289 |
AFURLResponseSerializer errors. Error codes for `AFURLResponseSerializationErrorDomain` correspond to codes in `NSURLErrorDomain`. |
|
290 |
*/ |
633752
|
291 |
FOUNDATION_EXPORT NSString * const AFURLResponseSerializationErrorDomain; |
6e1425
|
292 |
|
H |
293 |
/** |
|
294 |
## User info dictionary keys |
|
295 |
|
|
296 |
These keys may exist in the user info dictionary, in addition to those defined for NSError. |
|
297 |
|
|
298 |
- `NSString * const AFNetworkingOperationFailingURLResponseErrorKey` |
|
299 |
- `NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey` |
|
300 |
|
|
301 |
### Constants |
|
302 |
|
|
303 |
`AFNetworkingOperationFailingURLResponseErrorKey` |
|
304 |
The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`. |
|
305 |
|
|
306 |
`AFNetworkingOperationFailingURLResponseDataErrorKey` |
|
307 |
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`. |
|
308 |
*/ |
633752
|
309 |
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseErrorKey; |
6e1425
|
310 |
|
633752
|
311 |
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey; |
6e1425
|
312 |
|
H |
313 |
NS_ASSUME_NONNULL_END |