123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //
- // RQHTTPService.h
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/16.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import <AFNetworking/AFNetworking.h>
- #import "AFHTTPSessionManager+RACSupport.h"
- #import "RACSubscriber+AFProgressCallbacks.h"
- #import "RQHTTPRequest.h"
- #import "RQHTTPResponse.h"
- //#import "RQUserModel.h"
- #define RQ_HTTP_Service [RQHTTPService sharedInstance]
- // The domain for all errors originating in RQHTTPService.
- FOUNDATION_EXTERN NSString *const RQHTTPServiceErrorDomain ;
- ///
- FOUNDATION_EXTERN NSString *const RQHTTPServiceErrorHTTPPResponseStatusCodeKey;
- FOUNDATION_EXTERN NSString *const RQHTTPServiceErrorHTTPPResponseMsgKey ;
- // A user info key associated with the NSURL of the request that failed.
- FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorRequestURLKey ;
- // A user info key associated with an NSNumber, indicating the HTTP status code
- // that was returned with the error.
- FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorHTTPStatusCodeKey ;
- /// The descriptive message returned from the API, e.g., "Validation Failed".
- FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorDescriptionKey ;
- /// An array of specific message strings returned from the API, e.g.,
- /// "No commits between joshaber:master and joshaber:feature".
- FOUNDATION_EXTERN NSString * const RQHTTPServiceErrorMessagesKey ;
- /// 连接服务器失败 default
- FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorConnectionFailed ;
- FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorJSONParsingFailed ;
- // The request was invalid (HTTP error 400).
- FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorBadRequest ;
- // The server is refusing to process the request because of an
- // authentication-related issue (HTTP error 403).
- //
- // Often, this means that there have been too many failed attempts to
- // authenticate. Even a successful authentication will not work while this error
- // code is being returned. The only recourse is to stop trying and wait for
- // a bit.
- FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorRequestForbidden ;
- // The server refused to process the request (HTTP error 422)
- FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorServiceRequestFailed ;
- // There was a problem establishing a secure connection, although the server is
- // reachable.
- FOUNDATION_EXTERN NSInteger const RQHTTPServiceErrorSecureConnectionFailed ;
- /// 用户数据配置完成
- FOUNDATION_EXTERN NSString *const RQUserDataConfigureCompleteNotification;
- /// 用户数据配置完成,取出userInfo 数据的的key
- FOUNDATION_EXTERN NSString *const RQUserDataConfigureCompleteUserInfoKey;
- /// 网络改变改变通知
- FOUNDATION_EXTERN NSString *const RQNetworkingReachabilityDidChangeNotification;
- /// 登录相关
- FOUNDATION_EXTERN NSString * const RQWeChatOnRespNotification;
- @interface RQHTTPService : AFHTTPSessionManager
- /// 单例
- +(instancetype) sharedInstance;
- @property (assign, readonly, nonatomic) AFNetworkReachabilityStatus networkReachabilityStatus;
- @end
- /// 请求类
- @interface RQHTTPService (Request)
- /// 1. 使用须知:后台返回数据的保证为👇固定格式 且`data:{}`必须为`字典`或者`NSNull`;
- /// {
- /// code:0,
- /// msg: "",
- /// data:{
- /// }
- /// }
- /// 这个方法返回的 signal 将会 send `RQHTTPResponse`这个实例,`parsedResult`就是对应键data对应的值, 如果你想获得里面的parsedResult实例,请使用以下方法
- /// [[self enqueueRequest:request resultClass:SBUser.class] sb_parsedResults];
- /// 这样取出来的就是 SBUser对象
- /// 2.使用方法如下
- /*
- /// 1. 配置参数
- RQKeyedSubscript *subscript = [RQKeyedSubscript subscript];
- subscript[@"page"] = @1;
-
- /// 2. 配置参数模型
- RQURLParameters *paramters = [RQURLParameters urlParametersWithMethod:@"GET" path:SUProduct parameters:subscript.dictionary];
-
- /// 3. 创建请求
- /// 3.1 resultClass 传入对象必须得是 RQObject的子类
- /// 3.2 resultClass 传入nil ,那么回调回来的值就是,服务器返回来的数据
- [[[[RQHTTPRequest requestWithParameters:paramters]
- enqueueResultClass:[RQGoodsData class]]
- sb_parsedResults]
- subscribeNext:^(RQGoodsData * goodsData) {
- /// 成功回调
-
- } error:^(NSError *error) {
- /// 失败回调
-
- } completed:^{
- /// 完成
-
- }];
-
- */
- /**
- Enqueues a request to be sent to the server.
- This will automatically fetch a of the given endpoint. Each object
- from each page will be sent independently on the returned signal, so
- subscribers don't have to know or care about this pagination behavior.
-
- @param request config the request
- @param resultClass A subclass of `RQObject` that the response data should be returned as,
- and will be accessible from the `parsedResult`
- @return Returns a signal which will send an instance of `RQHTTPResponse` for each parsed
- JSON object, then complete. If an error occurs at any point,
- the returned signal will send it immediately, then terminate.
- */
- -(RACSignal *)enqueueRequest:(RQHTTPRequest *) request
- resultClass:(Class /*subclass of RQObject*/) resultClass;
- /**
- 用来上传多个文件流,也可以上传单个文件
-
- @param request RQHTTPRequest
- @param resultClass 要转化出来的请求结果且必须是 `RQObject`的子类,否则Crash
- @param fileDatas 要上传的 文件数据,数组里面必须是装着` NSData ` 否则Crash
- @param name 这个是服务器的`资源文件名`,这个服务器会给出具体的数值,不能传nil 否则 Crach
- @param mimeType http://www.jianshu.com/p/a3e77751d37c 如果传nil ,则会传递 application/octet-stream
- @return Returns a signal which will send an instance of `RQHTTPResponse` for each parsed
- JSON object, then complete. If an error occurs at any point,
- the returned signal will send it immediately, then terminate.
- */
- - (RACSignal *)enqueueUploadRequest:(RQHTTPRequest *) request
- resultClass:(Class /*subclass of RQObject*/) resultClass
- fileDatas:(NSArray <NSData *> *)fileDatas
- name:(NSString *)name
- mimeType:(NSString *)mimeType;
- @end
|