BUBaseRequest.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // BUBaseRequest.h
  3. // BUAdSDK
  4. //
  5. // Created by 李盛 on 2018/4/2.
  6. // Copyright © 2018年 bytedance. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. /// HTTP Request method.
  11. typedef NS_ENUM(NSInteger, BURequestMethod) {
  12. BURequestMethodGET = 0,
  13. BURequestMethodPOST,
  14. BURequestMethodHEAD,
  15. BURequestMethodPUT,
  16. BURequestMethodDELETE,
  17. BURequestMethodPATCH,
  18. };
  19. /// Request serializer type.
  20. typedef NS_ENUM(NSInteger, BURequestSerializerType) {
  21. BURequestSerializerTypeHTTP = 0,
  22. BURequestSerializerTypeJSON,
  23. };
  24. /// Response serializer type, which determines response serialization process and
  25. /// the type of `responseObject`.
  26. typedef NS_ENUM(NSInteger, BUResponseSerializerType) {
  27. /// NSData type
  28. BUResponseSerializerTypeHTTP,
  29. /// JSON object type
  30. BUResponseSerializerTypeJSON,
  31. };
  32. /// Request priority
  33. typedef NS_ENUM(NSInteger, BURequestPriority) {
  34. BURequestPriorityLow = -4L,
  35. BURequestPriorityDefault = 0,
  36. BURequestPriorityHigh = 4,
  37. };
  38. @protocol BU_AFMultipartFormData;
  39. typedef void (^BUAFConstructingBlock)(id<BU_AFMultipartFormData> formData);
  40. typedef void (^BUAFURLSessionTaskProgressBlock)(NSProgress *);
  41. @class BUBaseRequest;
  42. typedef void(^BURequestCompletionBlock)(__kindof BUBaseRequest *request);
  43. @interface BUBaseRequest : NSObject
  44. /// The underlying NSURLSessionTask.
  45. ///
  46. /// @warning This value is actually nil and should not be accessed before the request starts.
  47. @property (nonatomic, strong) NSURLSessionTask *requestTask;
  48. @property (nonatomic, strong) NSData *responseData;
  49. @property (nonatomic, strong) id responseJSONObject;
  50. @property (nonatomic, strong) id responseObject;
  51. @property (nonatomic, strong) NSString *responseString;
  52. @property (nonatomic, strong) NSError *error;
  53. @property (nonatomic, assign) BURequestMethod requestMethod;
  54. /// For post method, when httpbody can not be Serialized from NSDictionary json. if httpBody exists, please use httpBody directively and ignore 'requestArgument'
  55. @property (nonatomic, strong) NSData *httpBody;
  56. /// Shortcut for `requestTask.currentRequest`.当前活跃的request
  57. @property (nonatomic, strong, readonly) NSURLRequest *currentRequest;
  58. /// Shortcut for `requestTask.originalRequest`.在task创建的时候传入的request(有可能会重定向)
  59. @property (nonatomic, strong, readonly) NSURLRequest *originalRequest;
  60. /// Shortcut for `requestTask.response`.
  61. @property (nonatomic, strong, readonly) NSHTTPURLResponse *response;
  62. /// The response status code.
  63. @property (nonatomic, readonly) NSInteger responseStatusCode;
  64. /// The success callback. Note if this value is not nil and `requestFinished` delegate method is
  65. /// also implemented, both will be executed but delegate method is first called. This block
  66. /// will be called on the main queue.
  67. @property (nonatomic, copy, nullable) BURequestCompletionBlock successCompletionBlock;
  68. /// The failure callback. Note if this value is not nil and `requestFailed` delegate method is
  69. /// also implemented, both will be executed but delegate method is first called. This block
  70. /// will be called on the main queue.
  71. @property (nonatomic, copy, nullable) BURequestCompletionBlock failureCompletionBlock;
  72. /// Additional HTTP request header field.
  73. - (nullable NSDictionary<NSString *, NSString *> *)requestHeaderFieldValueDictionary;
  74. /// Request serializer type.
  75. - (BURequestSerializerType)requestSerializerType;
  76. /// Response serializer type. See also `responseObject`.
  77. - (BUResponseSerializerType)responseSerializerType;
  78. /// Request cache policy.
  79. - (NSURLRequestCachePolicy)bu_requestCachePolicy;
  80. //constructingBodyWithBlock:在此block种可以为上传的参数添加(拼接)新的需要的上传的数据,适用于上传给服务器的数据流比较大的时候
  81. @property (nonatomic, copy, nullable) BUAFConstructingBlock constructingBodyBlock;
  82. - (NSString *)requestUrl;
  83. - (NSString *)cdnUrl;
  84. - (NSString *)baseUrl;
  85. - (NSTimeInterval)requestTimeoutInterval;
  86. - (nullable id)requestArgument;
  87. /// Whether the request is allowed to use the cellular radio (if present). Default is YES.
  88. - (BOOL)allowsCellularAccess;
  89. /// Nil out both success and failure callback blocks.
  90. - (void)clearCompletionBlock;
  91. @property (nonatomic) BURequestPriority requestPriority;
  92. /// Should use CDN when sending request.
  93. - (BOOL)useCDN;
  94. #pragma mark - Request Action
  95. ///=============================================================================
  96. /// @name Request Action
  97. ///=============================================================================
  98. /// Append self to request queue and start the request.
  99. - (void)start;
  100. /// Remove self from request queue and cancel the request.
  101. - (void)stop;
  102. /// Convenience method to start the request with block callbacks.
  103. - (void)startWithCompletionBlockWithSuccess:(nullable BURequestCompletionBlock)success
  104. failure:(nullable BURequestCompletionBlock)failure;
  105. /// Return cancelled state of request task.
  106. @property (nonatomic, readonly, getter=isCancelled) BOOL cancelled;
  107. /// Executing state of request task.
  108. @property (nonatomic, readonly, getter=isExecuting) BOOL executing;
  109. @end
  110. NS_ASSUME_NONNULL_END