基于AFNetworking 封装的网络库,同时参考了YTKNetwork ,一个请求一个对象。
目前只实现了基本的发送http请求,上传、下载文件,基本满足彩云的需求。
常量定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 typedef NS_ENUM (NSUInteger , SHMHTTPMethod) { SHMHTTPMethodGET = 333 , SHMHTTPMethodPOST, SHMHTTPMethodPUT, SHMHTTPMethodDELETE, }; typedef NS_ENUM (NSUInteger , SHMHRequestSerializerType) { SHMRequestSerializerTypeHTTP = 666 , SHMRequestSerializerTypeJSON, }; typedef NS_ENUM (NSUInteger , SHMResponseSerializerType) { SHMResponseSerializerTypeNone = 888 , SHMResponseSerializerTypeHTTP, SHMResponseSerializerTypeJOSN, SHMResponseSerializerTypeXMLParser, };
Protocol SHMHTTPRequesting 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @protocol SHMHTTPRequesting <NSObject >- (NSString *)host; - (NSString *)path; - (SHMHTTPMethod)method; - (NSDictionary *)parameters; - (NSDictionary *)httpHeader; - (NSData *)bodyData; - (BOOL )ignoreLocalCache; - (BOOL )responseDataZipped; - (SHMHRequestSerializerType)requestSerializerType; - (SHMResponseSerializerType)responseSerializerType; @optional - (NSTimeInterval )timeout; @end
SHMDownloadRequesting 1 2 3 4 5 6 7 8 9 10 11 12 @protocol SHMDownloadRequesting <NSObject >- (NSString *)host; - (NSString *)path; - (NSDictionary *)parameters; - (NSDictionary *)httpHeader; - (NSTimeInterval )timeout; - (NSString *)destFilePath; @end
SHMUploadRequesting 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @protocol SHMUploadRequesting <NSObject >- (NSString *)host; - (NSString *)path; - (NSDictionary *)parameters; - (NSDictionary *)httpHeader; - (SHMResponseSerializerType)responseSerializerType; @optional - (NSData *)fileData; - (void (^)(id <AFMultipartFormData>))fileDataConstructor; @end
Demo 一般请求 新建一个request类,继承自类 SHMHTTPRequest ,同时遵循协议 SHMHTTPRequesting 。请求里需要的参数,通过实现协议里的方法提供。
1 2 3 4 5 6 7 @interface WDRolodexCreateGroupRequest : SHMHTTPRequest <SHMHTTPRequesting >- (instancetype )initWithGroupName:(NSString *)groupName orgId:(int64_t)orgId; @end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 @implementation WDRolodexCreateGroupRequest { NSString *_groupName; int64_t _orgId; } - (instancetype )initWithGroupName:(NSString *)groupName orgId:(int64_t)orgId { self = [super init]; if (!self ) { return nil ; } _groupName = groupName; _orgId = orgId; return self ; } #pragma mark -- SHMHTTPRequesting - (NSString *)host { return [WDRolodexRequestTools rolodexRequestHost]; } - (NSString *)path { return @"group/create" ; } - (SHMHTTPMethod)method { return SHMHTTPMethodPOST; } - (NSDictionary *)parameters { return nil ; } - (NSDictionary *)httpHeader { NSMutableDictionary *headers = [[NSMutableDictionary alloc] init]; [headers addEntriesFromDictionary:[WDRolodexRequestTools rolodexRequestCommonHeadersWithOrgId:_orgId]]; headers[@"orgId" ] = @(_orgId).stringValue; return headers.copy; } - (NSData *)bodyData { NSMutableDictionary *bodyDic = [[NSMutableDictionary alloc] init]; bodyDic[@"name" ] = _groupName; NSData *bodyData = [NSJSONSerialization dataWithJSONObject:bodyDic options:NSJSONWritingPrettyPrinted error:nil ]; return bodyData; } - (BOOL )ignoreLocalCache { return YES ; } - (BOOL )responseDataZipped { return NO ; } - (SHMHRequestSerializerType)requestSerializerType { return SHMRequestSerializerTypeHTTP; } - (SHMResponseSerializerType)responseSerializerType { return SHMResponseSerializerTypeHTTP; } @end
下载 新建一个request类,继承自类 SHMDownloadRequest ,同时遵循协议 SHMDownloadRequesting 。请求里需要的参数,通过实现协议里的方法提供。
1 2 3 4 5 6 7 8 9 @interface WDDownloadFileRequest : SHMDownloadRequest <SHMDownloadRequesting >- (instancetype )initWithDownloadURL:(NSString *)downloadURL localFilePath:(NSString *)localFilePath; - (instancetype )initWithDownloadURL:(NSString *)downloadURL localFilePath:(NSString *)localFilePath timeout:(NSTimeInterval )timeout; @end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 @implementation WDDownloadFileRequest { NSString *_downloadURL; NSString *_localFilePath; NSTimeInterval _timeout; } - (instancetype )initWithDownloadURL:(NSString *)downloadURL localFilePath:(NSString *)localFilePath { return [self initWithDownloadURL:downloadURL localFilePath:localFilePath timeout:0 ]; } - (instancetype )initWithDownloadURL:(NSString *)downloadURL localFilePath:(NSString *)localFilePath timeout:(NSTimeInterval )timeout { self = [super init]; if (!self ) { return nil ; } _downloadURL = downloadURL; _localFilePath = localFilePath; _timeout = timeout; return self ; } #pragma mark -- SHMDownloadRequesting - (NSString *)host { return nil ; } - (NSString *)path { return _downloadURL; } - (NSDictionary *)parameters { return nil ; } - (NSDictionary *)httpHeader { return nil ; } - (NSTimeInterval )timeout { return _timeout; } - (NSString *)destFilePath { return _localFilePath; } @end
一般情况下,彩云里可以直接使用 WDDownloadFileRequest 来进行文件下载
上传 新建一个request类,继承自类 SHMUploadRequest ,同时遵循协议 SHMUploadRequesting 。请求里需要的参数,通过实现协议里的方法提供。
1 2 3 4 5 6 7 @interface WDUploadAvatarRequest : SHMUploadRequest <SHMUploadRequesting >- (instancetype )initWithImageData:(NSData *)imageData; @end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 @implementation WDUploadAvatarRequest { NSData *_imageData; } - (instancetype )initWithImageData:(NSData *)imageData { self = [super init]; if (!self ) { return nil ; } _imageData = imageData; return self ; } #pragma mark -- SHMUploadRequesting - (NSString *)host { return [[WDServerURLMgr sharedInstance] imHttpServerUrl]; } - (NSString *)path { return [NSString stringWithFormat:@"/sfs/upload/avatar%@" , [WDAvatarRequestTools requestToken]]; } - (NSDictionary *)parameters { return nil ; } - (NSDictionary *)httpHeader { return nil ; } - (NSData *)fileData { return _imageData; } @end