Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paging through NetFlix odata results

I am playing around with the Netflix odata service to get a better understanding of how to consume odata data.

In VS 2010 I added a service reference to the NetFlix odata service. I then wrote this code which returns only some of the data.

        var cat = new NetflixCatalog(new Uri("http://odata.netflix.com/v1/Catalog/"));

        var x = from t in cat.Titles
                where t.ReleaseYear == 2009
                select t;

        foreach (Title title in x)
        {
            ProcessTitle(title);
        }

I looked at the uri generated for the call and ran it in a browser. The atom feed it returns has this element at the end

  <link rel="next" href="http://odata.netflix.com:20000/v1/Catalog/Titles()/?$filter=ReleaseYear%20eq%202009&amp;$orderby=AverageRating%20desc&amp;$skiptoken=3.9D,'BVqRa'" />

This is the a link that will retrieve the next set of data (paging done by Netflix). My question is how do I get my code to access this next batch of data and the next etc.?

like image 852
woaksie Avatar asked Nov 30 '25 17:11

woaksie


1 Answers

The query can be cast to DataServiceQuery, which has a method called Execute which returns the results as QueryOperationResponse which has a GetContinuation method, which returns a continuation object representing the next link. A rough code to go through all the titles could look like this:

var cat = new NetflixCatalog(new Uri("http://odata.netflix.com/v1/Catalog/"));

var x = from t in cat.Titles
        where t.ReleaseYear == 2009
        select t;
var response = (QueryOperationResponse<Title>)((DataServiceQuery<Title>)x).Execute();

while (true)
{
    foreach (Title title in response)
    {
        Console.WriteLine(title.Name);
    }

    var continuation = response.GetContinuation();
    if (continuation == null)
    {
        break;
    }

    response = cat.Execute(continuation);
}
like image 113
Vitek Karas MSFT Avatar answered Dec 05 '25 12:12

Vitek Karas MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!