-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
YTKBaseRequest.h
340 lines (258 loc) · 14 KB
/
YTKBaseRequest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// YTKBaseRequest.h
//
// Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
FOUNDATION_EXPORT NSString *const YTKRequestValidationErrorDomain;
NS_ENUM(NSInteger) {
YTKRequestValidationErrorInvalidStatusCode = -8,
YTKRequestValidationErrorInvalidJSONFormat = -9,
};
/// HTTP Request method.
typedef NS_ENUM(NSInteger, YTKRequestMethod) {
YTKRequestMethodGET = 0,
YTKRequestMethodPOST,
YTKRequestMethodHEAD,
YTKRequestMethodPUT,
YTKRequestMethodDELETE,
YTKRequestMethodPATCH,
};
/// Request serializer type.
typedef NS_ENUM(NSInteger, YTKRequestSerializerType) {
YTKRequestSerializerTypeHTTP = 0,
YTKRequestSerializerTypeJSON,
};
/// Response serializer type, which determines response serialization process and
/// the type of `responseObject`.
typedef NS_ENUM(NSInteger, YTKResponseSerializerType) {
/// NSData type
YTKResponseSerializerTypeHTTP,
/// JSON object type
YTKResponseSerializerTypeJSON,
/// NSXMLParser type
YTKResponseSerializerTypeXMLParser,
};
/// Request priority
typedef NS_ENUM(NSInteger, YTKRequestPriority) {
YTKRequestPriorityLow = -4L,
YTKRequestPriorityDefault = 0,
YTKRequestPriorityHigh = 4,
};
@protocol AFMultipartFormData;
typedef void (^AFConstructingBlock)(id<AFMultipartFormData> formData);
typedef void (^AFURLSessionTaskProgressBlock)(NSProgress *);
@class YTKBaseRequest;
typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request);
/// The YTKRequestDelegate protocol defines several optional methods you can use
/// to receive network-related messages. All the delegate methods will be called
/// on the main queue.
@protocol YTKRequestDelegate <NSObject>
@optional
/// Tell the delegate that the request has finished successfully.
///
/// @param request The corresponding request.
- (void)requestFinished:(__kindof YTKBaseRequest *)request;
/// Tell the delegate that the request has failed.
///
/// @param request The corresponding request.
- (void)requestFailed:(__kindof YTKBaseRequest *)request;
@end
/// The YTKRequestAccessory protocol defines several optional methods that can be
/// used to track the status of a request. Objects that conforms this protocol
/// ("accessories") can perform additional configurations accordingly. All the
/// accessory methods will be called on the main queue.
@protocol YTKRequestAccessory <NSObject>
@optional
/// Inform the accessory that the request is about to start.
///
/// @param request The corresponding request.
- (void)requestWillStart:(id)request;
/// Inform the accessory that the request is about to stop. This method is called
/// before executing `requestFinished` and `successCompletionBlock`.
///
/// @param request The corresponding request.
- (void)requestWillStop:(id)request;
/// Inform the accessory that the request has already stoped. This method is called
/// after executing `requestFinished` and `successCompletionBlock`.
///
/// @param request The corresponding request.
- (void)requestDidStop:(id)request;
@end
/// YTKBaseRequest is the abstract class of network request. It provides many options
/// for constructing request. It's the base class of `YTKRequest`.
@interface YTKBaseRequest : NSObject
#pragma mark - Request and Response Information
///=============================================================================
/// @name Request and Response Information
///=============================================================================
/// The underlying NSURLSessionTask.
///
/// @warning This value is actually nil and should not be accessed before the request starts.
@property (nonatomic, strong, readonly) NSURLSessionTask *requestTask;
/// Shortcut for `requestTask.currentRequest`.
@property (nonatomic, strong, readonly) NSURLRequest *currentRequest;
/// Shortcut for `requestTask.originalRequest`.
@property (nonatomic, strong, readonly) NSURLRequest *originalRequest;
/// Shortcut for `requestTask.response`.
@property (nonatomic, strong, readonly) NSHTTPURLResponse *response;
/// The response status code.
@property (nonatomic, readonly) NSInteger responseStatusCode;
/// The response header fields.
@property (nonatomic, strong, readonly, nullable) NSDictionary *responseHeaders;
/// The raw data representation of response. Note this value can be nil if request failed.
@property (nonatomic, strong, readonly, nullable) NSData *responseData;
/// The string representation of response. Note this value can be nil if request failed.
@property (nonatomic, strong, readonly, nullable) NSString *responseString;
/// This serialized response object. The actual type of this object is determined by
/// `YTKResponseSerializerType`. Note this value can be nil if request failed.
///
/// @discussion If `resumableDownloadPath` and DownloadTask is using, this value will
/// be the path to which file is successfully saved (NSURL), or nil if request failed.
@property (nonatomic, strong, readonly, nullable) id responseObject;
/// If you use `YTKResponseSerializerTypeJSON`, this is a convenience (and sematic) getter
/// for the response object. Otherwise this value is nil.
@property (nonatomic, strong, readonly, nullable) id responseJSONObject;
/// This error can be either serialization error or network error. If nothing wrong happens
/// this value will be nil.
@property (nonatomic, strong, readonly, nullable) NSError *error;
/// Return cancelled state of request task.
@property (nonatomic, readonly, getter=isCancelled) BOOL cancelled;
/// Executing state of request task.
@property (nonatomic, readonly, getter=isExecuting) BOOL executing;
#pragma mark - Request Configuration
///=============================================================================
/// @name Request Configuration
///=============================================================================
/// Tag can be used to identify request. Default value is 0.
@property (nonatomic) NSInteger tag;
/// The userInfo can be used to store additional info about the request. Default is nil.
@property (nonatomic, strong, nullable) NSDictionary *userInfo;
/// The delegate object of the request. If you choose block style callback you can ignore this.
/// Default is nil.
@property (nonatomic, weak, nullable) id<YTKRequestDelegate> delegate;
/// The success callback. Note if this value is not nil and `requestFinished` delegate method is
/// also implemented, both will be executed but delegate method is first called. This block
/// will be called on the main queue.
@property (nonatomic, copy, nullable) YTKRequestCompletionBlock successCompletionBlock;
/// The failure callback. Note if this value is not nil and `requestFailed` delegate method is
/// also implemented, both will be executed but delegate method is first called. This block
/// will be called on the main queue.
@property (nonatomic, copy, nullable) YTKRequestCompletionBlock failureCompletionBlock;
/// This can be used to add several accessories object. Note if you use `addAccessory` to add accessory
/// this array will be automatically created. Default is nil.
@property (nonatomic, strong, nullable) NSMutableArray<id<YTKRequestAccessory>> *requestAccessories;
/// This can be use to construct HTTP body when needed in POST request. Default is nil.
@property (nonatomic, copy, nullable) AFConstructingBlock constructingBodyBlock;
/// This value is used to perform resumable download request. Default is nil.
///
/// @discussion NSURLSessionDownloadTask is used when this value is not nil.
/// The exist file at the path will be removed before the request starts. If request succeed, file will
/// be saved to this path automatically, otherwise the response will be saved to `responseData`
/// and `responseString`. For this to work, server must support `Range` and response with
/// proper `Last-Modified` and/or `Etag`. See `NSURLSessionDownloadTask` for more detail.
@property (nonatomic, strong, nullable) NSString *resumableDownloadPath;
/// You can use this block to track the download progress. See also `resumableDownloadPath`.
@property (nonatomic, copy, nullable) AFURLSessionTaskProgressBlock resumableDownloadProgressBlock;
/// You can use this block to track the upload progress.
@property (nonatomic, copy, nullable) AFURLSessionTaskProgressBlock uploadProgressBlock;
/// The priority of the request. Effective only on iOS 8+. Default is `YTKRequestPriorityDefault`.
@property (nonatomic) YTKRequestPriority requestPriority;
/// Set completion callbacks
- (void)setCompletionBlockWithSuccess:(nullable YTKRequestCompletionBlock)success
failure:(nullable YTKRequestCompletionBlock)failure;
/// Nil out both success and failure callback blocks.
- (void)clearCompletionBlock;
/// Convenience method to add request accessory. See also `requestAccessories`.
- (void)addAccessory:(id<YTKRequestAccessory>)accessory;
#pragma mark - Request Action
///=============================================================================
/// @name Request Action
///=============================================================================
/// Append self to request queue and start the request.
- (void)start;
/// Remove self from request queue and cancel the request.
- (void)stop;
/// Convenience method to start the request with block callbacks.
- (void)startWithCompletionBlockWithSuccess:(nullable YTKRequestCompletionBlock)success
failure:(nullable YTKRequestCompletionBlock)failure;
#pragma mark - Subclass Override
///=============================================================================
/// @name Subclass Override
///=============================================================================
/// Called on background thread after request succeeded but before switching to main thread. Note if
/// cache is loaded, this method WILL be called on the main thread, just like `requestCompleteFilter`.
- (void)requestCompletePreprocessor;
/// Called on the main thread after request succeeded.
- (void)requestCompleteFilter;
/// Called on background thread after request failed but before switching to main thread. See also
/// `requestCompletePreprocessor`.
- (void)requestFailedPreprocessor;
/// Called on the main thread when request failed.
- (void)requestFailedFilter;
/// The baseURL of request. This should only contain the host part of URL, e.g., http://www.example.com.
/// See also `requestUrl`
- (NSString *)baseUrl;
/// The URL path of request. This should only contain the path part of URL, e.g., /v1/user. See alse `baseUrl`.
///
/// @discussion This will be concated with `baseUrl` using [NSURL URLWithString:relativeToURL].
/// Because of this, it is recommended that the usage should stick to rules stated above.
/// Otherwise the result URL may not be correctly formed. See also `URLString:relativeToURL`
/// for more information.
///
/// Additionally, if `requestUrl` itself is a valid URL, it will be used as the result URL and
/// `baseUrl` will be ignored.
- (NSString *)requestUrl;
/// Optional CDN URL for request.
- (NSString *)cdnUrl;
/// Request timeout interval. Default is 60s.
///
/// @discussion When using `resumableDownloadPath`(NSURLSessionDownloadTask), the session seems to completely ignore
/// `timeoutInterval` property of `NSURLRequest`. One effective way to set timeout would be using
/// `timeoutIntervalForResource` of `NSURLSessionConfiguration`.
- (NSTimeInterval)requestTimeoutInterval;
/// Additional request argument.
- (nullable id)requestArgument;
/// Override this method to filter requests with certain arguments when caching.
- (id)cacheFileNameFilterForRequestArgument:(id)argument;
/// HTTP request method.
- (YTKRequestMethod)requestMethod;
/// Request serializer type.
- (YTKRequestSerializerType)requestSerializerType;
/// Response serializer type. See also `responseObject`.
- (YTKResponseSerializerType)responseSerializerType;
/// Username and password used for HTTP authorization. Should be formed as @[@"Username", @"Password"].
- (nullable NSArray<NSString *> *)requestAuthorizationHeaderFieldArray;
/// Additional HTTP request header field.
- (nullable NSDictionary<NSString *, NSString *> *)requestHeaderFieldValueDictionary;
/// Use this to build custom request. If this method return non-nil value, `requestUrl`, `requestTimeoutInterval`,
/// `requestArgument`, `allowsCellularAccess`, `requestMethod` and `requestSerializerType` will all be ignored.
- (nullable NSURLRequest *)buildCustomUrlRequest;
/// Should use CDN when sending request.
- (BOOL)useCDN;
/// Whether the request is allowed to use the cellular radio (if present). Default is YES.
- (BOOL)allowsCellularAccess;
/// The validator will be used to test if `responseJSONObject` is correctly formed.
- (nullable id)jsonValidator;
/// This validator will be used to test if `responseStatusCode` is valid.
- (BOOL)statusCodeValidator;
@end
NS_ASSUME_NONNULL_END