Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for E-Tags #35

Closed
prabirshrestha opened this issue Jan 20, 2012 · 4 comments
Closed

support for E-Tags #35

prabirshrestha opened this issue Jan 20, 2012 · 4 comments
Assignees

Comments

@prabirshrestha
Copy link
Member

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.

@jimzim
Copy link

jimzim commented Jan 20, 2012

Yes we could use this. :)

Thanks,
Jim

On Jan 20, 2012, at 12:06 PM, "Prabir Shrestha" reply@reply.github.com 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

@prabirshrestha
Copy link
Member Author

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.

@prabirshrestha
Copy link
Member Author

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 });

@prabirshrestha
Copy link
Member Author

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\"" } } } });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants