Search code examples
c#freshdesk

Get all users from API


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

Solution

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