BUBaseRequest.h 5.2 KB

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