Skip to content

Commit 8f4fdc7

Browse files
committed
Added Cocoa style error handling to JSONAPI
1 parent 56ca6f8 commit 8f4fdc7

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎JSONModel/JSONModelNetworking/JSONAPI.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@
4848
/**
4949
* Makes a synchronious GET request to the API
5050
* @param params the variables to pass to the API
51+
* @param err pointer to an NSError object to pass back an error if needed
5152
* @return the JSON response as desrialized object
5253
*/
53-
+(id)getWithPath:(NSString*)path andParams:(NSDictionary*)params;
54+
+(id)getWithPath:(NSString*)path andParams:(NSDictionary*)params error:(NSError**)err;
5455

5556
/**
5657
* Makes an asynchronious GET request to the API
@@ -66,9 +67,10 @@
6667
/**
6768
* Makes a POST request to the API
6869
* @param params the variables to pass to the API
70+
* @param err pointer to an NSError object to pass back an error if needed
6971
* @return the JSON response as desrialized object
7072
*/
71-
+(id)postWithPath:(NSString*)path andParams:(NSDictionary*)params;
73+
+(id)postWithPath:(NSString*)path andParams:(NSDictionary*)params error:(NSError**)err;
7274

7375
/**
7476
* Makes a POST request to the API
@@ -84,9 +86,10 @@
8486
/**
8587
* Makes a synchronious JSON RPC request to the API
8688
* @param args the list of arguments to pass to the API
89+
* @param err pointer to an NSError object to pass back an error if needed
8790
* @return the JSON response as desrialized object
8891
*/
89-
+(id)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args;
92+
+(id)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args error:(NSError**)err;
9093

9194
/**
9295
* Makes an asynchronious JSON RPC request to the API

‎JSONModel/JSONModelNetworking/JSONAPI.m‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ +(void)setContentType:(NSString*)ctype
5757

5858
#pragma mark - GET methods
5959

60-
+(id)getWithPath:(NSString*)path andParams:(NSDictionary*)params
60+
+(id)getWithPath:(NSString*)path andParams:(NSDictionary*)params error:(NSError**)err
6161
{
6262
NSString* fullURL = [NSString stringWithFormat:@"%@%@", sharedInstance.baseURLString, path];
6363

64-
id json = [JSONHTTPClient getJSONFromURLWithString: fullURL params:params];
64+
id json = [JSONHTTPClient getJSONFromURLWithString: fullURL params:params error:err];
6565
return json;
6666
}
6767

@@ -76,11 +76,11 @@ +(void)getWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(J
7676

7777
#pragma mark - POST methods
7878

79-
+(id)postWithPath:(NSString*)path andParams:(NSDictionary*)params
79+
+(id)postWithPath:(NSString*)path andParams:(NSDictionary*)params error:(NSError**)err
8080
{
8181
NSString* fullURL = [NSString stringWithFormat:@"%@%@", sharedInstance.baseURLString, path];
8282

83-
id json = [JSONHTTPClient postJSONFromURLWithString: fullURL params:params];
83+
id json = [JSONHTTPClient postJSONFromURLWithString: fullURL params:params error:err];
8484
return json;
8585
}
8686

@@ -95,7 +95,7 @@ +(void)postWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(
9595

9696
#pragma mark - RPC 1.0 methods
9797

98-
+(id)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args
98+
+(id)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args error:(NSError**)err
9999
{
100100
if (!args) args = @[];
101101

@@ -111,7 +111,8 @@ +(id)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args
111111
NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData encoding: NSUTF8StringEncoding];
112112

113113
id json = [JSONHTTPClient postJSONFromURLWithString: sharedInstance.baseURLString
114-
bodyString: jsonRequestString];
114+
bodyString: jsonRequestString
115+
error: err];
115116
return json;
116117
}
117118

0 commit comments

Comments
 (0)