Skip to content

Commit

Permalink
[On #25]done search with ISBN
Browse files Browse the repository at this point in the history
  • Loading branch information
aquarhead committed Oct 19, 2013
1 parent 4c614c5 commit 64803d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Shoka/API/ShokaDoubanAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

@interface ShokaDoubanAPI : NSObject

+ (void)searchBookWithISBN:(NSString *)isbn success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure;

@end
16 changes: 16 additions & 0 deletions Shoka/API/ShokaDoubanAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@
//

#import "ShokaDoubanAPI.h"
#import <AFNetworking.h>

@implementation ShokaDoubanAPI

#define API_TIMEOUT 5

+ (void)searchBookWithISBN:(NSString *)isbn success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure
{
NSString *requestString = [[NSString stringWithFormat:@"http://api.douban.com/v2/book/isbn/%@", isbn] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperation *searchOP = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:API_TIMEOUT]];
searchOP.responseSerializer = [AFJSONResponseSerializer serializer];
[searchOP setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure([NSError errorWithDomain:@"DoubanAPI" code:4 userInfo:@{@"status": @"failure while searching book with isbn"}]);
}];
[searchOP start];
}

@end
9 changes: 8 additions & 1 deletion Shoka/ShokaBookDetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "ShokaWebpacAPI.h"
#import "ShokaItemTableViewCell.h"
#import "ShokaFavorites.h"
#import "ShokaDoubanAPI.h"

#define kShokaBackIconName @"back"
#define kShokaFavorite0IconName @"favorite-0"
Expand Down Expand Up @@ -80,7 +81,13 @@ - (void)viewDidLoad
self.result = api_result;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:itemsSection] withRowAnimation:UITableViewRowAnimationAutomatic];
} failure:^(NSError *err) {
NSLog(@"Failed fetch item data.");
NSLog(@"%@", err.userInfo[@"status"]);
}];

[ShokaDoubanAPI searchBookWithISBN:self.book.ISBN success:^(NSDictionary *doubanInfo) {
NSLog(@"%@", doubanInfo);
} failure:^(NSError *err) {
NSLog(@"%@", err.userInfo[@"status"]);
}];
}

Expand Down

0 comments on commit 64803d5

Please sign in to comment.