using System.Collections.Generic;
using System.Threading.Tasks;
var client = new HttpClient() { BaseAddress = new Uri("https://api.jsonbin.io/b/5fb62f9404be4f05c9272f9b/2") };
client.DefaultRequestHeaders.Add("secret-key", "$2b$10$FRKBdPsyV27ReVeMpo83U.BYS0Jko5KUjctlj0A865qRkEYVf49ty");
var dataService = new DataService(client);
var items = await dataService.GetItemsAsync();
var nodes = items.ToTree();
var display = nodes.MakeFormattedString();
Console.WriteLine(display);
public static class NeedToImplementThis
public static Node ToTree(this IEnumerable<Node> nodes)
var hashed = nodes.ToLookup(x => x.Parent);
foreach (var node in nodes)
node.Childs = hashed[node.Id];
return hashed[-1].First();
public static StringBuilder MakeFormattedString(this Node node, StringBuilder sb = null, int level = 1)
sb = sb ?? new StringBuilder();
var indent = new string('\t', level);
sb.AppendLine($"{indent}<li>{node.Id}. {node.Text} ({level})</li>");
sb.AppendLine($"{indent}<ul>");
foreach (var child in node.Childs)
MakeFormattedString(child, sb, level + 1);
sb.AppendLine($"{indent}</ul>");
sb.Insert(0, "<ul>" + Environment.NewLine);
public int Id { get; set; }
public string Text { get; set; }
public int Parent { get; set; }
public IEnumerable<Node> Childs { get; set; }
public bool IsRoot => Id == 0;
public bool HasChilds => Childs != null && Childs.Any();
private readonly HttpClient _httpClient;
public DataService(HttpClient httpClient) => _httpClient = httpClient;
public async Task<IEnumerable<Node>> GetItemsAsync()
using var resp = await _httpClient.GetAsync("");
var json = await resp.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Node[]>(json);