Skip to content

Commit 8d5b696

Browse files
committed
+ added json rpc 2.0 calls
1 parent 8766ab0 commit 8d5b696

2 files changed

Lines changed: 77 additions & 16 deletions

File tree

‎JSONModel/JSONModelNetworking/JSONAPI.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,25 @@
6666

6767
/////////////////////////////////////////////////////////////////////////////////////////////
6868

69-
/** @name JSON RPC (1.0) request method */
69+
/** @name JSON RPC methods */
7070
/**
71-
* Makes an asynchronious JSON RPC request to the API
71+
* Makes an asynchronious JSON RPC request to the API. Read more: http://www.jsonrpc.org
7272
* @param method the HTTP method name; GET or POST only
7373
* @param args the list of arguments to pass to the API
7474
* @param completeBlock JSONObjectBlock to execute upon completion
7575
*/
7676
+(void)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args completion:(JSONObjectBlock)completeBlock;
7777

78+
/** @name JSON RPC (2.0) request method */
79+
/**
80+
* Makes an asynchronious JSON RPC 2.0 request to the API. Read more: http://www.jsonrpc.org
81+
* @param method the HTTP method name; GET or POST only
82+
* @param params the params to pass to the API - an NSArray or an NSDictionary,
83+
* depending whether you're using named or unnamed parameters
84+
* @param completeBlock JSONObjectBlock to execute upon completion
85+
*/
86+
+(void)rpc2WithMethodName:(NSString*)method andParams:(id)params completion:(JSONObjectBlock)completeBlock;
87+
7888
/////////////////////////////////////////////////////////////////////////////////////////////
7989

8090
@end

‎JSONModel/JSONModelNetworking/JSONAPI.m‎

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
#import "JSONAPI.h"
1818

19+
#pragma mark - helper error model class
20+
@interface JSONAPIRPCErrorModel: JSONModel
21+
@property (assign, nonatomic) int code;
22+
@property (strong, nonatomic) NSString* message;
23+
@property (strong, nonatomic) id<Optional> data;
24+
@end
25+
1926
#pragma mark - static variables
2027

2128
static JSONAPI* sharedInstance = nil;
@@ -73,29 +80,73 @@ +(void)postWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(
7380
}];
7481
}
7582

76-
#pragma mark - RPC 1.0 methods
77-
+(void)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args completion:(JSONObjectBlock)completeBlock
83+
#pragma mark - RPC methods
84+
+(void)__rpcRequestWithObject:(id)jsonObject completion:(JSONObjectBlock)completeBlock
7885
{
79-
if (!args) args = @[];
80-
81-
NSDictionary* jsonRequest = @{
82-
@"id": [NSNumber numberWithLong: ++jsonRpcId],
83-
@"params": args,
84-
@"method": method
85-
};
8686

87-
NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:jsonRequest
87+
NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:jsonObject
8888
options:kNilOptions
8989
error:nil];
9090
NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData encoding: NSUTF8StringEncoding];
91-
91+
92+
NSAssert(sharedInstance.baseURLString, @"API base URL not set");
9293
[JSONHTTPClient postJSONFromURLWithString: sharedInstance.baseURLString
9394
bodyString: jsonRequestString
9495
completion:^(NSDictionary *json, JSONModelError* e) {
95-
//
96-
NSDictionary* result = json[@"result"];
97-
completeBlock(result, e);
96+
97+
if (completeBlock) {
98+
//handle the rpc response
99+
NSDictionary* result = json[@"result"];
100+
101+
if (!result) {
102+
JSONAPIRPCErrorModel* error = [[JSONAPIRPCErrorModel alloc] initWithDictionary:json[@"error"] error:nil];
103+
if (error) {
104+
//custom server error
105+
if (!error.message) error.message = @"Generic json rpc error";
106+
e = [JSONModelError errorWithDomain:JSONModelErrorDomain
107+
code:error.code
108+
userInfo: @{ NSLocalizedDescriptionKey : error.message}];
109+
} else {
110+
//generic error
111+
e = [JSONModelError errorBadResponse];
112+
}
113+
}
114+
115+
//invoke the callback
116+
completeBlock(result, e);
117+
}
98118
}];
99119
}
100120

121+
+(void)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args completion:(JSONObjectBlock)completeBlock
122+
{
123+
NSAssert(method, @"No method specified");
124+
if (!args) args = @[];
125+
126+
[self __rpcRequestWithObject:@{
127+
//rpc 1.0
128+
@"id": [NSNumber numberWithLong: ++jsonRpcId],
129+
@"params": args,
130+
@"method": method
131+
} completion:completeBlock];
132+
}
133+
134+
+(void)rpc2WithMethodName:(NSString*)method andParams:(id)params completion:(JSONObjectBlock)completeBlock
135+
{
136+
NSAssert(method, @"No method specified");
137+
if (!params) params = @[];
138+
139+
[self __rpcRequestWithObject:@{
140+
//rpc 2.0
141+
@"jsonrpc": @"2.0",
142+
@"id": [NSNumber numberWithLong: ++jsonRpcId],
143+
@"params": params,
144+
@"method": method
145+
} completion:completeBlock];
146+
}
147+
148+
@end
149+
150+
#pragma - helper rpc error model class implementation
151+
@implementation JSONAPIRPCErrorModel
101152
@end

0 commit comments

Comments
 (0)