- (IBAction)fetchBooks:(id)sender
{
NSLog(@"fetchBooks");
//show user something is happending
[progress startAnimation:nil];
//get the value we are looking for
NSString *input = [searchField stringValue];
//escaping
NSString *searchString = [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"searchString = %@",searchString);
//create the URL
NSString *urlString =[NSString stringWithFormat:
@"http://ecs.amazonaws.com/onca/xml?"
@"Service=AWSECommerceService&"
@"AWSAccessKeyId=%@&"
@"Operation=ItemSearch&"
@"SearchIndex=Books&"
@"Keywords=%@&"
@"Version=2007-07-16",
AWS_ID,searchString];
NSLog(@"url - %@",urlString);
NSString *signedURLString = [HMACSHA256 getSignedRequest:urlString withSecret:AWS_SECRET];
NSLog(@"signed url %@",signedURLString);
NSURL *url = [NSURL URLWithString:signedURLString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
//fetch the response
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
//check for errors in repsonse
if(!urlData){
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
return;
}
//parse the response
[doc release];
doc = [[NSXMLDocument alloc] initWithData:urlData options:0 error:&error];
NSLog(@"doc = %@",doc);
if(!doc){
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
return;
}
[itemNodes release];
itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:&error] retain];
if(!itemNodes){
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
return;
}
//update interface
[tableView reloadData];
[progress stopAnimation:nil];
}