Permalink
Browse files

Fixes to DIOSConnect for proper error handling

  • Loading branch information...
1 parent cf0ac53 commit 5053fd3f4b03eb3b021cd7258775d751df56bd69 @kylebrowning kylebrowning committed Oct 20, 2010
Showing with 61 additions and 46 deletions.
  1. +2 −0 DIOSConnect.h
  2. +59 −46 DIOSConnect.m
View
@@ -55,6 +55,7 @@
NSString *methodUrl;
NSString *responseStatusMessage;
NSString *requestMethod;
+ NSError *error;
}
@property (nonatomic, retain) NSDictionary *connResult;
@property (nonatomic, retain) NSString *sessid;
@@ -64,6 +65,7 @@
@property (nonatomic, retain) NSString *methodUrl;
@property (nonatomic, retain) NSString *responseStatusMessage;
@property (nonatomic, retain) NSString *requestMethod;
+@property (nonatomic, retain) NSError *error;
- (id) init;
- (void) initWithSessId:(NSString*)aSessId;
- (void) initWithUserInfo:(NSDictionary*)someUserInfo andSessId:(NSString*)sessId;
View
@@ -41,13 +41,14 @@
#import "ASIFormDataRequest.h"
@implementation DIOSConnect
-@synthesize connResult, sessid, method, params, userInfo, methodUrl, responseStatusMessage, requestMethod;
+@synthesize connResult, sessid, method, params, userInfo, methodUrl, responseStatusMessage, requestMethod, error;
/*
* This init function will automatically connect and setup the session for communicaiton with drupal
*/
- (id) init {
[super init];
+ error = nil;
isRunning = NO;
mainTimer = nil;
if(params == nil) {
@@ -68,6 +69,7 @@ - (id) initWithSession:(DIOSConnect*)aSession {
[self setUserInfo:[aSession userInfo]];
[self setSessid:[aSession sessid]];
}
+ error = nil;
isRunning = NO;
mainTimer = nil;
if(params == nil) {
@@ -151,21 +153,7 @@ -(NSString *) genRandStringLength {
//This runs our method and actually gets a response from drupal
-(void) runMethod {
- //Key Auth doesnt work in REST services
-// NSString *timestamp = [NSString stringWithFormat:@"%d", (long)[[NSDate date] timeIntervalSince1970]];
-// NSString *nonce = [self genRandStringLength];
-// //removed because we have to regen this every call
-// [self removeParam:@"hash"];
-// [self addParam:DRUPAL_DOMAIN forKey:@"domain_name"];
-// [self removeParam:@"domain_name"];
-// [self removeParam:@"domain_time_stamp"];
-// [self removeParam:@"nonce"];
-
-// NSString *hashParams = [NSString stringWithFormat:@"%@;%@;%@;%@",timestamp,DRUPAL_DOMAIN,nonce,[self method]];
-// [self addParam:[self generateHash:hashParams] forKey:@"hash"];
-// [self addParam:DRUPAL_DOMAIN forKey:@"domain_name"];
-// [self addParam:timestamp forKey:@"domain_time_stamp"];
-// [self addParam:nonce forKey:@"nonce"];
+ [self setError:nil];
[self removeParam:@"sessid"];
[self addParam:[self sessid] forKey:@"sessid"];
@@ -184,38 +172,63 @@ -(void) runMethod {
[requestBinary addRequestHeader:@"Content-Type" value:@"application/plist"];
[requestBinary addRequestHeader:@"Accept" value:@"application/plist"];
[requestBinary startSynchronous];
- NSError *error = [requestBinary error];
- NSData *response;
- if (!error) {
- response = [requestBinary responseData];
- }
- NSPropertyListFormat format;
- id plist;
responseStatusMessage = [requestBinary responseStatusMessage];
- if ([requestBinary responseStatusCode] == 200) {
- plist = [NSPropertyListSerialization propertyListFromData:response
- mutabilityOption:NSPropertyListMutableContainersAndLeaves
- format:&format
- errorDescription:&errorStr];
- [self setConnResult:plist];
- if([[self method] isEqualToString:@"system.connect"]) {
- if(plist != nil) {
- [self setSessid:[plist objectForKey:@"sessid"]];
- [self setUserInfo:[plist objectForKey:@"user"]];
- }
- }
- if([[self method] isEqualToString:@"user.login"]) {
- if(plist != nil) {
- [self setSessid:[plist objectForKey:@"sessid"]];
- [self setUserInfo:[plist objectForKey:@"user"]];
+ [self setError:[requestBinary error]];
+
+ if (!error) {
+ NSData *response = [requestBinary responseData];
+
+ NSPropertyListFormat format;
+ id plist = nil;
+
+ [self setResponseStatusMessage:[requestBinary responseStatusMessage]];
+
+ if(response != nil) {
+ plist = [NSPropertyListSerialization propertyListFromData:response
+ mutabilityOption:NSPropertyListMutableContainersAndLeaves
+ format:&format
+ errorDescription:&errorStr];
+ if (errorStr) {
+ NSError *e = [NSError errorWithDomain:@"DIOS-Error"
+ code:1
+ userInfo:[NSDictionary dictionaryWithObject:errorStr forKey:NSLocalizedDescriptionKey]];
+ [self setError:e];
}
- }
- } else {
- //Something WRONG happend
- }
- //Bug in ASIHTTPRequest, put here to stop activity indicator
- UIApplication* app = [UIApplication sharedApplication];
- app.networkActivityIndicatorVisible = NO;
+ } else {
+ NSError *e = [NSError errorWithDomain:@"DIOS-Error"
+ code:1
+ userInfo:[NSDictionary dictionaryWithObject:@"I couldnt get a response, is the site down?" forKey:NSLocalizedDescriptionKey]];
+ [self setError:e];
+ }
+
+
+ if (plist && !error) {
+ [self setConnResult:plist];
+ if([[self method] isEqualToString:@"system.connect"]) {
+ if(plist != nil) {
+ [self setSessid:[[plist objectForKey:@"#data"] objectForKey:@"sessid"]];
+ [self setUserInfo:[[plist objectForKey:@"#data"]objectForKey:@"user"]];
+ }
+ }
+ if([[self method] isEqualToString:@"user.login"]) {
+ if(plist != nil) {
+ [self setSessid:[[plist objectForKey:@"#data"] objectForKey:@"sessid"]];
+ [self setUserInfo:[[plist objectForKey:@"#data"]objectForKey:@"user"]];
+ }
+ }
+ if([[self method] isEqualToString:@"user.logout"]) {
+ if(plist != nil) {
+ [self setSessid:nil];
+ [self setUserInfo:nil];
+ }
+ }
+ }
+ }
+
+
+ //Bug in ASIHTTPRequest, put here to stop activity indicator
+ UIApplication* app = [UIApplication sharedApplication];
+ app.networkActivityIndicatorVisible = NO;
}
- (void) setMethod:(NSString *)aMethod {

0 comments on commit 5053fd3

Please sign in to comment.