static HttpClient client = new();
const string url_string = "http://127.0.0.1:8000/api/";
const string menu = "Menu\n----\n\n1. List all countries\n2. Show the continent of a Country\n3. Show a country's population.\n4. Quit.";
Action? act = ReadLine($"\n\n\n{menu}\n> ") switch
"2" => continent_of_country,
"3" => country_population,
ReadLine("Press enter to continue");
static void list_countries()
var response = JsonSerializer.Deserialize<JsonElement>(client.GetStringAsync($"{url_string}list_countries").Result);
foreach (var row in response.GetProperty("results").EnumerateArray())
$"Country: {row.GetProperty("name").GetString()}\n" +
$"Population: {row.GetProperty("population").GetInt64()}\n" +
$"Capital: {row.GetProperty("capital").GetString()}\n" +
$"Language: {row.GetProperty("language").GetString()}\n"
static void continent_of_country()
string? country = ReadLine("Enter the country: ");
var response = JsonSerializer.Deserialize<JsonElement>(client.GetStringAsync($"{url_string}country_continent?country={country}").Result);
Console.WriteLine($"The continent that {country} is part of is {response.GetProperty("results").EnumerateArray().First().GetProperty("continent").GetString()}");
static void country_population()
string? country = ReadLine("Enter the country: ");
var response = JsonSerializer.Deserialize<JsonElement>(client.GetStringAsync($"{url_string}country_continent?country={country}").Result);
Console.WriteLine($"The Population of {country} is {response.GetProperty("results").EnumerateArray().First().GetProperty("population").GetInt64()}");
static void BadSelection() => Console.WriteLine("Invalid Selection\nTry again.");
static string? ReadLine(string? prompt = null)
Console.Write(prompt ?? "> ");
return Console.ReadLine();