Created
June 18, 2020 16:38
-
-
Save jsheridanwells/b4306d62dc2e33d0255d110b67b7286f to your computer and use it in GitHub Desktop.
weather-walking-skeleton-part_1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WeatherWalkingSkeleton.Config | |
{ | |
public class OpenWeather | |
{ | |
public string ApiKey { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<UserSecretsId>65988f0a-26ed-44ef-8749-f86a2f5c18a9</UserSecretsId> | |
</PropertyGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Startup.cs | |
using WeatherWalkingSkeleton.Config; | |
// [...] | |
public class Startup | |
{ | |
// [...] | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Add OpenWeatherMap API key | |
var openWeatherConfig = Configuration.GetSection("OpenWeather"); | |
services.Configure<OpenWeather>(openWeatherConfig); | |
// [...] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WeatherForecast.cs | |
using System; | |
namespace WeatherWalkingSkeleton.Models | |
{ | |
public class WeatherForecast | |
{ | |
public DateTime Date { get; set; } | |
public decimal Temp { get; set; } | |
public decimal FeelsLike { get; set; } | |
public decimal TempMin { get; set; } | |
public decimal TempMax { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OpenWeatherResponse.cs | |
using System.Collections.Generic; | |
using System.Text.Json.Serialization; | |
namespace WeatherWalkingSkeleton.Models | |
{ | |
public class OpenWeatherResponse | |
{ | |
[JsonPropertyName("list")] | |
public List<Forecast> Forecasts { get; set; } | |
} | |
// [... continue to add here] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OpenWeatherResponse.cs | |
// [...] | |
public class Forecast | |
{ | |
[JsonPropertyName("dt")] | |
public int Dt { get; set; } | |
[JsonPropertyName("main")] | |
public Temps Temps { get; set; } | |
} | |
// [...] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OpenWeatherResponse.cs | |
// [...] | |
public class Temps | |
{ | |
[JsonPropertyName("temp")] | |
public decimal Temp { get; set; } | |
[JsonPropertyName("feels_like")] | |
public decimal FeelsLike { get; set; } | |
[JsonPropertyName("temp_min")] | |
public decimal TempMin { get; set; } | |
[JsonPropertyName("temp_max")] | |
public decimal TempMax { get; set; } | |
} | |
// [...] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using WeatherWalkingSkeleton.Models; | |
namespace WeatherWalkingSkeleton.Services | |
{ | |
public enum Unit | |
{ | |
Metric, | |
Imperial, | |
Kelvin | |
} | |
public interface IOpenWeatherService | |
{ | |
List<WeatherForecast> GetFiveDayForecast(string location, Unit unit = Unit.Metric); | |
} | |
// [...] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OpenWeatherService : IOpenWeatherService | |
public List<WeatherForecast> GetFiveDayForecast(string location, Unit unit = Unit.Metric) | |
{ | |
return new NotImplementedException(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
// [...] | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// [...] | |
services.AddScoped<IOpenWeatherService, OpenWeatherService>(); | |
// [...] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using WeatherWalkingSkeleton.Services; | |
namespace WeatherWalkingSkeleton.Controllers | |
{ | |
public class WeatherForecastController : ControllerBase | |
{ | |
private readonly IOpenWeatherService _weatherService; | |
public WeatherForecastController(ILogger<WeatherForecastController> logger, IOpenWeatherService weatherService) | |
{ | |
_logger = logger; | |
_weatherService = weatherService; | |
} | |
// [...] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpGet] | |
public IActionResult Get() | |
{ | |
var forecast = _weatherService.GetFiveDayForecast("Chicago"); | |
return Ok(forecast); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Options; | |
using WeatherWalkingSkeleton.Config; | |
// [...] | |
public class OpenWeatherService : IOpenWeatherService | |
{ | |
private OpenWeather _openWeatherConfig; | |
public OpenWeatherService(IOptions<OpenWeather> opts) | |
{ | |
_openWeatherConfig = opts.Value; | |
} | |
// [...] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<WeatherForecast> GetFiveDayForecast(string location, Unit unit = Unit.Metric) | |
{ | |
string url = $"https://api.openweathermap.org/data/2.5/forecast?q={ location }&appid={ _openWeatherConfig.ApiKey }&units={ unit }"; | |
// [...] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using WeatherWalkingSkeleton.Models; | |
// [...] | |
public List<WeatherForecast> GetFiveDayForecast(string location, Unit unit = Unit.Metric) | |
{ | |
string url = $"https://api.openweathermap.org/data/2.5/forecast?q={ location }&appid={ _openWeatherConfig.ApiKey }&units={ unit }"; | |
var forecasts = new List<WeatherForecast>(); | |
return forecasts; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 0. Use the .NET HttpClient library | |
using (HttpClient client = new HttpClient()) | |
{ | |
// 1. Make the request | |
var response = client.GetAsync(url).Result; | |
var json = response.Content.ReadAsStringAsync().Result; | |
// 2. Deserialize the response. | |
var openWeatherResponse = JsonSerializer.Deserialize<OpenWeatherResponse>(json); | |
// 3. Build the list of forecasts | |
foreach (var forecast in openWeatherResponse.Forecasts) | |
{ | |
forecasts.Add(new WeatherForecast | |
{ | |
Date = new DateTime(forecast.Dt), | |
Temp = forecast.Temps.Temp, | |
FeelsLike = forecast.Temps.FeelsLike, | |
TempMin = forecast.Temps.TempMin, | |
TempMax = forecast.Temps.TempMax, | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<WeatherForecast> GetFiveDayForecast(string location, Unit unit = Unit.Metric) | |
{ | |
string url = $"https://api.openweathermap.org/data/2.5/forecast?q={ location }&appid={ _openWeatherConfig.ApiKey }&units={ unit }"; | |
var forecasts = new List<WeatherForecast>(); | |
using (HttpClient client = new HttpClient()) | |
{ | |
var response = client.GetAsync(url).Result; | |
var json = response.Content.ReadAsStringAsync().Result; | |
var openWeatherResponse = JsonSerializer.Deserialize<OpenWeatherResponse>(json); | |
foreach (var forecast in openWeatherResponse.Forecasts) | |
{ | |
forecasts.Add(new WeatherForecast | |
{ | |
Date = new DateTime(forecast.Dt), | |
Temp = forecast.Temps.Temp, | |
FeelsLike = forecast.Temps.FeelsLike, | |
TempMin = forecast.Temps.TempMin, | |
TempMax = forecast.Temps.TempMax, | |
}); | |
} | |
} | |
return forecasts; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IActionResult Get(string location, Unit unit = Unit.Imperial) | |
{ | |
var forecast = _weatherService.GetFiveDayForecast(location, unit); | |
return Ok(forecast); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment