TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | May 2016 |
Maintained by Cendy Wang.
DRDNetworking is a delightful networking library which provide you a convenient way to handle API request, it has no invasion to your project, you won't get into trouble when you remove it one day, even though that's not what I want to see. Currently, we are using AFNetworking 3.0.0+ with Session Manager.
DRDNetworking compatible with RESTFUL API
and JSON-RPC API
. If you needs to support with your own JSON Based RPC Call
, just a few lines of code needed!
More detail, please run the Example project in the repo.
To run the example project, clone the repo, and run pod install
from the Example directory first.
pod 'DRDNetworking
, then pod update
.#import "DRDNetworking.h"
DRDGeneralAPI *apiGet = [[DRDGeneralAPI alloc] init];
apiGet.baseUrl = @"http://ele.me";
apiGet.apiRequestMethodType = DRDRequestMethodTypeGET;
[apiGet setApiCompletionHandler:^(id responseObject, NSError * error) {
// Your handle code
}];
[apiGet start];
DRDAPIManager
manages DRDBaseAPI
object. You can send an API request or a batch of API requests. Furthermore, you can cancel an API request which you sent before.
DRDConfig
is a global class that help us to maintain the whole behaviors.
DRDConfig *networkConfig = [[DRDConfig alloc] init];
networkConfig.baseUrlStr = @"https://httpbin.org";
networkConfig.userAgent = @"For example User-Agent";
[[DRDAPIManager sharedDRDAPIManager] setConfiguration:networkConfig];
If you need to send a batch of api simultaneously, use DRDAPIBatchAPIRequests
DRDGeneralAPI *generalAPIGet = [[DRDGeneralAPI alloc] init];
generalAPIGet.apiRequestMethodType = DRDRequestMethodTypeGET;
generalAPIGet.baseUrl = self.baseURLStr;
DRDGeneralAPI *generalAPIPost = [[DRDGeneralAPI alloc] init];
generalAPIPost.apiRequestMethodType = DRDRequestMethodTypePOST;
generalAPIPost.baseUrl = self.baseURLStr;
DRDAPIBatchAPIRequests *batchRequests = [[DRDAPIBatchAPIRequests alloc] init];
[batchRequests addAPIRequest:generalAPIGet];
[batchRequests addAPIRequest:generalAPIPost];
[[DRDAPIManager sharedDRDAPIManager] sendBatchAPIRequests:batchRequests];
DRDBaseAPI
is base class of all API request object. You will customize your API request by subclass of it.
@implementation DRDAPIPostCall
#pragma mark - init
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
#pragma mark - DRD
- (NSString *)customRequestUrl {
return @"http://httpbin.org";
}
- (NSString *)requestMethod {
return nil;
}
- (id)requestParameters {
return nil;
}
- (DRDRequestMethodType)apiRequestMethodType {
return DRDRequestMethodTypePOST;
}
- (id)apiResponseObjReformer:(id)responseObject andError:(NSError *)error {
// refrom JSON response to your model object
return responseObject;
}
DRDGeneralAPI
avoid subclass of DRDBaseAPI
which will lead to class explosion. It provide you a convenient way to send API request.
DRDGeneralAPI *apiGeGet = [[DRDGeneralAPI alloc] initWithRequestMethod:@"get"];
apiGeGet.apiRequestMethodType = DRDRequestMethodTypeGET;
apiGeGet.apiRequestSerializerType = DRDRequestSerializerTypeHTTP;
apiGeGet.apiResponseSerializerType = DRDResponseSerializerTypeHTTP;
[apiGeGet setApiCompletionHandler:^(id responseObject, NSError * error) {
NSLog(@"responseObject is %@", responseObject);
if (error) {
NSLog(@"Error is %@", error.localizedDescription);
}
}];
[apiGeGet start];
DRDRPCProtocol
is the way that where you could implements your custome JSON Based RPC Protocol
. We already implemented JSON-RPC protocol
in the Example.
Review it, and write your own protocol.
DRDGeneralAPI *plusAPI = [[DRDGeneralAPI alloc]init];
plusAPI.baseUrl = @"http://www.raboof.com/projects/jayrock/demo.ashx?test";
plusAPI.requestMethod = @"add";
plusAPI.requestParameters = @{
@"a" : @(a),
@"b" : @(b)
};
plusAPI.rpcDelegate = [DRDJsonRpcVersionTwo sharedJsonRpcVersionTwo];
__weak typeof(self) weakSelf = self;
[plusAPI setApiCompletionHandler:^(NSNumber * responseObject, NSError * error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:[NSString stringWithFormat:@"%@", error.localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil]show];
} else {
weakSelf.labelResult.text = [NSString stringWithFormat:@"%@", responseObject];
}
}];
[plusAPI start];
DRDNetworking
提供了一个方便实现API
调用的网络库。
目前,内部使用AFNetworking 3.0.0+
来简化JSON
、XML
等网络序列化工作。
它有以下优势:
API
设计RESTFUL
, JSON-RPC
及自定义RPC
等通讯协议扩展HTTP /2
(iOS 9.0+
)HTTP 1.1
下,TCP/IP
连接复用,优化网络连接BaseAPI
,减少ViewController
层的代码量您可以使用CocoaPods来集成DRDNetworking
.
在您的Podfile
里添加以下代码即可集成DRDNetworking
:
pod "DRDNetworking"
我们提供了一个示例项目来帮助您更好地了解和使用DRDNetworking
。
clone
下来代码,Scheme
选择 DRDNetworking-Example
即可运行。
podfile
中,添加pod "DRDNetworking"
,然后 pod update
DRDNetworking
的地方,添加#import "DRDNetworking.h"
API
调用:DRDGeneralAPI *apiGet = [[DRDGeneralAPI alloc] init];
apiGet.baseUrl = @"http://ele.me";
apiGet.apiRequestMethodType = DRDRequestMethodTypeGET;
[apiGet setApiCompletionHandler:^(id responseObject, NSError * error) {
// Your handle code
}];
[apiGet start];
更多用法,可以参考代码中.h
文件。您是幸运的,目前所有文档都以中文撰写。
cendywang, [email protected]
Alex Ao, [email protected]
DRDNetworking is available under the MIT license. See the LICENSE file for more info.