This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Examples
brunodecarvalho edited this page Mar 31, 2013
·
3 revisions
This page contains examples for some of the most common use cases.
[[[BBHTTPRequest readResource:@"http://foo.bar/baz.json"] asJSON] execute:^(BBHTTPResponse* r) {
NSLog(@"User email: %@", r.content[@"user.email"]);
NSLog(@"# of followers: %@", r.content[@"user.followers.@count"]);
} error:nil];
[[BBHTTPRequest readResource:@"http://foo.bar/baz.png"] asImage] execute:^(BBHTTPResponse* response) {
UIImage* image = response.content; // NSImage on OSX
NSLog(@"image size: %@", NSStringFromCGSize(image.size));
} error:nil];
[[[BBHTTPRequest readResource:@"http://foo.bar/baz.json"] asString] execute:^(BBHTTPResponse* r) {
NSLog(@"%@", r.content);
} error:nil];
[[BBHTTPRequest createResource:@"http://foo.bar/baz" withContentsOfFile:@"/path/to/file"]
execute:^(BBHTTPResponse* response) {
// handle response
} error:nil];
[[BBHTTPRequest readResource:@"http://foo.bar/baz"] setup:^(BBHTTPRequest* request) {
[request downloadToFile:@"/path/to/file"];
} execute:^(BBHTTPResponse* response) {
// handle response
} error:nil];
[[BBHTTPRequest readResource:@"http://foo.bar/baz/1"] setup:^(id request) {
// Prepare request...
} execute:^(BBHTTPResponse* response) {
// Handle response...
} error:^(NSError* error) {
// Handle error...
} finally:^{
// Do after error OR success.
}];
All requests are executed by a BBHTTPExecutor
. If you don't specify one, these requests go on the singleton executor. If you want to control exactly how your requests behave, there's a lower level API for that too:
BBHTTPExecutor* twitterExecutor = [BBHTTPExecutor initWithId:@"twitter.com"];
BBHTTPExecutor* facebookExecutor = [BBHTTPExecutor initWithId:@"facebook.com"];
twitterExecutor.maxParallelRequests = 10;
facebookExecutor.maxParallelRequests = 2;
...
BBHTTPRequest* request = [[BBHTTPRequest alloc]
initWithURL:[NSURL URLWithString:@"http://twitter.com/resource"]
andVerb:@"GET"];
request[@"Accept-Language"] = @"en-us";
request.downloadProgressBlock = ^(NSUInteger current, NSUInteger total) { /* ... */ };
// The finish block will be called when the request ends successfully, with error or cancelled
request.finishBlock = ^(BBHTTPRequest* request) {
if ([request wasCancelled]) {
// Handle request cancellation, only happens when you call the 'cancel' method
} else if ([request hasSuccessfulResponse]) {
// Handle completed request with success response from server
} else if ([request wasSuccessfullyExecuted]) {
// Handle completed request with error response from server
} else {
// Some other error occurred which prevented the request from terminating
// The 'error' read-only property will be set
}
};
[twitterExecutor executeRequest:request];