Skip to content

Commit

Permalink
Refactors statics on FBRequest and FBRequestConnection
Browse files Browse the repository at this point in the history
Summary:
Moves some statics around and adds a few helpers, in order to make
FBRequest and FBRequestConnection more cohesive and helpful.

Test Plan: updated samples, built, ran related methods

Reviewers: clang, ekoneil, mmarucheck, gregschechte, caabernathy

Reviewed By: clang

CC: msdkexp@, platform-diffs@lists

Differential Revision: https://phabricator.fb.com/D530267

Task ID: 1235919
  • Loading branch information
Jason Clark committed Jul 25, 2012
1 parent 9dbac0a commit 67ce4ba
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 212 deletions.
66 changes: 33 additions & 33 deletions samples/BooleanOGSample/BooleanOGSample/BOGFirstViewController.m
Expand Up @@ -158,39 +158,39 @@ - (void)postAction:(NSString *)actionPath


// post the action using one of the lightweight static start* methods on FBRequest
[FBRequest startForPostWithGraphPath:actionPath
graphObject:action
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// successful post, in the sample we do nothing with the id, however
// a more complex applicaiton may want to store or perform addtional actions
// with the id that represents the just-posted action
} else {
// get the basic error message
NSString *message = error.localizedDescription;

// see if we can improve on it with an error message from the server
id json = [error.userInfo objectForKey:FBErrorParsedJSONResponseKey];
if ([json isKindOfClass:[NSArray class]] &&
(json = [json objectAtIndex:0]) &&
[json isKindOfClass:[NSDictionary class]] &&
(json = [json objectForKey:@"body"]) &&
[json isKindOfClass:[NSDictionary class]] &&
(json = [json objectForKey:@"error"]) &&
[json isKindOfClass:[NSDictionary class]] &&
(json = [json objectForKey:@"message"])) {
message = [json description];
}

// display the message that we have
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OG Post Failed"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
[FBRequestConnection startForPostWithGraphPath:actionPath
graphObject:action
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// successful post, in the sample we do nothing with the id, however
// a more complex applicaiton may want to store or perform addtional actions
// with the id that represents the just-posted action
} else {
// get the basic error message
NSString *message = error.localizedDescription;
// see if we can improve on it with an error message from the server
id json = [error.userInfo objectForKey:FBErrorParsedJSONResponseKey];
if ([json isKindOfClass:[NSArray class]] &&
(json = [json objectAtIndex:0]) &&
[json isKindOfClass:[NSDictionary class]] &&
(json = [json objectForKey:@"body"]) &&
[json isKindOfClass:[NSDictionary class]] &&
(json = [json objectForKey:@"error"]) &&
[json isKindOfClass:[NSDictionary class]] &&
(json = [json objectForKey:@"message"])) {
message = [json description];
}
// display the message that we have
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OG Post Failed"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
}
}

Expand Down
17 changes: 6 additions & 11 deletions samples/HelloFacebookSample/HelloFacebookSample/HFViewController.m
Expand Up @@ -115,18 +115,13 @@ - (IBAction)postStatusUpdateClick:(UIButton *)sender {

NSString *message = [NSString stringWithFormat:@"Updating %@'s status at %@",
self.loggedInUser.first_name, [NSDate date]];
NSDictionary *params = [NSDictionary dictionaryWithObject:message forKey:@"message"];

// use the "startWith" helper static on FBRequest to both create and start a request, with
// a specified completion handler.
[FBRequest startWithGraphPath:@"me/feed"
parameters:params
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

[self showAlert:message result:result error:error];
self.buttonPostStatus.enabled = YES;
}];
[FBRequestConnection startForPostStatusUpdate:message
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

[self showAlert:message result:result error:error];
self.buttonPostStatus.enabled = YES;
}];

self.buttonPostStatus.enabled = NO;
}
Expand Down
45 changes: 23 additions & 22 deletions samples/Scrumptious/scrumptious/SCViewController.m
Expand Up @@ -142,28 +142,29 @@ - (void)postOpenGraphActionWithPhotoURL:(NSString *)photoURL {
}

// Create the request and post the action to the "me/fb_sample_scrumps:eat" path.
[FBRequest startForPostWithGraphPath:@"me/fb_sample_scrumps:eat"
graphObject:action
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
[self.activityIndicator stopAnimating];
[self.view setUserInteractionEnabled:YES];

NSString *alertText;
if (!error) {
alertText = [NSString stringWithFormat:@"Posted Open Graph action, id: %@",
[result objectForKey:@"id"]];
} else {
alertText = [NSString stringWithFormat:@"error: domain = %@, code = %d",
error.domain, error.code];
}
[[[UIAlertView alloc] initWithTitle:@"Result"
message:alertText
delegate:nil
cancelButtonTitle:@"Thanks!"
otherButtonTitles:nil]
show];
}];
[FBRequestConnection startForPostWithGraphPath:@"me/fb_sample_scrumps:eat"
graphObject:action
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
[self.activityIndicator stopAnimating];
[self.view setUserInteractionEnabled:YES];

NSString *alertText;
if (!error) {
alertText = [NSString stringWithFormat:@"Posted Open Graph action, id: %@",
[result objectForKey:@"id"]];
} else {
alertText = [NSString stringWithFormat:@"error: domain = %@, code = %d",
error.domain, error.code];
}
[[[UIAlertView alloc] initWithTitle:@"Result"
message:alertText
delegate:nil
cancelButtonTitle:@"Thanks!"
otherButtonTitles:nil]
show];
}];
}

// FBSample logic
Expand Down
7 changes: 6 additions & 1 deletion scripts/build_framework.sh
Expand Up @@ -113,12 +113,17 @@ mkdir $FB_SDK_FRAMEWORK/Versions/A/Resources
$FB_SDK_BUILD/${BUILDCONFIGURATION}-iphoneos/facebook-ios-sdk/*.h \
$FB_SDK_FRAMEWORK/Versions/A/Headers \
|| die "Error building framework while copying SDK headers"
\cp \
$FB_SDK_BUILD/${BUILDCONFIGURATION}-iphoneos/facebook-ios-sdk/*.h \
$FB_SDK_FRAMEWORK/Versions/A/DeprecatedHeaders \
|| die "Error building framework while copying SDK headers to deprecated folder"
for HEADER in FBConnect.h \
FBDialog.h \
FBFrictionlessRequestSettings.h \
FBLoginDialog.h \
Facebook.h \
FBRequest.h \
Facebook.h
FBSessionManualTokenCachingStrategy.h
do
\cp \
$FB_SDK_SRC/$HEADER \
Expand Down
150 changes: 83 additions & 67 deletions src/FBRequest.h
Expand Up @@ -293,65 +293,6 @@ typedef NSUInteger FBRequestState __attribute__((deprecated));
connection.
*/

/*!
@method
@abstract
Creates an `FBRequest` object for a Graph API call, instantiate an
<FBRequestConnection> object, add the request to the newly created
connection and finally start the connection. The request uses
the active session represented by `[FBSession activeSession]`.
See <connectionWithSession:graphPath:parameters:HTTPMethod:completionHandler:>
@param graphPath The Graph API endpoint to use for the request, for example "me".
@param handler The handler block to call when the request completes with a success, error, or cancel action.
*/
+ (FBRequestConnection*)startWithGraphPath:(NSString*)graphPath
completionHandler:(FBRequestHandler)handler;

/*!
@method
@abstract
Creates an `FBRequest` object for an HTTP POST Graph API call, instantiate
an <FBRequestConnection> object, add the request to the newly created
connection and finally start the connection. The request uses
the active session represented by `[FBSession activeSession]`.
@param graphPath The Graph API endpoint to use for the request, for example "me".
@param graphObject An object or open graph action to post.
@param handler The handler block to call when the request completes with a success, error, or cancel action.
*/
+ (FBRequestConnection*)startForPostWithGraphPath:(NSString*)graphPath
graphObject:(id<FBGraphObject>)graphObject
completionHandler:(FBRequestHandler)handler;

/*!
@method
@abstract
Creates an `FBRequest` object for a Graph API call, instantiate an
<FBRequestConnection> object, add the request to the newly created
connection and finally start the connection. Use this method for
specifying the request parameters and HTTP Method. The request uses
the active session represented by `[FBSession activeSession]`.
@param graphPath The Graph API endpoint to use for the request, for example "me".
@param parameters The parameters for the request. A value of nil sends only the automatically handled parameters, for example, the access token. The default is nil.
@param HTTPMethod The HTTP method to use for the request. A nil value implies a GET.
@param handler The handler block to call when the request completes with a success, error, or cancel action.
*/
+ (FBRequestConnection*)startWithGraphPath:(NSString*)graphPath
parameters:(NSDictionary*)parameters
HTTPMethod:(NSString*)HTTPMethod
completionHandler:(FBRequestHandler)handler;

/*!
@methodgroup FBRequest factory methods
Expand Down Expand Up @@ -438,20 +379,43 @@ typedef NSUInteger FBRequestState __attribute__((deprecated));

/*!
@method
@abstract
Returns a newly initialized request object that can be used to make a Graph API call for the active session.
Creates a request representing a status update.
@discussion
This method simplifies the preparation of a Graph API call.
Simplifies preparing a request to post a status update.
This method does not initialize an <FBRequestConnection> object. To initiate the API
call first instantiate an <FBRequestConnection> object, add the request to this object,
then call the `start` method on the connection instance.
@param message The message to post.
*/
+ (FBRequest *)requestForPostStatusUpdate:(NSString *)message;

@param graphPath The Graph API endpoint to use for the request, for example "me".
*/
+ (FBRequest*)requestForGraphPath:(NSString*)graphPath;
/*!
@method
@abstract
Creates a request representing a status update.
@discussion
Simplifies preparing a request to post a status update.
This method does not initialize an <FBRequestConnection> object. To initiate the API
call first instantiate an <FBRequestConnection> object, add the request to this object,
then call the `start` method on the connection instance.
@param message The message to post.
@param place The place to checkin with, or nil. Place may be an fbid or a
graph object representing a place.
@param tags Array of friends to tag in the status update, each element
may be an fbid or a graph object representing a user.
*/
+ (FBRequest *)requestForPostStatusUpdate:(NSString *)message
place:(id)place
tags:(id<NSFastEnumeration>)tags;

/*!
@method
Expand Down Expand Up @@ -485,4 +449,56 @@ typedef NSUInteger FBRequestState __attribute__((deprecated));
resultsLimit:(NSInteger)limit
searchText:(NSString*)searchText;

/*!
@method
@abstract
Returns a newly initialized request object that can be used to make a Graph API call for the active session.
@discussion
This method simplifies the preparation of a Graph API call.
This method does not initialize an <FBRequestConnection> object. To initiate the API
call first instantiate an <FBRequestConnection> object, add the request to this object,
then call the `start` method on the connection instance.
@param graphPath The Graph API endpoint to use for the request, for example "me".
*/
+ (FBRequest*)requestForGraphPath:(NSString*)graphPath;

/*!
@method
@abstract
Creates a request representing a POST for a graph object.
@param graphPath The Graph API endpoint to use for the request, for example "me".
@param graphObject An object or open graph action to post.
*/
+ (FBRequest*)requestForPostWithGraphPath:(NSString*)graphPath
graphObject:(id<FBGraphObject>)graphObject;

/*!
@method
@abstract
Returns a newly initialized request object that can be used to make a Graph API call for the active session.
@discussion
This method simplifies the preparation of a Graph API call.
This method does not initialize an <FBRequestConnection> object. To initiate the API
call first instantiate an <FBRequestConnection> object, add the request to this object,
then call the `start` method on the connection instance.
@param graphPath The Graph API endpoint to use for the request, for example "me".
@param parameters The parameters for the request. A value of nil sends only the automatically handled parameters, for example, the access token. The default is nil.
@param HTTPMethod The HTTP method to use for the request. A nil value implies a GET.
*/
+ (FBRequest*)requestWithGraphPath:(NSString*)graphPath
parameters:(NSDictionary*)parameters
HTTPMethod:(NSString*)HTTPMethod;
@end

0 comments on commit 67ce4ba

Please sign in to comment.