Repository navigation
Expand file tree
/
Copy pathETBaseRequest.h
More file actions
executable file
·219 lines (109 loc) · 5.61 KB
/
Copy pathETBaseRequest.h
File metadata and controls
executable file
·219 lines (109 loc) · 5.61 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
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
//
// ETBaseRequest.h
// 能量圈
//
// Created by vj on 2017/3/27.
// Copyright © 2017年 Weijie Zhu. All rights reserved.
// 为什么要用_Nonnull http://stackoverflow.com/questions/32539285/pointer-is-missing-a-nullability-type-specifier
#import <Foundation/Foundation.h>
#import "ServerConfig.h"
@protocol AFMultipartFormData;
typedef void (^ETConstructingBlock)(_Nonnull id<AFMultipartFormData> formData);
typedef void (^ETURLSessionTaskProgressBlock)(NSProgress * _Nonnull);
@class ETBaseRequest;
typedef void(^ETRequestCompletionBlock)(__kindof ETBaseRequest * _Nonnull request);
typedef void (^AFURLSessionTaskProgressBlock)(NSProgress * _Nonnull);
@protocol ETRequestDelegate <NSObject>
@optional
/// 通过delegate的方式告知请求完成
///
/// @param 参数必须为ETBaseRequest类型.
- (void)requestFinished:(__kindof ETBaseRequest * _Nonnull)request;
/// 通过delegate的方式告知请求失败
///
/// @param 参数必须为ETBaseRequest类型.
- (void)requestFailed:(__kindof ETBaseRequest * _Nonnull)request;
@end
@protocol ETRequestAccessory <NSObject>
@optional
- (void)requestWillStart:(id _Nonnull)request;
- (void)requestWillStop:(id _Nonnull)request;
- (void)requestDidStop:(id _Nonnull)request;
@end
@interface ETBaseRequest : NSObject
#pragma mark - Request and Response Information
/// Http请求任务,只有在开始请求的时候才会有值,其余时间都为nil
@property (nonatomic, strong) NSURLSessionTask * _Nonnull requestTask;
@property (nonatomic, strong, readonly) NSURLRequest * _Nonnull currentRequest;
@property (nonatomic, strong, readonly) NSURLRequest * _Nonnull originalRequest;
@property (nonatomic, strong, readonly) NSHTTPURLResponse * _Nonnull response;
@property (nonatomic, readonly) NSInteger responseStatusCode;
// 响应报文的头部 请求失败时为nil
@property (nonatomic, strong, readonly, nullable) NSDictionary *responseHeaders;
// 响应报文Data类型 请求失败时为nil
@property (nonatomic, strong, readwrite, nullable) NSData *responseData;
// 响应报文String类型 请求失败时为nil
@property (nonatomic, strong, readwrite, nullable) NSString *responseString;
// 响应报文 数据类型根据'ETResponseSerializerType'来决定 如果是本地缓存的话将会返回本地的路径URL
@property (nonatomic, strong, readwrite, nullable) id responseObject;
// 响应报文 如果ETResponseSerializerType 为 'ETResponseSerializerTypeJSON' 那么返回的就是该值
@property (nonatomic, strong, readwrite, nullable) id responseJSONObject;
@property (nonatomic, strong, readwrite, nullable) NSError *error;
// 任务是否被取消
@property (nonatomic, readonly, getter=isCancelled) BOOL cancelled;
// 任务是否在执行状态
@property (nonatomic, readonly, getter=isExecuting) BOOL executing;
// 请求的id 初始值为0
@property (nonatomic) NSInteger tag;
@property (nonatomic, strong, nullable) NSDictionary *userInfo;
@property (nonatomic, weak, nullable) id<ETRequestDelegate> delegate;
@property (nonatomic, copy, nullable) ETRequestCompletionBlock successCompletionBlock;
@property (nonatomic, copy, nullable) ETRequestCompletionBlock failureCompletionBlock;
@property (nonatomic, strong, nullable) NSMutableArray<id<ETRequestAccessory>> *requestAccessories;
@property (nonatomic, copy, nullable) ETConstructingBlock constructingBodyBlock;
@property (nonatomic) ETRequestPriority requestPriority;
@property (nonatomic, strong, nullable) NSString *resumableDownloadPath;
@property (nonatomic, copy, nullable) AFURLSessionTaskProgressBlock resumableDownloadProgressBlock;
- (void)setCompletionBlockWithSuccess:(nullable ETRequestCompletionBlock)success
failure:(nullable ETRequestCompletionBlock)failure;
- (void)clearCompletionBlock;
#pragma mark - Request Action
///=============================================================================
/// @name Request Action
///=============================================================================
/// 开始请求并且放入请求队列
- (void)start;
/// 取消请求并从请求队列中移除
- (void)stop;
/// 开始请求的block,使用了这个函数就不用再另外调用start了
- (void)startWithCompletionBlockWithSuccess:(nullable ETRequestCompletionBlock)success
failure:(nullable ETRequestCompletionBlock)failure;
/// 请求超时时间
- (NSTimeInterval)requestTimeoutInterval;
#pragma mark - Subclass Override
/// 请求结束 预处理 在请求结束时线程还没切换到主线程的时候使用的后台线程
- (void)requestCompletePreprocessor;
/// 请求结束 数据处理 意思就是在请求结束后切换到主线程
- (void)requestCompleteFilter;
/// 请求失败 预处理 同‘requestCompletePreprocessor’
- (void)requestFailedPreprocessor;
/// 请求失败 数据处理 同'requestCompleteFilter'
- (void)requestFailedFilter;
- (ETRequestSerializerType)requestSerializerType;
- (ETResponseSerializerType)responseSerializerType;
//域名 服务器地址
- (NSString * _Nonnull)baseUrl;
//接口地址
- (NSString * _Nonnull)requestUrl;
// 接口请求类型 默认Get
- (ETAPIManagerRequestType)requestMethod;
- (nullable NSArray<NSString *> *)requestAuthorizationHeaderFieldArray;
- (nullable NSDictionary<NSString *, NSString *> *)requestHeaderFieldValueDictionary;
- (nullable NSURLRequest *)buildCustomUrlRequest;
- (_Nonnull id)cacheFileNameFilterForRequestArgument:(_Nonnull id)argument;
- (nullable id)requestArgument;
- (BOOL)allowsCellularAccess;
// json格式验证器
- (nullable id)jsonValidator;
- (BOOL)statusCodeValidator;
@end