diff --git a/OriginDotNetChallenge.Unit/OriginDotNetChallenge.Unit.csproj b/OriginDotNetChallenge.Unit/OriginDotNetChallenge.Unit.csproj new file mode 100644 index 0000000..6db60a6 --- /dev/null +++ b/OriginDotNetChallenge.Unit/OriginDotNetChallenge.Unit.csproj @@ -0,0 +1,28 @@ + + + + net6.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/OriginDotNetChallenge.Unit/UnitTest1.cs b/OriginDotNetChallenge.Unit/UnitTest1.cs new file mode 100644 index 0000000..a84172a --- /dev/null +++ b/OriginDotNetChallenge.Unit/UnitTest1.cs @@ -0,0 +1,28 @@ +using OriginDotNetChallenge.Controllers; + +namespace OriginDotNetChallenge.Unit; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + var controller = new PokemonController(new HttpClient()); + var expectedUrl = "v2/pokemon?offset=0&limit=20"; + + var actualUrl = controller.BuildGetUrl(1); + + Assert.Equal(expectedUrl, actualUrl); + } + + [Fact] + public void Test2() + { + var controller = new PokemonController(new HttpClient()); + var expectedUrl = "v2/pokemon?offset=20&limit=20"; + + var actualUrl = controller.BuildGetUrl(2); + + Assert.Equal(expectedUrl, actualUrl); + } +} \ No newline at end of file diff --git a/OriginDotNetChallenge.Unit/Usings.cs b/OriginDotNetChallenge.Unit/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/OriginDotNetChallenge.Unit/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/OriginDotNetChallenge.sln b/OriginDotNetChallenge.sln index 1881354..99963a3 100644 --- a/OriginDotNetChallenge.sln +++ b/OriginDotNetChallenge.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OriginDotNetChallenge", "OriginDotNetChallenge\OriginDotNetChallenge.csproj", "{90CC368F-58C8-4801-8428-6A3F715CF588}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OriginDotNetChallenge.Unit", "OriginDotNetChallenge.Unit\OriginDotNetChallenge.Unit.csproj", "{C7C815CE-A900-445D-886E-91CCE2EF01B3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {90CC368F-58C8-4801-8428-6A3F715CF588}.Debug|Any CPU.Build.0 = Debug|Any CPU {90CC368F-58C8-4801-8428-6A3F715CF588}.Release|Any CPU.ActiveCfg = Release|Any CPU {90CC368F-58C8-4801-8428-6A3F715CF588}.Release|Any CPU.Build.0 = Release|Any CPU + {C7C815CE-A900-445D-886E-91CCE2EF01B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C7C815CE-A900-445D-886E-91CCE2EF01B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C7C815CE-A900-445D-886E-91CCE2EF01B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C7C815CE-A900-445D-886E-91CCE2EF01B3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/OriginDotNetChallenge/Controllers/PokemonController.cs b/OriginDotNetChallenge/Controllers/PokemonController.cs index 45e9f6f..2c95ecc 100644 --- a/OriginDotNetChallenge/Controllers/PokemonController.cs +++ b/OriginDotNetChallenge/Controllers/PokemonController.cs @@ -1,3 +1,5 @@ +using System.Net.Http.Headers; +using System.Text.Json; using Microsoft.AspNetCore.Mvc; namespace OriginDotNetChallenge.Controllers; @@ -6,9 +8,49 @@ namespace OriginDotNetChallenge.Controllers; [Route("pokemon")] public class PokemonController : ControllerBase { - public IActionResult Get() + public readonly HttpClient _client; + + public PokemonController(HttpClient client) { - return Ok("hello world"); + _client = client; } + public async Task Get(int page = 1) + { + _client.BaseAddress = new Uri("https://pokeapi.co/api/"); + _client.DefaultRequestHeaders.Authorization = + new AuthenticationHeaderValue("origin-user", + "eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI"); + var response = await _client.GetAsync(BuildGetUrl(page)); + + var result = await response.Content.ReadAsStringAsync(); + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }; + var deserializedObject = JsonSerializer.Deserialize(result, options); + + + return Ok(deserializedObject?.Results); + } + + public string BuildGetUrl(int page) + { + var limit = 20; + var offset = page - 1; + + return $"v2/pokemon?offset={offset}&limit={limit}"; + } + +} + +public class PokeApiResponse +{ + public List Results { get; set; } +} + +public class PokemonDTO +{ + public string Name { get; set; } + public string Url { get; set; } } \ No newline at end of file diff --git a/OriginDotNetChallenge/Program.cs b/OriginDotNetChallenge/Program.cs index 31d0922..5ba0ed6 100644 --- a/OriginDotNetChallenge/Program.cs +++ b/OriginDotNetChallenge/Program.cs @@ -5,6 +5,7 @@ $"appsettings.{builder.Environment.EnvironmentName}.json", optional: false ); builder.Services.AddControllers(); +builder.Services.AddSingleton(); var app = builder.Build();