Created
October 25, 2011 21:00
-
-
Save BlairDuncan/1314252 to your computer and use it in GitHub Desktop.
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
@implementation CPURL(add_targetAction) | |
// setters | |
- (void)setAction:(SEL)aAction | |
{ | |
[self setResourceValue:aAction forKey:"action"]; | |
} | |
- (void)setData:(id)aData | |
{ | |
[self setResourceValue:aData forKey:"data"]; | |
} | |
- (void)setErrorCheckAction:(SEL)aErrorCheckAction | |
{ | |
[self setResourceValue:aErrorCheckAction forKey:"errorCheck"]; | |
} | |
- (void)setStatus:(int)statusCode | |
{ | |
[self setResourceValue:statusCode forKey:"status"]; | |
} | |
- (void)setTarget:(id)aTarget | |
{ | |
[self setResourceValue:aTarget forKey:"target"]; | |
} | |
// getters | |
- (SEL)action | |
{ | |
return [self resourceValueForKey:"action"]; | |
} | |
- (id)data | |
{ | |
return [self resourceValueForKey:"data"]; | |
} | |
- (SEL)errorCheckAction | |
{ | |
return [self resourceValueForKey:"errorCheck"]; | |
} | |
- (int)status | |
{ | |
return [self resourceValueForKey:"status"]; | |
} | |
- (id)target | |
{ | |
return [self resourceValueForKey:"target"]; | |
} | |
+ (CPURL)URLWithString:(CPString)aString target:(id)aTarget action:(SEL)aAction | |
{ | |
return [self URLWithString:aString target:aTarget action:aAction errorCheckAction:nil]; | |
} | |
+ (CPURL)URLWithString:(CPString)aString target:(id)aTarget action:(SEL)aAction errorCheckAction:(SEL)aErrorCheckAction | |
{ | |
var aURL = [CPURL URLWithString:aString]; | |
[aURL setTarget:aTarget]; | |
[aURL setAction:aAction]; | |
[aURL setErrorCheckAction:aErrorCheckAction]; | |
return aURL; | |
} | |
- (void)postString:(CPString)content | |
{ | |
[self postString:content immediately:NO]; | |
} | |
- (void)postString:(CPString)content immediately:(BOOL)sendImmediately | |
{ | |
var request = [[CPURLRequest alloc] initWithURL:self]; | |
[request setHTTPBody:content]; | |
[request setValue:"text/plain;charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; | |
[request setHTTPMethod:@"POST"]; | |
if(sendImmediately) // CAREFUL - I only use this if the user quits and I need to write data before the app exits | |
[CPURLConnection sendSynchronousRequest:request returningResponse:nil]; | |
else | |
[CPURLConnection connectionWithRequest:request delegate:self]; | |
} | |
- (void)getRequest | |
{ | |
[self getRequest:NO callback:nil]; | |
} | |
- (void)getJSONPRequest | |
{ | |
[self getRequest:YES callback:"jsoncallback"]; | |
} | |
- (void)getJSONPRequestWithCallback:(CPString)aCallback | |
{ | |
[self getRequest:YES callback:aCallback]; | |
} | |
- (void)getRequest:(BOOL)isJSONP callback:(CPString)aCallback | |
{ | |
var request = [[CPURLRequest alloc] initWithURL:self]; | |
[request setHTTPMethod:@"GET"]; | |
if(isJSONP) | |
[CPJSONPConnection sendRequest:request callback:aCallback delegate:self]; | |
else | |
[CPURLConnection connectionWithRequest:request delegate:self]; | |
} | |
/////////////////////standard delegate methods (getRequest always sets the delegate to self) | |
- (void)connection:(CPURLConnection)aConnection didFailWithError:(id)error | |
{ | |
// if(CPLOG_ON) CPLog.info(_cmd + self + error); | |
} | |
- (void)connection:(CPURLConnection)aConnection didReceiveResponse:(CPHTTPURLResponse)response | |
{ | |
//// if(CPLOG_ON) CPLog.info(_cmd + self + response); | |
} | |
- (void)connection:(CPURLConnection)aConnection didReceiveData:(id)data | |
{ | |
// if(CPLOG_ON) CPLog.info(_cmd + self); | |
var request = aConnection._request, | |
url = [request URL], | |
theData; | |
if ([aConnection isKindOfClass:[CPJSONPConnection class]]) | |
{ | |
[url setStatus:CPNotFound]; // no status code returned for JSONP | |
if([self errorCheckAction]) | |
{ | |
[self setData:data]; | |
if([[self target] performSelector:[self errorCheckAction] withObject:url]) | |
return; | |
else | |
data = [self data]; | |
} | |
var theData = [CPData dataWithJSONObject:data]; | |
} | |
else | |
{ | |
[url setStatus:[aConnection _HTTPRequest].status()]; | |
if([self errorCheckAction]) | |
{ | |
[self setData:data]; | |
if([[self target] performSelector:[self errorCheckAction] withObject:url]) | |
return; | |
else | |
data = [self data]; | |
} | |
var theData = [CPData dataWithRawString:data]; | |
} | |
if([[self target] respondsToSelector:[self action]]) | |
[[self target] performSelector:[self action] withObject:theData]; | |
} | |
- (void)connectionDidFinishLoading:(CPURLConnection)aConnection | |
{ | |
// if(CPLOG_ON) CPLog.info(_cmd + self + aConnection); | |
} | |
- (void)connectionDidReceiveAuthenticationChallenge:(CPURLConnection)aConnection | |
{ | |
// if(CPLOG_ON) CPLog.info(_cmd + self + aConnection); | |
} | |
///////////////////// end default delegates | |
/////// utility function to get the value of a parameter in querry | |
- (CPString)getValueForQuerryParamName:(CPString)PName | |
{ | |
var querry = [self parameterString]; | |
if(querry) | |
{ | |
var params = querry.split("&"), | |
kvpDict = [CPDictionary dictionary], | |
iter = [params objectEnumerator], | |
obj; | |
while(obj = [iter nextObject]) | |
{ | |
var kvs = obj.split("="); | |
var tempDict = [CPDictionary dictionaryWithObject:kvs[1] forKey:kvs[0]]; | |
[kvpDict addEntriesFromDictionary:tempDict]; | |
} | |
return([kvpDict valueForKey:PName]); | |
} | |
else | |
return nil; | |
} | |
@end | |
/* USAGE | |
+ (CPURL)URLWithString:(CPString)aString target:(id)aTarget action:(SEL)aAction | |
+ (CPURL)URLWithString:(CPString)aString target:(id)aTarget action:(SEL)aAction errorCheckAction:(SEL)aErrorCheckAction | |
- (void)postString:(CPString)content | |
- (void)postString:(CPString)content immediately:(BOOL)sendImmediately | |
- (void)getRequest | |
- (void)getJSONPRequest | |
- (void)getJSONPRequestWithCallback:(CPString)aCallback // if the server requires a custom callback | |
/// EXAMPLES | |
// a get request with a target/action | |
var url = [CPURL URLWithString:@"theCompleteURL" target:self action:@selector(myAction:)]; | |
[url getRequest]; | |
- (void)myAction:(CPData)data | |
{ | |
// do something with the data | |
} | |
// a post requests are similar | |
var url = [CPURL URLWithString:@"theCompleteURL"]; // or pass a target,action / errorHandler | |
[url postString:@"SomeData"]; // usually a JSON string | |
// request with a target/action and a custom error checker | |
// the target/action will only be called if the error checker return YES | |
var url = [CPURL URLWithString:@"theCompleteURL" | |
target:self action:@selector(myAction:) | |
errorCheckAction:@selector(myErrorCheck:)]; | |
[url getRequest]; | |
- (BOOL)myErrorCheck:(id)theURL | |
{ | |
// an example error check | |
var theReturn = NO; | |
switch([theURL status]) | |
{ | |
case CPNotFound: // no status for JSONP requests | |
var data = [theURL data]; | |
if(data) | |
{ // this will be unique to your particular server response, | |
// in this case the servers returns JSON with either 0 for no error or error+message | |
switch(data.status) | |
{ | |
case "0": | |
theReturn = YES; | |
break; | |
case "error": | |
theReturn = YES; | |
[AppControllerSharedInstance showIndeterminateprogressMessage:""]; | |
[CPAlert alertWithMessage:@"Rdio connection error" info:data.message]; | |
break; | |
} | |
} | |
else | |
{ | |
// some kind of error here should we display a message | |
[AppControllerSharedInstance showIndeterminateprogressMessage:""]; | |
theReturn = YES; | |
} | |
break; | |
case 0: | |
case 404: | |
case 200: | |
break; | |
} | |
return theReturn; | |
} | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment