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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user