RC_NetworkProxy 0.1.5

RC_NetworkProxy 0.1.5

Hymn-RoyCHANG 维护。



  • Roy CHANG

RC_NetworkProxy

RC_NetworkProxy 是一个用于发送 POSTGET 等的简单网络代理协议
RC_NetworkProxy 是一个用来发送 POSTGET 等的简单网络代理协议

1. 它是什么意思

您可以使用但不仅限于 ‘AFNetworking’ 来实现此协议。目前,它使用 3.1.2 版本的 'AFNetworking' 实现该协议。

1.1 如何自定义网络代理协议

/// the network proxy protocol
@protocol RC_NetworkProtocol <NSObject>

@required
...
@optional
...
@end

/// -------- custom your protocol --------

@interface SomeProtocol_Imp : SomeSuperObj <RC_NetworkProtocol>
//... your code here...
@end

/// implementation
@implementation SomeProtocol_Imp

/// -------- required --------

+ (id<RC_NetworkProtocol>)rc_sharedProtocol{

    /// your code here
    return your_shared_protocol_instance;
}

+ (id<RC_NetworkProtocol>)rc_protocol{
    
    /// your code here
    return your_protocol_instance;
}

- (void)rc_addHTTPHeaders:(NSDictionary<NSString*, NSString*> *)headers{
    
    /// your code here
}

- (void)rc_sendRequest:(RC_HTTPRequest *)request completionHandler:(RC_NetworkProtocolHandler)handler{

    /// your code here
}

/// -------- optional --------

- (void)rc_setTimeout:(NSTimeInterval)timeout{
    /// your code here
}

@end

2. 如何使用

两种使用方法

2.1 Podfile

...
pod 'RC_NetworkPorxy', '~> 0.1.5'
....
pod install

2.2 将文件添加到项目中

下载源代码文件并将它们添加到您的项目中

#import "RC_NetworkPorxy.h"

2.3 使用方法

/// url
NSString *host = @"http:/xxx.yyy.zzz.com";
NSString *api = @"/add/bookmark/";
/// full string is: http:/xxx.yyy.zzz.com//add/bookmark/
NSString *url = RC_HTTPURL(host, api);
/// or
/// NSString *url = @"http:/xxx.yyy.zzz.com/aaa/bbb/cccc";

/// http headers
NSDictionary<NSString*, NSString*> *headers = @{"xxx" : @"yyy"};

/// request body / parameters
NSDictionary *params = @{@"param1" : @"value1", @"param2" : @"value2"};

/// here is http post method
RC_HTTPRequest *request = [RC_HTTPRequest rc_POSTRequestWithURL:url parameters:params headers:headers];

/// sending request
[RC_NetworkProxy rc_sendRequest:request completionHandler:^(RC_HTTPResponse * _Nonnull response) {
    
    /// default block is in multi-thread
    NSLog(@"\nmain-thread: %@\n", ([NSThread isMainThread] ? @"YES" : @"NO"));
    NSLog(@"%@", [response description]);
}];

3. 解析响应数据

3.1 全局响应数据解析器

在使用之前注册全局数据解析器。之后,数据解析器将为每个HTTP请求(除非为特定HTTP请求指定数据解析代理)(见第3.2节)。

//// sample code
[RC_NetworkProxy rc_registerResponseDataHandler:^RC_HTTPResponse * _Nonnull(id  _Nonnull data) {
    /// assume that the aim data structure is dictionary
    RC_JSON *json = [RC_JSON rc_JSONWithData:data];
    NSDictionary *dic = [json rc_JSONDictionary];
    NSString *code = [dic objectForKey:@"code"];
    NSString *msg = [dic objectForKey:@"message"];
    NSDictionary *result = [dic objectForKey:@"result"];

    RC_HTTPResponse *resp = [RC_HTTPResponse rc_responseWithMessage:msg code:code];
    /// success or failure , required
    resp.success = [code isEqualToString:@"200"];
   /// the data we want to process next
    resp.jsonResult = result;

    return resp;
}];

3.2 指定数据解析器

您可以使用“RC_HTTPResponseDataParser”解析特定请求的响应数据。它的优先级高于全局响应数据解析器

@protocol RC_HTTPResponseDataParser <NSObject>

@optional

- (RC_HTTPResponse *)rc_responseFromRawData:(id)rawData originalHTTPResponse:(nullable NSHTTPURLResponse *)httpResponse;

@end

/// --------

@interface RC_HTTPRequest : NSObject
...
@property (nonatomic, weak) id<RC_HTTPResponseDataParser> dataParser;
...
@end

4. 全局钩子

通过注册全局钩子,您可以在发送请求之前执行某些操作

4.1 注册全局钩子

/// sample code
[RC_NetworkProxy rc_registerRequestHookHandler:^(RC_HTTPRequest * _Nonnull request, RC_RequestHookCompletionHandler  _Nonnull completion) {

    /// e.g. we change the original 'request' instance
    RC_HTTPRequest *new_request = [xxx_obj makeSomeChange:request];
    if(completion){
        
        completion(new_request);
    }
}];

4.2 钩子白名单

@interface RC_HTTPRequest : NSObject
...
@property (nonatomic, assign, getter=isWhiteList) BOOL whiteList;
...
@end

注意:当属性‘whiteList’为‘YES’时,将忽略请求的预处理。

许可

RC_NetworkProxy 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。