Search code examples
c#jsonrestrest-client

How to loop RestClient request passing id and then appending to response


The API I am using to grab data from is very limited. The only way I can get data out is via a GET request passing the identifier of the record I require. Issue is I could have multiple ID's I need data for. Is there an efficient way of looping a request and appending the json response to a List and adding that id to that JSON?

 private static List<Question> getQuestions(string bearer, List<int> customerids)
 {
   //the example below passes the customer ID 1.  What I want to do is pass customerids

   var qq = new RestClient("https://server.test.net/api/customers/1/");
   qq.AddDefaultHeader("Authorization", bearer);
   var request = new RestRequest("questions");
   var response = qq.Execute(request);
   string rawResponse = response.Content;
   List<Question> questions = JsonConvert.DeserializeObject<List<Question>>(rawResponse);
              
   return questions;
}

I need to run the API call through a loop (therefore a call for customer 1, 6, 776 if they exist in the customerids list) instead of currently its set to customer 1. I then want to append the result to the rawResponse together with the customer id as it doesnt exist in the data i pull back.

Unfortunately it's probably not the best way to run this and have had many conversations with the software supplier about reaching response limits but its the only way I can extract data by running in a loop and then joining the customerid back as it doesnt exist within the data i pull back, even though I can use it within the get request.

Therefore results would look like (where customer id didnt exist within the response, however added by passing from customerids list after the call, and appending all id's to same response dataset:

customerid question id answer
1 43 true
6 76 Married
774 76 Single

Question Class:

public class Question
{
public int QuestionID {get; set;}
public string answer {get; set;}
}

Potential foreach need some help in appending results for each customer also adding the associated customer id to the result

foreach (var item in customerids)
{
var qq = new RestClient("https://system.test.net/api/customers/" + item + "/");
qq.AddDefaultHeader("Authorization", bearer);
var request = new RestRequest("questions");
var response = qq.Execute(request);
string rawResponse = response.Content;
}

Solution

  • You have almost solved the issue based on the comments above.

    I think the only part which is not solved yet is how to assign customerid to the questions returned from the API.

    Once you receive the JSON from the API, you should deserialize it to a list of Questions and then assign CustomerId to each of the questions in the list.

    You can either use SELECT LINQ method or loop thru the list of Questions.

    In the following code, I have used approach of using SELECT LINQ method.

    public class Question
    {
        public int CustomerId { get; set; }
        public int QuestionId { get; set; }
        public string Answer { get; set; }
    }
    
    private static List<Question> GetQuestions(string bearer, List<int> customerids)
        {
            var list = new List<Question>();
    
            // Create RestClient with base address.
            var restClient = new RestClient("https://server.test.net/api");
    
            // Add authorization header.
            restClient.AddDefaultHeader("Authorization", bearer);
    
            // Loop thru customerids
            foreach (var customerid in customerids)
            {
                // Create RestRequest with URL for each customerid.
                var request = new RestRequest($"api/customers/{customerid}/questions", Method.GET);
    
                var res = restClient.Execute(request);
                var content = res.Content;
    
                // Deserialize response to a list of Questions.
                // here I am assuming that the JSON returned from the API is for a list of Questions.
                var questions = JsonConvert.DeserializeObject<List<Question>>(content);
    
                // Creating new list of Questions with customerid assigned to them.
                // Here I am assuming that the JSON from the API does not have customerId value in it. 
                questions = questions.Select(q => new Question { Answer = q.Answer, QuestionId = q.QuestionId, CustomerId = customerid }).ToList();
    
                // Add questions to the list to be returned.
                list.AddRange(questions);
            }
            return list;
        }
    

    I hope this will help you solve your issue.