IOS network request encapsulation, based on AFN (iOS)

Github link: https://github.com/Charlesyaoxin/CYBaseRequest

AFNetWorking-> CYRequestManager-> CYBaseRequest

Encapsulated a network request, involving three classes

1 AFNetworking 2 CYRequestManager 3 CYBaseRequest
Design idea
  1. First package AFNetworking, that is CYRequestManager
  2. However, CYRequestManager requires parameters, links, parameters and links are too exposed, so the design of the CYBaseRequest class
  3. When using the inheritance CYBaseRequest, the class attributes as a parameter (model to JSON), the property into a dictionary parameter, send a request, you can
  4. Simple packaging, after gradually improve
Use case 1

Direct use of CYBaseRequest

1 CYBaseRequest *request / / create the request new] = [CYBaseRequest; request.url = 2; / / @ "www.baidu.com" (BOOL success sendRequestWithCompletionHandle:^ [request request, ID response, NSString *message) {}]};
Use case 2

Create a CYUserInfoRequest inherited from CYBaseRequest

CYUserInfoRequest.h @interface CYUserInfoRequest: CYBaseRequest @property (nonatomic, assign) NSInteger userId; @end CYUserInfoRequest.m @implementation CYUserInfoRequest (instancetype init) {self = [super init]; if (self) {self.url = @ "request link";} return self;} @end how to use? @interface ViewController (@end @implementation ViewController) - (void) viewDidLoad {[super viewDidLoad]; / / CYUserInfoRequest *userInfoR 1 to create the request = [CYUserInfoRequest new]; userInfoR.userId = 1; / / can be set in.M / / userInfoR.url = @ "www.baidu.com"; / / / / / / @{optional userInfoR.extraParams = @ @ "username": "XXX"} / / / /; 2 set the request of userInfoR.methodType = CYRequestMethodTypePOST; / / 3 sendRequestWithCompletionHandle:^ (BOOL success [userInfoR request, ID response, NSString *message) {@end}}}];
Design code example – CYRequestManager
#import "AFNetworking.h" #import "CYRequestConstant.h" / / request success callback typedef (^CYRequestManagerSuccessHandle) void (NSURLSessionTask *task, ID response); / / request failed callback typedef (^CYRequestManagerFailureHandle) void (NSURLSessionTask *task, NSError *error); @interface CYRequestManager: NSObject request By NSURLSession @param URLString / * * request link @param params request parameter @param methodType @param serializerType request data analysis of type @param successHandle request @param failureHandle request failed success callback callback * + (void) requestWithURLString: (NSString * URLString) params: (NSDictionary *) params methodType: (CYRequestMethodType) methodType serializerT Ype: (CYResponseSeializerType) serializerType successHandle: (CYRequestManagerSuccessHandle) successHandle failureHandle: (CYRequestManagerFailureHandle) failureHandle a multipart `POST` request By; / * * NSURLSession @param URLString @ param params request link request parameters @param analysis of serializerType data type @param constructingBody add @param successHandle request body request success callback @param failureHandle request failed callback * (void) + POSTWithURLString: (NSString *) URLString params: (NSDictionary *) params serializerType: (CYResponseSeializerType) serializerType (constructingBody: (void ^) (id< AFMultipartFormData> formData) constructingBody successHa) Ndle: (CYRequestManagerSuccessHandle) successHandle failureHandle: (CYRequestManagerFailureHandle) failureHandle; @end #import "CYRequestManager.h" #import "AFNetworking.h" @interface AFHTTPSessionManager (Shared) + (instancetype) sharedManager; @end @implementation AFHTTPSessionManager (Shared) + (instancetype) sharedManager static AFHTTPSessionManager static dispatch_once_t {*_singleton; onceToken; dispatch_once (& onceToken, _singleton = ^{[AFHTTPSessionManager manager]; _singleton.responseSerializer.acceptableContentTypes [NSSet setWithObjects:@ = "application/json", "text/plain", @ @ @ "text/json", "text/javascript", "text/html", @ nil]; return _singleton;}}); @end @implementation CYR EquestManager + (void) requestWithURLString: (NSString * URLString) params: (NSDictionary *) params methodType: (CYRequestMethodType) methodType serializerType: (CYResponseSeializerType) serializerType successHandle: (CYRequestManagerSuccessHandle) successHandle failureHandle: (CYRequestManagerFailureHandle failureHandle) {AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager] = [self responseSearalizerWithSerilizerType:; manager.responseSerializer serializerType]; switch (methodType) case CYRequestMethodTypeGET: [manager GET:URLString parameters:params {progress:nil success:successHandle failure:failureHandle]; break; case CYRequestMethodTypePOST: [manager POST:URLString parameters:params progress:nil success:successHandle failure:failureHandle] case CYRequestMethodTypePUT: [manager PUT:URLString; break; parameters:params success:successHandle failure:failureHandle]; break case; CYRequestMethodTypePATCH: [manager PATCH:URLString parameters:params success:successHandle failure:failureHandle] break; case CYRequestMethodTypeDELETE: [manager; DELETE:URLString parameters:params success:successHandle failure: failureHandle] break; case CYRequestMethodTypeHEAD: [manager; HEAD:URLString parameters:params success:^ (NSURLSessionDataTask * _Nonnull task) {! SuccessHandle?: successHandle (task, Nil) failure:failureH;} Andle]; break;}} + (void) POSTWithURLString: (NSString * URLString) params: (NSDictionary *) params serializerType: (CYResponseSeializerType) serializerType (constructingBody: (void ^) (id< AFMultipartFormData> formData)) constructingBody successHandle: (CYRequestManagerSuccessHandle) successHandle failureHandle: (CYRequestManagerFailureHandle failureHandle) {AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager] manager.responseSerializer = [self; responseSearalizerWithSerilizerType:serializerType]; [manager POST:URLString parameters:params constructingBodyWithBlock:constructingBody progress:nil success:successHandle failure:failureHandle];} + (A FHTTPResponseSerializer *) responseSearalizerWithSerilizerType: (CYResponseSeializerType) serilizerType (serilizerType) {switch {case CYResponseSeializerTypeDefault: return [AFJSONResponseSerializer serializer] case CYResponseSeializerTypeJSON: return [AFJSONResponseSerializer; break; serializer]; break; case CYResponseSeializerTypeXML: return [AFXMLParserResponseSerializer serializer]; break case; CYResponseSeializerTypePlist: return break; case serializer] AFPropertyListResponseSerializer; CYResponseSeializerTypeCompound: return [AFCompoundResponseSerializer serializer] case CYResponseSeializerTypeImage: return [AFImageResponseSerializer; break; serializer]; break; case CYRespon SeSeializerTypeData: return serializer] [AFHTTPResponseSerializer; break; default: [AFJSONResponseSerializer serializer]; break;}} @end return
Design code example – CYBaseRequest
#import "CYRequestConstant.h" typedef void (^CYBaseRequestCompletionHandle) (BOOL success, ID response, NSString *message); @interface CYBaseRequest: NSObject @property (nonatomic, copy) NSString *url @property (nonatomic, assign); CYRequestMethodType methodType; @property (nonatomic, strong) NSDictionary *extraParams; (void) - sendRequestWithCompletionHandle: (CYBaseRequestCompletionHandle) completionHandle @end #import; "CYBaseRequest.h" #import "YYModel.h" #import "CYRequestManager.h" @implementation CYBaseRequest (void) - sendRequestWithCompletionHandle: (CYBaseRequestCompletionHandle completionHandle) {if (_url ||!! _url.length return [CYRequestManager requestWithURLString:_url params:); [self params] methodType:self.methodType serializerType:CYResponseSeializerTypeJSON successHandle:^ (NSURLSessionTask *task, ID response) {! CompletionHandle?: completionHandle (YES, response, Nil);} failureHandle:^ (NSURLSessionTask *task, NSError *error) {! CompletionHandle?: completionHandle (NO, nil, error.description);}];} - (NSDictionary * params) {NSDictionary *json = self.yy_modelToJSONObject; if ([json isKindOfClass:[NSDictionary class]]) {NSMutableDictionary *params = [(NSDictionary *) JSON mutableCopy] [params; addEntriesFromDictionary:self.extraParams]; for (NSString *delKey in @[@ "URL", "methodType", "@ @ extraParams") {if ([params.allKeys containsObject:delKey]) {[params}} (removeObjectForKey:delKey]; for ID key in params.allKeys obj [params) {id = objectForKey:key]; if (obj [obj isKindOfClass:[NSNull class]]! || || (obj = = ID kCFNull)) {[params removeObjectForKey:key]}}}; return params; return self.extraParams @end;}