using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using System.Collections.Generic;
public static void Main()
static async Task MainAsync()
var builder = Host.CreateApplicationBuilder(new string[0]);
builder.Services.AddHttpClient("DogApi", client =>
client.BaseAddress = new Uri("https://dogapi.dog/api/v2/breeds");
builder.Services.AddScoped<IDogBreedService, DogBreedService>();
var serviceProvider = builder.Services.BuildServiceProvider();
var dogService = serviceProvider.GetService<IDogBreedService>();
Dictionary<int,string> breedList = new Dictionary<int,string>();
Console.WriteLine("Choose Option:");
Console.WriteLine("1: Get Breeds");
Console.WriteLine("2: Get Breed By Id");
Console.WriteLine("3: Exit");
Console.WriteLine("Chose Option:");
Console.WriteLine("1: Get Breeds");
Console.WriteLine("1: Get Breed By Id");
step = Convert.ToInt32(Console.ReadLine());
var breeds = await dogService.GetBreeds();
foreach(var breedData in breeds.data)
breedList.Add(breedline,breedData.id);
Console.WriteLine($"{breedline++}: {breedData.attributes.name} / {breedData.id}");
Console.WriteLine("Goodbye");
Console.WriteLine("Enter Breed Number:");
var id = breedList[Convert.ToInt32(Console.ReadLine())];
var breed = await dogService.GetBreed(id);
Console.WriteLine(breed.data.attributes.name);
public interface IDogBreedService
Task<BreedCollection> GetBreeds();
Task<Breed> GetBreed(string id);
public class DogBreedService : IDogBreedService
private readonly HttpClient _httpClient;
public DogBreedService(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("DogApi"); ;
public async Task<BreedCollection> GetBreeds()
var response = await _httpClient.GetAsync(_httpClient.BaseAddress);
response.EnsureSuccessStatusCode();
var responseStream = await response.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<BreedCollection>(responseStream);
public async Task<Breed> GetBreed(string id)
var response = await _httpClient.GetAsync($"{_httpClient.BaseAddress}/{id}");
response.EnsureSuccessStatusCode();
var responseStream = await response.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<Breed>(responseStream);
public string name { get; set; }
public int min_life { get; set; }
public int max_life { get; set; }
public string description { get; set; }
public bool hypoallergenic { get; set; }
public string id { get; set; }
public string type { get; set; }
public Attributes attributes { get; set; }
public string self { get; set; }
public Data data { get; set; }
public Links links { get; set; }
public class BreedCollection
public Data[] data {get; set;}