RQHTTPService.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // RQHTTPService.h
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/16.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import <AFNetworking/AFNetworking.h>
  9. #import "AFHTTPSessionManager+RACSupport.h"
  10. #import "RACSubscriber+AFProgressCallbacks.h"
  11. #import "RQHTTPRequest.h"
  12. #import "RQHTTPResponse.h"
  13. //#import "RQUserModel.h"
  14. #define RQ_HTTP_Service [RQHTTPService sharedInstance]
  15. // The domain for all errors originating in RQHTTPService.
  16. FOUNDATION_EXTERN NSString *const RQHTTPServiceErrorDomain ;
  17. ///
  18. FOUNDATION_EXTERN NSString *const RQHTTPServiceErrorHTTPPResponseStatusCodeKey;
  19. FOUNDATION_EXTERN NSString *const RQHTTPServiceErrorHTTPPResponseMsgKey ;
  20. // A user info key associated with the NSURL of the request that failed.
  21. FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorRequestURLKey ;
  22. // A user info key associated with an NSNumber, indicating the HTTP status code
  23. // that was returned with the error.
  24. FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorHTTPStatusCodeKey ;
  25. /// The descriptive message returned from the API, e.g., "Validation Failed".
  26. FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorDescriptionKey ;
  27. /// An array of specific message strings returned from the API, e.g.,
  28. /// "No commits between joshaber:master and joshaber:feature".
  29. FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorMessagesKey ;
  30. /// 连接服务器失败 default
  31. FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorConnectionFailed ;
  32. FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorJSONParsingFailed ;
  33. // The request was invalid (HTTP error 400).
  34. FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorBadRequest ;
  35. // The server is refusing to process the request because of an
  36. // authentication-related issue (HTTP error 403).
  37. //
  38. // Often, this means that there have been too many failed attempts to
  39. // authenticate. Even a successful authentication will not work while this error
  40. // code is being returned. The only recourse is to stop trying and wait for
  41. // a bit.
  42. FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorRequestForbidden ;
  43. // The server refused to process the request (HTTP error 422)
  44. FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorServiceRequestFailed ;
  45. // There was a problem establishing a secure connection, although the server is
  46. // reachable.
  47. FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorSecureConnectionFailed ;
  48. /// 用户数据配置完成
  49. FOUNDATION_EXTERN NSString *const RQUserDataConfigureCompleteNotification;
  50. /// 用户数据配置完成,取出userInfo 数据的的key
  51. FOUNDATION_EXTERN NSString *const RQUserDataConfigureCompleteUserInfoKey;
  52. /// 网络改变改变通知
  53. FOUNDATION_EXTERN NSString *const RQNetworkingReachabilityDidChangeNotification;
  54. /// 登录相关
  55. FOUNDATION_EXTERN NSString * const RQWeChatOnRespNotification;
  56. @interface RQHTTPService : AFHTTPSessionManager
  57. /// 单例
  58. +(instancetype) sharedInstance;
  59. @property (assign, readonly, nonatomic) AFNetworkReachabilityStatus networkReachabilityStatus;
  60. @end
  61. /// 请求类
  62. @interface RQHTTPService (Request)
  63. /// 1. 使用须知:后台返回数据的保证为👇固定格式 且`data:{}`必须为`字典`或者`NSNull`;
  64. /// {
  65. /// code:0,
  66. /// msg: "",
  67. /// data:{
  68. /// }
  69. /// }
  70. /// 这个方法返回的 signal 将会 send `RQHTTPResponse`这个实例,`parsedResult`就是对应键data对应的值, 如果你想获得里面的parsedResult实例,请使用以下方法
  71. /// [[self enqueueRequest:request resultClass:SBUser.class] sb_parsedResults];
  72. /// 这样取出来的就是 SBUser对象
  73. /// 2.使用方法如下
  74. /*
  75. /// 1. 配置参数
  76. RQKeyedSubscript *subscript = [RQKeyedSubscript subscript];
  77. subscript[@"page"] = @1;
  78. /// 2. 配置参数模型
  79. RQURLParameters *paramters = [RQURLParameters urlParametersWithMethod:@"GET" path:SUProduct parameters:subscript.dictionary];
  80. /// 3. 创建请求
  81. /// 3.1 resultClass 传入对象必须得是 RQObject的子类
  82. /// 3.2 resultClass 传入nil ,那么回调回来的值就是,服务器返回来的数据
  83. [[[[RQHTTPRequest requestWithParameters:paramters]
  84. enqueueResultClass:[RQGoodsData class]]
  85. sb_parsedResults]
  86. subscribeNext:^(RQGoodsData * goodsData) {
  87. /// 成功回调
  88. } error:^(NSError *error) {
  89. /// 失败回调
  90. } completed:^{
  91. /// 完成
  92. }];
  93. */
  94. /**
  95. Enqueues a request to be sent to the server.
  96. This will automatically fetch a of the given endpoint. Each object
  97. from each page will be sent independently on the returned signal, so
  98. subscribers don't have to know or care about this pagination behavior.
  99. @param request config the request
  100. @param resultClass A subclass of `RQObject` that the response data should be returned as,
  101. and will be accessible from the `parsedResult`
  102. @return Returns a signal which will send an instance of `RQHTTPResponse` for each parsed
  103. JSON object, then complete. If an error occurs at any point,
  104. the returned signal will send it immediately, then terminate.
  105. */
  106. -(RACSignal *)enqueueRequest:(RQHTTPRequest *) request
  107. resultClass:(Class /*subclass of RQObject*/) resultClass;
  108. /**
  109. 用来上传多个文件流,也可以上传单个文件
  110. @param request RQHTTPRequest
  111. @param resultClass 要转化出来的请求结果且必须是 `RQObject`的子类,否则Crash
  112. @param fileDatas 要上传的 文件数据,数组里面必须是装着` NSData ` 否则Crash
  113. @param name 这个是服务器的`资源文件名`,这个服务器会给出具体的数值,不能传nil 否则 Crach
  114. @param mimeType http://www.jianshu.com/p/a3e77751d37c 如果传nil ,则会传递 application/octet-stream
  115. @return Returns a signal which will send an instance of `RQHTTPResponse` for each parsed
  116. JSON object, then complete. If an error occurs at any point,
  117. the returned signal will send it immediately, then terminate.
  118. */
  119. - (RACSignal *)enqueueUploadRequest:(RQHTTPRequest *) request
  120. resultClass:(Class /*subclass of RQObject*/) resultClass
  121. fileDatas:(NSArray <NSData *> *)fileDatas
  122. name:(NSString *)name
  123. mimeType:(NSString *)mimeType;
  124. @end