我目前正在以下列方式使用TwilioRestClient(显然这是一个循环):
var currentPage = 0;
private TwilioRestClient mTwilioClient;
var listRequest = new MessageListRequest()
{
To = PhoneNumber,
Count = mMessagesPerPage,
DateSent = DateTime.Today.Subtract(TimeSpan.FromDays(mDaysToSearch)),
DateSentComparison = ComparisonType.GreaterThanOrEqualTo,
PageNumber = currentPage++,
};
var result = mTwilioClient.ListMessages(listRequest);
这对我来说很好,但我正在阅读Paging Through API Resources
中的部分“Page参数已被弃用,可能会在API的未来版本中删除.Page参数比nextpageuri慢,如果使用Page参数进行分页时创建新资源,连续页面可能包含重复数据。“
如果Page在不久的将来得到折旧,我想开始使用nextpageuri。
我的问题是如何通过TwilioRestHelper(C#)使用nextpageuri?我一直无法找到这方面的例子,我没有看到任何适当的方法调用来做到这一点。
答案 0 :(得分:0)
所以我最终做的事情如下:
private TwilioRestClient mTwilioClient;
var listRequest = new MessageListRequest()
{
To = PhoneNumber,
Count = mMessagesPerPage,
DateSent = DateTime.Today.Subtract(TimeSpan.FromDays(mDaysToSearch)),
DateSentComparison = ComparisonType.GreaterThanOrEqualTo,
};
var ret = mTwilioClient.ListMessages(options);
// ret.Messages will now have your first page of messages
// For your next page of results
if (ret.NextPageUri != null)
{
ret.next_page_uri = ret.NextPageUri.ToString().Substring(11);
}
IRestResponse result = mTwilioClient.Execute(new RestSharp.RestRequest(ret.next_page_uri));
// The classes provided by Twilio do not line up with the fields return in Json for some reason, so we have to massage the data a bit.
result.Content = CleanupJsonContent(result.Content);
// Deserialize the Json content into a class.
var nextRet = JsonConvert.DeserializeObject<TwilioResponse>(result.Content);
// Set the next_page_uri string value. We can't use the Uri class since it doesn't seem to be a valid Uri...
nextRet.next_page_uri = nextRet.NextPageUri.ToString().Substring(11);
private string CleanupJsonContent(string jsonContent)
{
string ret = jsonContent.Replace("date_sent", "DateSent");
ret = ret.Replace("account_sid", "AccountSid");
ret = ret.Replace("date_created", "DateCreated");
ret = ret.Replace("date_updated", "DateUpdated");
ret = ret.Replace("num_segments", "NumSegments");
ret = ret.Replace("api_version", "ApiVersion");
ret = ret.Replace("price_unit", "PriceUnit");
ret = ret.Replace("error_code", "ErrorCode");
ret = ret.Replace("error_message", "ErrorMessage");
ret = ret.Replace("first_page_uri", "FirstPageUri");
ret = ret.Replace("previous_page_uri", "PreviousPageUri");
ret = ret.Replace("page_size", "PageSize");
ret = ret.Replace("next_page_uri", "NextPageUri");
ret = ret.Replace("num_pages", "NumPages");
ret = ret.Replace("last_page_uri", "LastPageUri");
return ret;
}