如何循环传递 id 然后附加到响应的 RestClient 请求

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

我用来抓取数据的 API 非常有限。我可以获取数据的唯一方法是通过 GET 请求传递我需要的记录的标识符。问题是我可能有多个需要数据的 ID。是否有一种有效的方法来循环请求并将 json 响应附加到列表并将该 id 添加到 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;
}

我需要通过循环 运行 API 调用(因此调用客户 1、6、776,如果它们存在于客户 ID 列表中)而不是当前设置为客户 1 . 然后我想将结果连同客户 ID 一起附加到 rawResponse,因为它不存在于我拉回的数据中。

不幸的是,这可能不是 运行 的最佳方式,并且与软件供应商就达到响应限制进行了多次对话,但这是我可以通过 运行 提取数据的唯一方式循环然后加入 customerid,因为它不存在于我拉回的数据中,即使我可以在 get 请求中使用它。

因此结果看起来像(响应中不存在客户 ID,但是通过在调用后从 customerids 列表传递并将所有 ID 附加到同一响应数据集来添加:

客户编号 问题编号 回答
1 43 正确
6 76 已婚
774 76 单身

问题Class:

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

潜在的 foreach 需要一些帮助来为每个客户附加结果并将关联的客户 ID 添加到结果

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

根据上面的评论,你几乎已经解决了问题。

我认为唯一没有解决的部分是如何将 customerid 分配给从 API 返回的问题。

从 API 收到 JSON 后,您应该将其反序列化为问题列表,然后将 CustomerId 分配给列表中的每个问题。

您可以使用 SELECT LINQ 方法或循环遍历问题列表。

在下面的代码中,我使用了 SELECT LINQ 方法。

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

希望本文能帮助您解决问题。