GithubHelp home page GithubHelp logo

Comments (4)

jimzim avatar jimzim commented on May 22, 2024

Yes we could use this. :)

Thanks,
Jim

On Jan 20, 2012, at 12:06 PM, "Prabir Shrestha" [email protected] wrote:

https://developers.facebook.com/blog/post/627/

We would most probably want a method that looks similar to this.

dynamic result = fb.Api("GET", "me", new { fields = new { "id", "name"}, etag = "...." });
dynamic headers = result.headers;
dynamic etag = result.headers.etag;
dynamic body = result.body;

Then result.body actually contains the actual json result.
etag parameter is a special parameter like the "access_token" parameter. it should be set as a request header instead of normal parameter.


Reply to this email directly or view it on GitHub:
#35

from facebook-csharp-sdk.

prabirshrestha avatar prabirshrestha commented on May 22, 2024

added etag support for sync methods. Does not yet work for batch requests and async methods.

The ETag is calculated using the entire response from the API call including its formatting. Developers should be aware that the formatting of API response output may be impacted by the user agent string. Therefore, calls originating from the same client should keep the user agent consistent between calls. (https://developers.facebook.com/blog/post/627/)

 FacebookClient.SetDefaultHttpWebRequestFactory(
            uri =>
            new HttpWebRequestWrapper((HttpWebRequest)WebRequest.Create(uri)) { UserAgent = "Facebook C# SDK" });

Normal request still works like v5.

var fb = new FacebookClient("access_token");
dynamic result = fb.Get("me", new { fields = "id,name"});
string id = result.id;

For etag you need to pass the special parameter called etag
For the first request it should be empty string

dynamic result1 = fb.Get("me", new { fields = "id,name", _etag_ = string.Empty});

This will tell the fb c# sdk to return a JsonObject with headers and body.

dynamic headers = result1.headers; // JsonObject of headers.
dynamic body = result1.body; // The actual json response.
// to access the actual json response use result.body.x instead of just result.x
string id = result1.body.id;

then the you can use the etag from the previous response to get the next responses.

dynamic result2 = fb.Get("me", new {fields = "id,name", _etag_ = result1.headers.ETag});
dynamic headers = result1.headers;
// always check if the response has a body.
if(result2.ContainsKey("body")) {
    // we've got the updated response.
    string id = result1.id;
}
else {
    // the last request was the latest.
    // so do nothing.
}

Note: result1.header.ETag (make sure ETag contains the right capitalization). It is exactly how Facebook returns the response header.
when etag is string.Empty it will always return a body, so you don't need to check result1.ContainsKey("body") for it.

from facebook-csharp-sdk.

prabirshrestha avatar prabirshrestha commented on May 22, 2024

Sample code for using etag with async methods.

Note: If you are using XAsync methods make sure to use different instance of FacebookClient. Use same instance only if you are using with XTaskAsync.

var firstFb = new FacebookClient(accessToken);
var secondFb = new FacebookClient(accessToken);

secondFb.GetCompleted +=
    (o, e) =>
    {
        if (e.Cancelled)
        {
            Console.WriteLine("cancelled");
              return;
        }
        else if (e.Error != null)
        {
            Console.WriteLine(e.Error.Message);
            return;
        }
        dynamic result = e.GetResultData();
        Console.WriteLine(result.ToString());
        Console.WriteLine(result.ContainsKey("body"));
    };

firstFb.GetCompleted +=
    (o, e) =>
    {
        if (e.Cancelled)
        {
            Console.WriteLine("cancelled");
            return;
        }
        else if (e.Error != null)
        {
            Console.WriteLine(e.Error.Message);
            return;
        }

        dynamic result = e.GetResultData();
        Console.WriteLine(result.ToString());
        Console.WriteLine(result.ContainsKey("body"));
        secondFb.GetAsync("me?fields=id,name", new { _etag_ = result.headers.ETag });
    };

firstFb.GetAsync("me?fields=id,name", new { _etag_ = string.Empty });

from facebook-csharp-sdk.

prabirshrestha avatar prabirshrestha commented on May 22, 2024

Here is an example of using etags in batch requests.

var fb = new FacebookClient(accessToken);

dynamic result = fb.Batch(
    new FacebookBatchParameter("me", new { _etag_ = string.Empty }),
    new FacebookBatchParameter("me", new { _etag_ = "\"ac9e51b60e883e294cc98f35f70a1ec8fdf0e736\"" }),
    new FacebookBatchParameter("me") { Data = new { headers = new Dictionary<string, object> { { "If-None-Match", "\"ac9e51b60e883e294cc98f35f70a1ec8fdf0e736\"" } } } });

from facebook-csharp-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.