Batch() public method

Makes a batch request to the Facebook server.
public Batch ( ) : object
return object
Esempio n. 1
0
        public static void BatchRequest(string accessToken)
        {
            try
            {
                var fb = new FacebookClient(accessToken);

                var result = (IList<object>)fb.Batch(
                    new FacebookBatchParameter("me"),
                    new FacebookBatchParameter(HttpMethod.Get, "me/friends", new { limit = 10 }));

                var result0 = result[0];
                var result1 = result[1];

                // Note: Always check first if each result set is an exeption.

                if (result0 is Exception)
                {
                    var ex = (Exception)result0;
                    // Note: make sure to handle this exception.
                    throw ex;
                }
                else
                {
                    var me = (IDictionary<string, object>)result0;
                    var name = (string)me["name"];

                    Console.WriteLine("Hi {0}", name);
                }

                Console.WriteLine();

                if (result1 is Exception)
                {
                    var ex = (Exception)result1;
                    // Note: make sure to handle this exception.
                    throw ex;
                }
                else
                {
                    var friends = (IList<object>)((IDictionary<string, object>)result1)["data"];

                    Console.WriteLine("Some of your friends: ");

                    foreach (IDictionary<string, object> friend in friends)
                    {
                        Console.WriteLine(friend["name"]);
                    }
                }
            }
            catch (FacebookApiException ex)
            {
                // Note: make sure to handle this exception.
                throw;
            }
        }
Esempio n. 2
0
        public static void BatchRequestWithFql(string accessToken)
        {
            try
            {
                var fb = new FacebookClient(accessToken);

                var result = (IList<object>)fb.Batch(
                    new FacebookBatchParameter("/4"),
                    new FacebookBatchParameter().Query("SELECT name FROM user WHERE uid=me()"));

                var result0 = result[0];
                var result1 = result[1];

                // Note: Always check first if each result set is an exeption.

                if (result0 is Exception)
                {
                    var ex = (Exception)result0;
                    // Note: make sure to handle this exception.
                    throw ex;
                }
                else
                {
                    Console.WriteLine("Batch Result 0: {0}", result0);
                }

                Console.WriteLine();

                if (result1 is Exception)
                {
                    var ex = (Exception)result1;
                    // Note: make sure to handle this exception.
                    throw ex;
                }
                else
                {
                    var fqlResult = (IList<object>)result1;

                    var fqlResult1 = (IDictionary<string, object>) fqlResult[0];
                    Console.WriteLine("Hi {0}", fqlResult1["name"]);
                }
            }
            catch (FacebookApiException ex)
            {
                // Note: make sure to handle this exception.
                throw;
            }
        }
        public CanvasModule(IFacebookApplication fbApp, FacebookClient fb)
        {
            this.HandleFacebookOAuthDialogError(fbApp.AppId, scope: "user_about_me,read_stream");
            this.DropFacebookQueryStrings();

            Post["/"] = _ =>
                            {
                                var canvasPageUrl = Context.FacebookCanvasPageUrl(fbApp.CanvasPage);
                                return View["index", canvasPageUrl];
                            };

            Post["/feed"] = _ =>
            {
                var perms = Context.FacebooPermissions();

                if (!perms.Intersect(new[] { "user_about_me", "read_stream" }).Any())
                    return Response.AsFacebookLogin(fbApp.AppId, scope: "user_about_me,read_stream");

                dynamic model = new JsonObject();
                model.canvasPageUrl = Context.FacebookCanvasPageUrl(fbApp.CanvasPage);
                model.facebookLoginUrl = Context.FacebookLoginUrl(fbApp.AppId, scope: "user_about_me,read_stream");

                if (perms.Contains("user_about_me"))
                {
                    dynamic result = fb.Get("me?fields=picture,name");
                    model.name = result.name;
                    model.picture = result.picture;
                }

                if (perms.Contains("read_stream"))
                {
                    dynamic result = fb.Get("me/feed");
                    model.feeds = result;
                }

                return View["Feed", model];
            };

            Post["/feed/batch"] = _ =>
                                {
                                    var perms = Context.FacebooPermissions();

                                    if (!perms.Intersect(new[] { "user_about_me", "read_stream" }).Any())
                                        return Response.AsFacebookLogin(fbApp.AppId, scope: "user_about_me,read_stream");

                                    dynamic model = new JsonObject();
                                    model.canvasPageUrl = Context.FacebookCanvasPageUrl(fbApp.CanvasPage);
                                    model.facebookLoginUrl = Context.FacebookLoginUrl(fbApp.AppId, scope: "user_about_me,read_stream");

                                    var bp = new Dictionary<string, Tuple<int, FacebookBatchParameter>>();
                                    int bpi = 0;
                                    if (perms.Contains("user_about_me"))
                                        bp.Add("me", new Tuple<int, FacebookBatchParameter>(bpi++, new FacebookBatchParameter("me?fields=picture,name")));

                                    if (perms.Contains("read_stream"))
                                        bp.Add("feeds", new Tuple<int, FacebookBatchParameter>(bpi++, new FacebookBatchParameter("me/feed")));

                                    dynamic result = fb.Batch(bp.Values.Select(t => t.Item2).ToArray());

                                    if (bp.ContainsKey("me"))
                                    {
                                        dynamic me = result[bp["me"].Item1];
                                        if (!(me is Exception))
                                        {
                                            model.name = me.name;
                                            model.picture = me.picture;
                                        }
                                    }

                                    if (bp.ContainsKey("feeds"))
                                    {
                                        dynamic feeds = result[bp["feeds"].Item1];
                                        if (!(feeds is Exception))
                                            model.feeds = feeds;
                                    }

                                    return View["Feed", model];
                                };
        }
        private void BatchRequestExample()
        {
            try
            {
                var fb = new FacebookClient(_accessToken);
                dynamic result = fb.Batch(
                    new FacebookBatchParameter { HttpMethod = HttpMethod.Get, Path = "/4" },
                    new FacebookBatchParameter(HttpMethod.Get, "/me/friend", new Dictionary<string, object> { { "limit", 10 } }), // this should throw error
                    new FacebookBatchParameter("/me/friends", new { limit = 1 }) { Data = new { name = "one-friend", omit_response_on_success = false } }, // use Data to add additional parameters that doesn't exist
                    new FacebookBatchParameter { Parameters = new { ids = "{result=one-friend:$.data.0.id}" } },
                    new FacebookBatchParameter("{result=one-friend:$.data.0.id}/feed", new { limit = 5 }),
                    new FacebookBatchParameter().Query("SELECT name FROM user WHERE uid="), // fql
                    new FacebookBatchParameter().Query("SELECT first_name FROM user WHERE uid=me()", "SELECT last_name FROM user WHERE uid=me()") // fql multi-query
                    //,new FacebookBatchParameter(HttpMethod.Post, "/me/feed", new { message = "test status update" })
                    );

                // always remember to check individual errors for the batch requests.
                if (result[0] is Exception)
                    MessageBox.Show(((Exception)result[0]).Message);
                dynamic first = result[0];
                string name = first.name;

                // note: incase the omit_response_on_success = true, result[x] == null

                // for this example, just comment it out.
                //if (result[1] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
                //if (result[2] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
                //if (result[3] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
                //if (result[4] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
                //if (result[5] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
                //if (result[6] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
                //if (result[7] is Exception)
                //    MessageBox.Show(((Exception)result[1]).Message);
            }
            catch (FacebookApiException ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }