Skip to content

Instantly share code, notes, and snippets.

@kerin
Created December 6, 2012 11:32
Show Gist options
  • Save kerin/4223841 to your computer and use it in GitHub Desktop.
Save kerin/4223841 to your computer and use it in GitHub Desktop.
+ (void)makeAsyncRequestForPath:(NSString *)path
withQueryParams:(NSDictionary *)queryParams
responseHandler:(void (^)(NSData*, NSError*))handler
{
NSURLRequest *req = [self getRequestForPath:path
queryStringParams:queryParams];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:req
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
handler(data, error);
}];
}
+ (void)moviesByPage:(NSInteger)page
numPerPage:(NSInteger)perPage
withGenre:(NSString *)genre
forDelegate:(id<MLLanternAPIClientDelegate>)delegate{
NSString *path = [NSString stringWithFormat:@"movies/"];
NSMutableDictionary *queryparams = [[NSMutableDictionary alloc] init];
if(page){
[queryparams setObject:[NSNumber numberWithInt:page]
forKey:@"page"];
}
if(perPage){
[queryparams setObject:[NSNumber numberWithInt:perPage]
forKey:@"per_page"];
}
if (genre && ![genre isEqualToString:@""]) {
[queryparams setObject:genre forKey:@"genre"];
}
[self makeAsyncRequestForPath:path
withQueryParams:queryparams
responseHandler:^(NSData *data, NSError *error){
NSError *err = nil;
if ([data length] > 0 && error == nil) {
NSDictionary *apiMovies = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:&err];
NSMutableArray *movies = [[NSMutableArray alloc] init];
for (id movie in apiMovies[@"movies"]) {
[movies addObject:[[LanternMovie alloc] initWithDictionary:movie]];
}
[delegate moviesReceived:movies withPage:page withGenre:genre];
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment