Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Return FBRequest* from request: methods.
The request: selectors in the Facebook class now return a pointer to the
instantiated FBRequest object (rather than void). This makes it easier
to know in the delegate callback which request the callback maps to.
  • Loading branch information
Jim Brusstar committed Jan 31, 2011
1 parent 8b3c17d commit 479f284
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 65 deletions.
29 changes: 16 additions & 13 deletions sample/DemoApp/Classes/DemoAppViewController.m
Expand Up @@ -125,10 +125,10 @@ - (IBAction)getPublicInfo:(id)sender {
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"SELECT uid,name FROM user WHERE uid=4", @"query",
nil];
[_facebook requestWithMethodName: @"fql.query"
andParams: params
andHttpMethod: @"POST"
andDelegate: self];
[_facebook requestWithMethodName:@"fql.query"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
}

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ - (void)fbDidLogout {
}


///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// FBRequestDelegate

/**
Expand All @@ -232,14 +232,16 @@ - (void)fbDidLogout {
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"received response");
};
}

/**
* Called when a request returns and its response has been parsed into an object.
* The resulting object may be a dictionary, an array, a string, or a number, depending
* on the format of the API response.
* If you need access to the raw response, use
* (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response.
* Called when a request returns and its response has been parsed into
* an object. The resulting object may be a dictionary, an array, a string,
* or a number, depending on the format of the API response. If you need access
* to the raw response, use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
*/
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
Expand All @@ -253,14 +255,15 @@ - (void)request:(FBRequest *)request didLoad:(id)result {
};

/**
* Called when an error prevents the Facebook API request from completing successfully.
* Called when an error prevents the Facebook API request from completing
* successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
[self.label setText:[error localizedDescription]];
};


///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate

/**
Expand Down
12 changes: 5 additions & 7 deletions src/FBRequest.h
Expand Up @@ -51,10 +51,7 @@
* standard Objective-C object-to-string conversion facilities.
*/
@property(nonatomic,retain) NSMutableDictionary* params;


@property(nonatomic,assign) NSURLConnection* connection;

@property(nonatomic,assign) NSMutableData* responseText;


Expand All @@ -75,7 +72,7 @@

@end

///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*
*Your application should implement this delegate
Expand All @@ -100,10 +97,11 @@
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error;

/**
* Called when a request returns and its response has been parsed into an object.
* Called when a request returns and its response has been parsed into
* an object.
*
* The resulting object may be a dictionary, an array, a string, or a number, depending
* on thee format of the API response.
* The resulting object may be a dictionary, an array, a string, or a number,
* depending on thee format of the API response.
*/
- (void)request:(FBRequest *)request didLoad:(id)result;

Expand Down
17 changes: 12 additions & 5 deletions src/FBRequest.m
Expand Up @@ -44,6 +44,7 @@ + (FBRequest *)getRequestWithParams:(NSMutableDictionary *) params
httpMethod:(NSString *) httpMethod
delegate:(id<FBRequestDelegate>) delegate
requestURL:(NSString *) url {

FBRequest* request = [[[FBRequest alloc] init] autorelease];
request.delegate = delegate;
request.url = url;
Expand Down Expand Up @@ -229,7 +230,8 @@ - (id)parseJsonResponse:(NSData *)data error:(NSError **)error {
}

/*
* private helper function: call the delegate function when the request fail with Error
* private helper function: call the delegate function when the request
* fails with error
*/
- (void)failWithError:(NSError *)error {
if ([_delegate respondsToSelector:@selector(request:didFailWithError:)]) {
Expand All @@ -241,17 +243,21 @@ - (void)failWithError:(NSError *)error {
* private helper function: handle the response data
*/
- (void)handleResponseData:(NSData *)data {
if ([_delegate respondsToSelector:@selector(request:didLoadRawResponse:)]) {
if ([_delegate respondsToSelector:
@selector(request:didLoadRawResponse:)]) {
[_delegate request:self didLoadRawResponse:data];
}

if ([_delegate respondsToSelector:@selector(request:didLoad:)] ||
[_delegate respondsToSelector:@selector(request:didFailWithError:)]) {
[_delegate respondsToSelector:
@selector(request:didFailWithError:)]) {
NSError* error = nil;
id result = [self parseJsonResponse:data error:&error];

if (error) {
[self failWithError:error];
} else if ([_delegate respondsToSelector:@selector(request:didLoad:)]) {
} else if ([_delegate respondsToSelector:
@selector(request:didLoad:)]) {
[_delegate request:self didLoad:(result == nil ? data : result)];
}

Expand Down Expand Up @@ -321,7 +327,8 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
_responseText = [[NSMutableData alloc] init];

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if ([_delegate respondsToSelector:@selector(request:didReceiveResponse:)]) {
if ([_delegate respondsToSelector:
@selector(request:didReceiveResponse:)]) {
[_delegate request:self didReceiveResponse:httpResponse];
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/Facebook.h
Expand Up @@ -51,25 +51,25 @@

- (void)logout:(id<FBSessionDelegate>)delegate;

- (void)requestWithParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate;
- (FBRequest*)requestWithParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate;

- (void)requestWithMethodName:(NSString *)methodName
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate;
- (FBRequest*)requestWithMethodName:(NSString *)methodName
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate;

- (void)requestWithGraphPath:(NSString *)graphPath
andDelegate:(id <FBRequestDelegate>)delegate;
- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
andDelegate:(id <FBRequestDelegate>)delegate;

- (void)requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate;
- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate;

- (void)requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate;
- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate;

- (void)dialog:(NSString *)action
andDelegate:(id<FBDialogDelegate>)delegate;
Expand Down
70 changes: 45 additions & 25 deletions src/Facebook.m
Expand Up @@ -80,10 +80,11 @@ - (void)dealloc {
* Callback interface for notifying the calling application when
* the request has received response
*/
- (void)openUrl:(NSString *)url
params:(NSMutableDictionary *)params
httpMethod:(NSString *)httpMethod
delegate:(id<FBRequestDelegate>)delegate {
- (FBRequest*)openUrl:(NSString *)url
params:(NSMutableDictionary *)params
httpMethod:(NSString *)httpMethod
delegate:(id<FBRequestDelegate>)delegate {

[params setValue:@"json" forKey:@"format"];
[params setValue:kSDK forKey:@"sdk"];
[params setValue:kSDKVersion forKey:@"sdk_version"];
Expand All @@ -97,6 +98,7 @@ - (void)openUrl:(NSString *)url
delegate:delegate
requestURL:url] retain];
[_request connect];
return _request;
}

/**
Expand Down Expand Up @@ -353,21 +355,23 @@ - (void)logout:(id<FBSessionDelegate>)delegate {
* @param delegate
* Callback interface for notifying the calling application when
* the request has received response
* @return FBRequest*
* Returns a pointer to the FBRequest object.
*/
- (void)requestWithParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate {
- (FBRequest*)requestWithParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate {
if ([params objectForKey:@"method"] == nil) {
NSLog(@"API Method must be specified");
return;
return nil;
}

NSString * methodName = [params objectForKey:@"method"];
[params removeObjectForKey:@"method"];

[self requestWithMethodName:methodName
andParams:params
andHttpMethod:@"GET"
andDelegate:delegate];
return [self requestWithMethodName:methodName
andParams:params
andHttpMethod:@"GET"
andDelegate:delegate];
}

/**
Expand All @@ -389,13 +393,18 @@ - (void)requestWithParams:(NSMutableDictionary *)params
* @param delegate
* Callback interface for notifying the calling application when
* the request has received response
* @return FBRequest*
* Returns a pointer to the FBRequest object.
*/
- (void)requestWithMethodName:(NSString *)methodName
- (FBRequest*)requestWithMethodName:(NSString *)methodName
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate {
NSString * fullURL = [kRestserverBaseURL stringByAppendingString:methodName];
[self openUrl:fullURL params:params httpMethod:httpMethod delegate:delegate];
return [self openUrl:fullURL
params:params
httpMethod:httpMethod
delegate:delegate];
}

/**
Expand All @@ -410,14 +419,16 @@ - (void)requestWithMethodName:(NSString *)methodName
* @param delegate
* Callback interface for notifying the calling application when
* the request has received response
* @return FBRequest*
* Returns a pointer to the FBRequest object.
*/
- (void)requestWithGraphPath:(NSString *)graphPath
- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
andDelegate:(id <FBRequestDelegate>)delegate {

[self requestWithGraphPath:graphPath
andParams:[NSMutableDictionary dictionary]
andHttpMethod:@"GET"
andDelegate:delegate];
return [self requestWithGraphPath:graphPath
andParams:[NSMutableDictionary dictionary]
andHttpMethod:@"GET"
andDelegate:delegate];
}

/**
Expand All @@ -439,14 +450,17 @@ - (void)requestWithGraphPath:(NSString *)graphPath
* @param delegate
* Callback interface for notifying the calling application when
* the request has received response
* @return FBRequest*
* Returns a pointer to the FBRequest object.
*/
- (void)requestWithGraphPath:(NSString *)graphPath
- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *)params
andDelegate:(id <FBRequestDelegate>)delegate {
[self requestWithGraphPath:graphPath
andParams:params
andHttpMethod:@"GET"
andDelegate:delegate];

return [self requestWithGraphPath:graphPath
andParams:params
andHttpMethod:@"GET"
andDelegate:delegate];
}

/**
Expand Down Expand Up @@ -475,13 +489,19 @@ - (void)requestWithGraphPath:(NSString *)graphPath
* @param delegate
* Callback interface for notifying the calling application when
* the request has received response
* @return FBRequest*
* Returns a pointer to the FBRequest object.
*/
- (void)requestWithGraphPath:(NSString *)graphPath
- (FBRequest*)requestWithGraphPath:(NSString *)graphPath
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate {

NSString * fullURL = [kGraphBaseURL stringByAppendingString:graphPath];
[self openUrl:fullURL params:params httpMethod:httpMethod delegate:delegate];
return [self openUrl:fullURL
params:params
httpMethod:httpMethod
delegate:delegate];
}

/**
Expand Down

0 comments on commit 479f284

Please sign in to comment.