-1

I'm new to the world of API and need to get a list of all of our users (400 in total). Initially I could only get 30 users but after reading the API notes I see that 30 is a default value. I set the page value to 100, which is the max and I can display 100 users in my data grid. However, I would like to get all 400 users. From what I understand I need to set up pagination to get the other 300, but have no idea how to achieve this. I'd appreciate it if somebody could look at my code and advise. The code below returns a list of contacts, which I display in a winforms datagrid.

public static async Task<List<Contact>> LoadContacts(string filter)
    {
        string apiPath = ApiHelper.ApiClient.BaseAddress.ToString();
        switch (filter)
        {
            case "Deleted":
                apiPath += "?state=deleted;per_page=100";
                break;
            default:
                apiPath += "?per_page=100";
                break;
        }

        using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(apiPath))
        {
            Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);

            if (response.IsSuccessStatusCode)
            {
                List<Contact> emp = await response.Content.ReadAsAsync<List<Contact>>();

                return emp;

            }
            else
            {
                throw new Exception(response.ReasonPhrase);
            }
        }
    }
madreflection
  • 4,744
  • 3
  • 19
  • 29
Jono
  • 49
  • 1
  • 8
  • 1
    Which API are you using? Any defaults would specific to that particular API's implementation, as would limits and any other operational parameters. It's impossible to know how to manipulate an API without knowing details about it and, in all likelihood, it's something specific to your application so you would have to share that code, too. In the process of doing so, you should be looking for those details yourself. You may be able to answer your own question in the process. – madreflection Jan 13 '20 at 22:04
  • https://developer.freshdesk.com/api/#list_all_contacts. – Jono Jan 13 '20 at 22:24
  • Well, for one thing, you're using semicolon (`;`) to separate parameters whereas the docs show ampersand (`&`), which is normal for query strings. – madreflection Jan 13 '20 at 22:26
  • I have been reading the documentation, and got this far by my own research. section 9 says, To scroll through the pages add page parameter to the url. The page number starts with 1 and should not exceed 10 - does this mean I have to set up a loop and keep requesting pages until there are no more pages? – Jono Jan 13 '20 at 22:30
  • According to the documentation, it would seem as though that's what you'll have to do. Page size is capped at 100 and page number is capped at 10, limiting you to a total of 1000 items across 10 requests. – madreflection Jan 13 '20 at 22:34

1 Answers1

2

From the documentation, it appears that the Response will contain a header titled "link" which will contain the URL with the next set of data... if the header isn't set, that means there are no more records.

The 'link' header in the response will hold the next page url if exists. If you have reached the last page of objects, then the link header will not be set.

Headers: "link":< https://domain.freshdesk.com/api/v2/tickets?filter=all_tickets&page=2>;rel="next"

A simple recursive function will do the job... see below.

    public void GetAllContacts()
    {
        List<YourModel> listOfContacts = new List<YourModel();
        string startingUrl = "https://domain.freshdesk.com/api/v2/contacts?per_page=10";

        void GetNext(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                //reader.ReadToEnd(); // deserialize to model
                // listOfContacts.Add(<deserialized model>);

                if (response.Headers["link"] != null) // Check if the header is set on the Response
                    GetNext(response.Headers["link"]); // Header was set, recursive call to the link from the header
            }
        }


        GetNext(startingUrl);

        return listOfContacts;

    }
Adrian
  • 8,271
  • 2
  • 26
  • 43