Skip to content

Instantly share code, notes, and snippets.

@steipete
Created October 24, 2010 15:38
Show Gist options
  • Save steipete/643615 to your computer and use it in GitHub Desktop.
Save steipete/643615 to your computer and use it in GitHub Desktop.
- (void)login {
// build request
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
#ifdef MOCK_HANDSHAKE
// create a nice mock
request = [OCMockObject niceMockForClass:[ASIHTTPRequest class]];
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"mock_handshake" ofType:@"txt"];
NSString *responseString = [NSString stringWithContentsOfFile:modelPath encoding:NSUTF8StringEncoding error:nil];
[[[(id)request stub] andReturn:responseString] responseString];
// keep the completion block upon setting
__block ASIHTTPRequestBlock requestBlock;
[[(id)request expect] setCompletionBlock:[OCMArg checkWithBlock:^(id value) { requestBlock = [value copy]; }]];
// start the delayed call response
[[[(id)request stub] andDo:^(NSInvocation *invocation) {
RunAfterDelay(3, ^{
requestBlock(request);
[requestBlock release];
});
}] startAsynchronous];
#endif
__block __typeof__(self) blockSelf = self;
[request setFailedBlock:^(ASIHTTPRequest *response){
DDLogError(@"login failed with error: %@", response.error);
NSError *error = [NSError errorWithDomain:TVApiErrorDomain code:TVApiLoginHandshakeError userInfo:[NSDictionary dictionaryWithObject:response.error forKey:@"Error"]];
[[NSNotificationCenter defaultCenter] postNotificationName:kTVLoginFailed object:error];
}];
[request setCompletionBlock:^(ASIHTTPRequest *response){
NSError *error;
NSString *responseString = [response responseString];
NSDictionary *handshakeJson = [responseString yajl_JSONWithOptions:YAJLParserOptionsNone error:&error];
// error handling removed...
[blockSelf.channelManager loadChannelFileVersion:handshakeChannelFileVersion];
}];
[request startAsynchronous];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment