Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions src/Twilio/Base/Page.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Twilio.Rest;
Expand Down Expand Up @@ -119,13 +120,35 @@
/// <param name="json">JSON payload</param>
/// <returns>Page of results</returns>
public static Page<T> FromJson(string recordKey, string json)
{
return FromJson(recordKey, json, null);
}

/// <summary>
/// Converts a JSON payload to a Page of results
/// </summary>
/// <param name="recordKey">JSON key where the records are</param>
/// <param name="json">JSON payload</param>
/// <param name="requestUrl">Original request URL, used for token-based pagination</param>
/// <returns>Page of results</returns>
public static Page<T> FromJson(string recordKey, string json, string requestUrl)

Check failure on line 134 in src/Twilio/Base/Page.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=twilio_twilio-csharp&issues=AZ1jClVCfzVx0pBE-Mfh&open=AZ1jClVCfzVx0pBE-Mfh&pullRequest=822
{
var root = JObject.Parse(json);
var records = root[recordKey];
var parsedRecords = records.Children().Select(
record => JsonConvert.DeserializeObject<T>(record.ToString())
).ToList();

List<T> parsedRecords;
if (records.Children().Any() && records.Children().First().Type != JTokenType.Object)
{
parsedRecords = records.Children().Select(
record => (T)System.Activator.CreateInstance(typeof(T), record.ToString())
).ToList();
}
else
{
parsedRecords = records.Children().Select(
record => JsonConvert.DeserializeObject<T>(record.ToString())
).ToList();
}

if(root["uri"] != null){
var uriNode = root["uri"];
if (uriNode != null)
Expand All @@ -147,9 +170,25 @@
}
}

// next-gen API
if(root["meta"] != null){
var meta = root["meta"];

// token-based pagination
if (meta["pageSize"] != null)
{
var nextToken = meta["nextToken"]?.Value<string>();
var previousToken = meta["previousToken"]?.Value<string>();
string baseUrl = requestUrl != null ? requestUrl.Split('?')[0] : null;

return new Page<T>(
parsedRecords,
meta["pageSize"].Value<int>(),
nextPageUrl: nextToken != null && baseUrl != null ? baseUrl + "?pageToken=" + nextToken : null,
previousPageUrl: previousToken != null && baseUrl != null ? baseUrl + "?pageToken=" + previousToken : null
);
}

// next-gen API
return new Page<T>(
parsedRecords,
meta["page_size"].Value<int>(),
Expand Down
1 change: 1 addition & 0 deletions src/Twilio/Rest/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static implicit operator Domain(string value)
public static readonly Domain Marketplace = new Domain("marketplace");
public static readonly Domain Messaging = new Domain("messaging");
public static readonly Domain Monitor = new Domain("monitor");
public static readonly Domain Memory = new Domain("memory");
public static readonly Domain Notify = new Domain("notify");
public static readonly Domain Numbers = new Domain("numbers");
public static readonly Domain Oauth = new Domain("oauth");
Expand Down
Loading