|
16 | 16 |
|
17 | 17 | #import "JSONAPI.h" |
18 | 18 |
|
| 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 | + |
19 | 26 | #pragma mark - static variables |
20 | 27 |
|
21 | 28 | static JSONAPI* sharedInstance = nil; |
@@ -73,29 +80,73 @@ +(void)postWithPath:(NSString*)path andParams:(NSDictionary*)params completion:( |
73 | 80 | }]; |
74 | 81 | } |
75 | 82 |
|
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 |
78 | 85 | { |
79 | | - if (!args) args = @[]; |
80 | | - |
81 | | - NSDictionary* jsonRequest = @{ |
82 | | - @"id": [NSNumber numberWithLong: ++jsonRpcId], |
83 | | - @"params": args, |
84 | | - @"method": method |
85 | | - }; |
86 | 86 |
|
87 | | - NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:jsonRequest |
| 87 | + NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:jsonObject |
88 | 88 | options:kNilOptions |
89 | 89 | error:nil]; |
90 | 90 | NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData encoding: NSUTF8StringEncoding]; |
91 | | - |
| 91 | + |
| 92 | + NSAssert(sharedInstance.baseURLString, @"API base URL not set"); |
92 | 93 | [JSONHTTPClient postJSONFromURLWithString: sharedInstance.baseURLString |
93 | 94 | bodyString: jsonRequestString |
94 | 95 | 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 | + } |
98 | 118 | }]; |
99 | 119 | } |
100 | 120 |
|
| 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 |
101 | 152 | @end |
0 commit comments