Skip to content

Commit

Permalink
📜Implemented the fetching data from an external API
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorDolzan committed Mar 22, 2024
1 parent 3b52ad3 commit d000063
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ApiDataAccess/ApiDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ namespace StarWarsPlanetsStats.ApiDataAccess;

public class ApiDataReader : IApiDataReader
{
public Task<string> Read(string baseAddress, string requestUri)
public async Task<string> Read(string baseAddress, string requestUri)
{
throw new NotImplementedException();
using var client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
var response = await client.GetAsync(requestUri);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
20 changes: 20 additions & 0 deletions DTOs/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.Json.Serialization;

namespace StarWarsPlanetStatus.DTOs;

public record Result(
[property: JsonPropertyName("name")] string name,
[property: JsonPropertyName("rotation_period")] string rotation_period,
[property: JsonPropertyName("orbital_period")] string orbital_period,
[property: JsonPropertyName("diameter")] string diameter,
[property: JsonPropertyName("climate")] string climate,
[property: JsonPropertyName("gravity")] string gravity,
[property: JsonPropertyName("terrain")] string terrain,
[property: JsonPropertyName("surface_water")] string surface_water,
[property: JsonPropertyName("population")] string population,
[property: JsonPropertyName("residents")] IReadOnlyList<string> residents,
[property: JsonPropertyName("films")] IReadOnlyList<string> films,
[property: JsonPropertyName("created")] DateTime created,
[property: JsonPropertyName("edited")] DateTime edited,
[property: JsonPropertyName("url")] string url
);
10 changes: 10 additions & 0 deletions DTOs/Root.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace StarWarsPlanetStatus.DTOs;

public record Root(
[property: JsonPropertyName("count")] int count,
[property: JsonPropertyName("next")] string next,
[property: JsonPropertyName("previous")] object previous,
[property: JsonPropertyName("results")] IReadOnlyList<Result> results
);
11 changes: 10 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// See https://aka.ms/new-console-template for more information
using System.Text.Json;
using StarWarsPlanetsStats.ApiDataAccess;
using StarWarsPlanetStatus.DTOs;

var apiDataReader = new ApiDataReader();

var json = await apiDataReader.Read("https://swapi.dev/", "api/planets");

var root = JsonSerializer.Deserialize<Root>(json);

Console.ReadKey();
Console.WriteLine("Hello, World!");

0 comments on commit d000063

Please sign in to comment.