How can I use C # TwilioRestClient and nextpageuri to swap

I am currently using TwilioRestClient as follows (this is obvious in a loop):

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

      

This works great for me, but I am reading a section from Paging Through API Resources

"The Page parameter is deprecated and may be removed in a future version of the API. The Page parameter is slower than nextpageuri, and if new resources are created when swapped with a page parameter, consecutive pages may contain duplicate data."

And I would like to start using the next page in case the page depreciates in the near future.

My question is, how can I use nextpageuri via TwilioRestHelper (C #)? I haven't been able to find examples of this, and I don't see any appropriate method calls for this.

+3


source to share


1 answer


So what I did was something like the following:



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

      

0


source







All Articles