从 API 获取所有用户
Get all users from API
我是 API 世界的新手,需要获取我们所有用户的列表(总共 400 个)。最初我只能获得 30 个用户,但在阅读 API 注释后我看到 30 是默认值。我将页面值设置为 100,这是最大值,我可以在我的数据网格中显示 100 个用户。但是,我想获得所有 400 个用户。据我了解,我需要设置分页才能获得其他 300 个,但不知道如何实现。如果有人可以查看我的代码并提出建议,我将不胜感激。 returns 下方的代码是我在 winforms 数据网格中显示的联系人列表。
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);
}
}
}
根据文档,似乎响应将包含一个标题为 "link" 的 header,其中将包含 URL 和下一组数据...如果 header 没有设置,这意味着没有更多的记录。
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"
一个简单的递归函数就可以完成这项工作......见下文。
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;
}
我是 API 世界的新手,需要获取我们所有用户的列表(总共 400 个)。最初我只能获得 30 个用户,但在阅读 API 注释后我看到 30 是默认值。我将页面值设置为 100,这是最大值,我可以在我的数据网格中显示 100 个用户。但是,我想获得所有 400 个用户。据我了解,我需要设置分页才能获得其他 300 个,但不知道如何实现。如果有人可以查看我的代码并提出建议,我将不胜感激。 returns 下方的代码是我在 winforms 数据网格中显示的联系人列表。
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);
}
}
}
根据文档,似乎响应将包含一个标题为 "link" 的 header,其中将包含 URL 和下一组数据...如果 header 没有设置,这意味着没有更多的记录。
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"
一个简单的递归函数就可以完成这项工作......见下文。
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;
}