Interface
namespace DesafiosAluraApi.ConsumindoApi.Interface;
public interface IApiMethods
{
public List<T> GetAll<T>(HttpClient client) where T : class;
public void ViewMovieInformation();
}
Filme
using DesafiosAluraApi.ConsumindoApi.Interface;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DesafiosAluraApi.ConsumindoApi;
public class MovieApi : IApiMethods
{
public readonly static string _url = "https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/TopMovies.json";
[JsonPropertyName("rank")]
public string? Rank { get; set; }
[JsonPropertyName("fullTitle")]
public string? Title { get; set; }
[JsonPropertyName("imDbRating")]
public string? Rating { get; set; }
public void ViewMovieInformation() =>
Console.WriteLine($"Classificação: {Rank}\nTítulo: {Title}\nNota: {Rating}\n\n");
public List<T> GetAll<T>(HttpClient client) where T : class
{
var result = client.GetStringAsync(_url).Result;
List<T> List = JsonSerializer.Deserialize<List<T>>(result) ?? [];
return List;
}
}
País
using System.Text.Json.Serialization;
using System.Text.Json;
using DesafiosAluraApi.ConsumindoApi.Interface;
namespace DesafiosAluraApi.ConsumindoApi;
public class CountryApi : IApiMethods
{
public readonly static string _url = "https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/Paises.json";
[JsonPropertyName("nome")]
public string? Name { get; set; }
[JsonPropertyName("capital")]
public string? Capital { get; set; }
[JsonPropertyName("idioma")]
public string? Language { get; set; }
public void ViewMovieInformation() =>
Console.WriteLine($"Páis: {Name}\nCapital: {Capital}\nIdioma: {Language}\n\n");
public List<T> GetAll<T>(HttpClient client) where T : class
{
var result = client.GetStringAsync(_url).Result;
List<T> List = JsonSerializer.Deserialize<List<T>>(result) ?? [];
return List;
}
}
Carro
using System.Text.Json.Serialization;
using System.Text.Json;
using DesafiosAluraApi.ConsumindoApi.Interface;
namespace DesafiosAluraApi.ConsumindoApi;
public class CarApi : IApiMethods
{
public readonly static string _url = "https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/Carros.json";
[JsonPropertyName("marca")]
public string? Brand { get; set; }
[JsonPropertyName("modelo")]
public string? Model { get; set; }
[JsonPropertyName("ano")]
public int Year { get; set; }
[JsonPropertyName("motor")]
public string? Engine { get; set; }
public void ViewMovieInformation() =>
Console.WriteLine($"Carro: {Brand} {Model}\nAno: {Year}\nMotor: {Engine}\n\n");
public List<T> GetAll<T>(HttpClient client) where T : class
{
var result = client.GetStringAsync(_url).Result;
List<T> List = JsonSerializer.Deserialize<List<T>>(result) ?? [];
return List;
}
}
Livro
using DesafiosAluraApi.ConsumindoApi.Interface;
using System.Text.Json.Serialization;
using System.Text.Json;
namespace DesafiosAluraApi.ConsumindoApi;
internal class BookApi : IApiMethods
{
public readonly static string _url = "https://raw.githubusercontent.com/ArthurOcFernandes/Exerc-cios-C-/curso-4-aula-2/Jsons/Livros.json";
[JsonPropertyName("titulo")]
public string? Title { get; set; }
[JsonPropertyName("autor")]
public string? Author { get; set; }
[JsonPropertyName("ano_publicacao")]
public int Year { get; set; }
[JsonPropertyName("genero")]
public string? Genre { get; set; }
public void ViewMovieInformation() =>
Console.WriteLine($"Título: {Title}\nAutor: {Author}\nAno: {Year}\nGênero: {Genre}\n\n");
public List<T> GetAll<T>(HttpClient client) where T : class
{
var result = client.GetStringAsync(_url).Result;
List<T> List = JsonSerializer.Deserialize<List<T>>(result) ?? [];
return List;
}
}
Program
using DesafiosAluraApi.ConsumindoApi;
using DesafiosAluraApi.ConsumindoApi.Interface;
using (HttpClient client = new())
{
var apiMethod = ((IApiMethods)new MovieApi()).GetAll<MovieApi>(client);
var apiMethod2 = ((IApiMethods)new CountryApi()).GetAll<CountryApi>(client);
var apiMethod3 = ((IApiMethods)new CarApi()).GetAll<CarApi>(client);
var apiMethod4 = ((IApiMethods)new BookApi()).GetAll<BookApi>(client);
apiMethod.ForEach(m => m.ViewMovieInformation());
apiMethod2.ForEach(m => m.ViewMovieInformation());
apiMethod3.ForEach(m => m.ViewMovieInformation());
apiMethod4.ForEach(m => m.ViewMovieInformation());
}