BUBaseRequest.h
5.1 KB
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
//
// BUBaseRequest.h
// BUAdSDK
//
// Created by 李盛 on 2018/4/2.
// Copyright © 2018年 bytedance. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/// HTTP Request method.
typedef NS_ENUM(NSInteger, BURequestMethod) {
BURequestMethodGET = 0,
BURequestMethodPOST,
BURequestMethodHEAD,
BURequestMethodPUT,
BURequestMethodDELETE,
BURequestMethodPATCH,
};
/// Request serializer type.
typedef NS_ENUM(NSInteger, BURequestSerializerType) {
BURequestSerializerTypeHTTP = 0,
BURequestSerializerTypeJSON,
};
/// Response serializer type, which determines response serialization process and
/// the type of `responseObject`.
typedef NS_ENUM(NSInteger, BUResponseSerializerType) {
/// NSData type
BUResponseSerializerTypeHTTP,
/// JSON object type
BUResponseSerializerTypeJSON,
};
/// Request priority
typedef NS_ENUM(NSInteger, BURequestPriority) {
BURequestPriorityLow = -4L,
BURequestPriorityDefault = 0,
BURequestPriorityHigh = 4,
};
@protocol BU_AFMultipartFormData;
typedef void (^BUAFConstructingBlock)(id<BU_AFMultipartFormData> formData);
typedef void (^BUAFURLSessionTaskProgressBlock)(NSProgress *);
@class BUBaseRequest;
typedef void(^BURequestCompletionBlock)(__kindof BUBaseRequest *request);
@interface BUBaseRequest : NSObject
/// The underlying NSURLSessionTask.
///
/// @warning This value is actually nil and should not be accessed before the request starts.
@property (nonatomic, strong) NSURLSessionTask *requestTask;
@property (nonatomic, strong) NSData *responseData;
@property (nonatomic, strong) id responseJSONObject;
@property (nonatomic, strong) id responseObject;
@property (nonatomic, strong) NSString *responseString;
@property (nonatomic, strong) NSError *error;
@property (nonatomic, assign) BURequestMethod requestMethod;
/// For post method, when httpbody can not be Serialized from NSDictionary json. if httpBody exists, please use httpBody directively and ignore 'requestArgument'
@property (nonatomic, strong) NSData *httpBody;
/// Shortcut for `requestTask.currentRequest`.当前活跃的request
@property (nonatomic, strong, readonly) NSURLRequest *currentRequest;
/// Shortcut for `requestTask.originalRequest`.在task创建的时候传入的request(有可能会重定向)
@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 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) BURequestCompletionBlock 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) BURequestCompletionBlock failureCompletionBlock;
/// Additional HTTP request header field.
- (nullable NSDictionary<NSString *, NSString *> *)requestHeaderFieldValueDictionary;
/// Request serializer type.
- (BURequestSerializerType)requestSerializerType;
/// Response serializer type. See also `responseObject`.
- (BUResponseSerializerType)responseSerializerType;
/// Request cache policy.
- (NSURLRequestCachePolicy)bu_requestCachePolicy;
//constructingBodyWithBlock:在此block种可以为上传的参数添加(拼接)新的需要的上传的数据,适用于上传给服务器的数据流比较大的时候
@property (nonatomic, copy, nullable) BUAFConstructingBlock constructingBodyBlock;
- (NSString *)requestUrl;
- (NSString *)cdnUrl;
- (NSString *)baseUrl;
- (NSTimeInterval)requestTimeoutInterval;
- (nullable id)requestArgument;
/// Whether the request is allowed to use the cellular radio (if present). Default is YES.
- (BOOL)allowsCellularAccess;
/// Nil out both success and failure callback blocks.
- (void)clearCompletionBlock;
@property (nonatomic) BURequestPriority requestPriority;
/// Should use CDN when sending request.
- (BOOL)useCDN;
#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 BURequestCompletionBlock)success
failure:(nullable BURequestCompletionBlock)failure;
/// 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;
@end
NS_ASSUME_NONNULL_END