Created
July 10, 2012 20:54
-
-
Save mlamina/3086177 to your computer and use it in GitHub Desktop.
Fuer meine iPad app hab ich mir mit Django nen backend geschrieben. Das hier ist eine abstrakte Klasse, die ich mir gebaut hab um das backend anzusprechen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Request.m | |
// IronMon | |
// | |
// Created by Marco Lamina on 29.09.11. | |
// Copyright 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "Request.h" | |
#import "ASIHTTPRequest.h" | |
#import "ASIFormDataRequest.h" | |
@implementation Request | |
@synthesize url, params, method, postParams, timeoutInSeconds; | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
adapter = [SBJsonStreamParserAdapter new]; | |
adapter.delegate = self; | |
parser = [SBJsonStreamParser new]; | |
parser.delegate = adapter; | |
parser.supportMultipleDocuments = YES; | |
self.method = @"GET"; | |
self.params = [NSMutableDictionary new]; | |
self.postParams = [NSMutableDictionary new]; | |
self.timeoutInSeconds = 0; | |
} | |
return self; | |
} | |
- (id) initWithUrl: (NSString*) requestUrl { | |
self = [self init]; | |
if (self) { | |
self.url = requestUrl; | |
} | |
return self; | |
} | |
- (void) fire: (NSObject<RequestDelegate>*) del { | |
// Fetch all available machines | |
delegate = [del retain]; | |
// Add parameters to URL | |
if ([self.params count]>0) { | |
self.url = [NSString stringWithFormat:@"%@?", self.url]; | |
for (NSString* key in [self.params allKeys]) { | |
NSString* value = @""; | |
if ([key isEqualToString:@"limit_attributes"]) | |
for (NSString* filter in ((NSArray*)[self.params objectForKey: key])) | |
value = [NSString stringWithFormat:@"%@,%@", value, filter]; | |
else | |
value = [self.params objectForKey:key]; | |
self.url = [NSString stringWithFormat:@"%@&%@=%@", self.url, key, value]; | |
} | |
} | |
NSURL *requestUrl = [NSURL URLWithString: self.url]; | |
if ([self.method isEqualToString:@"POST"]) { | |
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL: requestUrl]; | |
// Enable gzip compression | |
request.shouldWaitToInflateCompressedResponses = NO; | |
for (NSString* key in [self.postParams allKeys]) | |
[request setPostValue: [self.postParams valueForKey: key] forKey: key]; | |
[request setDelegate:self]; | |
[request setTimeOutSeconds: self.timeoutInSeconds]; | |
[request startAsynchronous]; | |
} else { | |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:requestUrl]; | |
// Enable gzip compression | |
request.shouldWaitToInflateCompressedResponses = NO; | |
[request setDelegate:self]; | |
[request setTimeOutSeconds: self.timeoutInSeconds]; | |
[request startAsynchronous]; | |
} | |
} | |
- (void)requestFinished:(ASIHTTPRequest *)request | |
{ | |
// Use when fetching text data | |
NSData *responseString = [((ASIFormDataRequest*)request) responseData]; | |
SBJsonStreamParserStatus status = [parser parse:responseString]; | |
if (status==SBJsonStreamParserError) { | |
// parser error | |
[delegate requestFailed: parser.error]; | |
[delegate release]; | |
} else if (status==SBJsonStreamParserWaitingForData) { | |
// waiting for data | |
} | |
} | |
- (void)requestFailed:(ASIHTTPRequest *)request | |
{ | |
NSError *error = [request error]; | |
NSLog(@"Request Error: %@: %@ ", [error localizedDescription], [error localizedFailureReason]); | |
[delegate requestFailed: [error localizedDescription]]; | |
[delegate release]; | |
} | |
#pragma mark SBJsonStreamParserAdapterDelegate methods | |
-(void)parser:(SBJsonStreamParser*)parser foundArray:(NSArray *)array { | |
NSArray* models = [self parseJSONArray: array]; | |
[delegate requestFinished: models]; | |
[delegate release]; | |
} | |
-(void)parser:(SBJsonStreamParser*)parser foundObject:(NSDictionary *)dict { | |
} | |
- (NSArray*) parseJSONArray: (NSArray*) array { | |
// Abstract method | |
@throw [NSException | |
exceptionWithName:NSInternalInconsistencyException | |
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] | |
userInfo:nil]; | |
} | |
- (void) limit: (int) num { | |
[self.params | |
setValue:[NSString stringWithFormat:@"%d", num] | |
forKey:@"limit"]; | |
} | |
- (void) latest: (int) num { | |
[self.params | |
setValue:[NSString stringWithFormat:@"%d", num] | |
forKey:@"latest"]; | |
} | |
- (void) addFilterForModel: (NSString*) model attribute: (NSString*) attribute { | |
if (![self.params objectForKey: @"limit_attributes"]) | |
[self.params setValue: [NSMutableArray new] forKey:@"limit_attributes"]; | |
NSMutableArray* filters = [self.params objectForKey:@"limit_attributes"]; | |
[filters addObject: [NSString stringWithFormat:@"%@__%@", model, attribute]]; | |
} | |
- (void) filterFrom: (NSDate*) startTime to: (NSDate*) endTime { | |
NSDateFormatter* formatter = [[NSDateFormatter new] autorelease]; | |
formatter.dateFormat = @"YYYY-MM-dd'T'HH:mm:ss"; | |
[self.params | |
setValue:[NSString stringWithFormat:@"%@,%@", [formatter stringFromDate: startTime], [formatter stringFromDate: endTime]] | |
forKey:@"date"]; | |
} | |
-(void) dealloc { | |
//[params release]; | |
[parser release]; | |
[adapter release]; | |
[self.method release]; | |
[self.params release]; | |
[self.url release]; | |
[self.postParams release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment