4

In this tip we are going to leverage the SimpleRequestFactory and gather a multipage response.

Let's pull the first 5 pages of 100 users on stackapps.com.

NOTE: for Silverlight, substitute Newtonsoft.Json.JsonConvert for the JavaScriptSerializer.

Simple Domain Objects

First we need some domain objects to deserialize into and a simple deserialization strategy.

using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Script.Serialization; // in System.Web.Extensions

namespace StackApps.Tips.ApiDomain
{

    public static class SimpleDeserializer
    {
        private static readonly JavaScriptSerializer Serializer = new JavaScriptSerializer();


        public static T Deserialize<T>(string json) where T : new()
        {
            return Serializer.Deserialize<T>(json);
        }


        public static T GetResponse<T>(HttpWebRequest request) where T : new()
        {
            using (var response = request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        string json = reader.ReadToEnd();
                        return Deserialize<T>(json);
                    }
                }
            }
        }
    }


    public class UsersResponse
    {
        public int page;
        public int pagesize;
        public List<User> users;
        public int total;
    }

    /// <summary>
    /// This is a slimmed down User. Add fields 
    /// as you see fit
    /// </summary>
    public class User
    {
        public string display_name;
        public string email_hash;
        public int reputation;
        public int user_id;
        public string user_type;
    }
}

Test as Example

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using NUnit.Framework;
using StackApps.Tips.ApiDomain;

namespace StackApps.Tips
{
    [TestFixture]
    public class PagedResponseFixture
    {
        [Test]
        public void SimpleTest()
        {
            const string yourAPiKey = "";

            var myApiRequestPath
                = new Uri("http://api.stackapps.com/0.9/users");


            var users = new List<User>();

            int page = 1;
            var parameters = new NameValueCollection
                                 {
                                     {"pagesize", "100"},
                                     {"page", page.ToString()},
                                 };

            HttpWebRequest request
                = SimpleRequestFactory.Create(myApiRequestPath, parameters, yourAPiKey);

            var response = SimpleDeserializer.GetResponse<UsersResponse>(request);

            while (response.users.Count > 0 && page < 6)
            {

                users.AddRange(response.users);
                page++;
                parameters["page"] = page.ToString();

                request = SimpleRequestFactory.Create(myApiRequestPath, parameters, yourAPiKey);
                response = SimpleDeserializer.GetResponse<UsersResponse>(request);
            }

            Assert.AreEqual(500, users.Count);
        }
    }
}

Remember to include the code for SimpleRequestFactory

0

You must log in to answer this question.

Browse other questions tagged .