Lesson 10 - Displaying Json
This commit is contained in:
25
SpectreDemos/Helpers.cs
Normal file
25
SpectreDemos/Helpers.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using Spectre.Console;
|
||||
|
||||
namespace SpectreDemos;
|
||||
|
||||
public static class Helpers
|
||||
{
|
||||
public static async Task<string> FetchApiDataAsync(string apiUrl)
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await client.GetAsync(apiUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]Error fetching data from API:[/]{ex.Message}");
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
using Spectre.Console;
|
||||
using Spectre.Console.Json;
|
||||
using SpectreDemos;
|
||||
|
||||
#region "Previous Lessons"
|
||||
//Lesson 2 - Initial Setup
|
||||
@ -149,20 +151,37 @@
|
||||
////panel.Padding(2, 1);
|
||||
|
||||
////AnsiConsole.Write(panel);
|
||||
#endregion
|
||||
|
||||
// Lesson 9 - FIGlet Text
|
||||
|
||||
AnsiConsole.Write(new FigletText("Hello")
|
||||
.Centered()
|
||||
.Color(Color.Red)
|
||||
);
|
||||
////AnsiConsole.Write(new FigletText("Hello")
|
||||
//// .Centered()
|
||||
//// .Color(Color.Red)
|
||||
////);
|
||||
|
||||
FigletText figlet = new("World");
|
||||
figlet.Centered();
|
||||
figlet.Color(Color.Red);
|
||||
////FigletText figlet = new("World");
|
||||
////figlet.Centered();
|
||||
////figlet.Color(Color.Red);
|
||||
|
||||
AnsiConsole.Write(figlet);
|
||||
////AnsiConsole.Write(figlet);
|
||||
#endregion
|
||||
|
||||
// Lesson 10 - Displaying Json
|
||||
|
||||
string jsonResponse = await Helpers.FetchApiDataAsync("https://thesampleapi.com/courses/12");
|
||||
|
||||
JsonText json = new JsonText(jsonResponse);
|
||||
|
||||
json.StringColor(Color.Yellow);
|
||||
json.ColonColor(Color.Orange1);
|
||||
|
||||
|
||||
AnsiConsole.Write(
|
||||
new Panel(json)
|
||||
.Header("API Results")
|
||||
.Collapse()
|
||||
.BorderColor(Color.White)
|
||||
);
|
||||
|
||||
Console.ReadLine();
|
||||
AnsiConsole.Clear();
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Spectre.Console" Version="0.50.0" />
|
||||
<PackageReference Include="Spectre.Console.Json" Version="0.50.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user