Skip to content

Instantly share code, notes, and snippets.

@jsheridanwells
Created June 18, 2020 16:38
Show Gist options
  • Save jsheridanwells/b4306d62dc2e33d0255d110b67b7286f to your computer and use it in GitHub Desktop.
Save jsheridanwells/b4306d62dc2e33d0255d110b67b7286f to your computer and use it in GitHub Desktop.
weather-walking-skeleton-part_1
namespace WeatherWalkingSkeleton.Config
{
public class OpenWeather
{
public string ApiKey { get; set; }
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>65988f0a-26ed-44ef-8749-f86a2f5c18a9</UserSecretsId>
</PropertyGroup>
</Project>
// 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);
// [...]
}
// 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; }
}
}
// 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]
}
// OpenWeatherResponse.cs
// [...]
public class Forecast
{
[JsonPropertyName("dt")]
public int Dt { get; set; }
[JsonPropertyName("main")]
public Temps Temps { get; set; }
}
// [...]
// 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; }
}
// [...]
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);
}
// [...]
}
public class OpenWeatherService : IOpenWeatherService
public List<WeatherForecast> GetFiveDayForecast(string location, Unit unit = Unit.Metric)
{
return new NotImplementedException();
}
public class Startup
{
// [...]
public void ConfigureServices(IServiceCollection services)
{
// [...]
services.AddScoped<IOpenWeatherService, OpenWeatherService>();
// [...]
}
}
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;
}
// [...]
}
}
[HttpGet]
public IActionResult Get()
{
var forecast = _weatherService.GetFiveDayForecast("Chicago");
return Ok(forecast);
}
}
using Microsoft.Extensions.Options;
using WeatherWalkingSkeleton.Config;
// [...]
public class OpenWeatherService : IOpenWeatherService
{
private OpenWeather _openWeatherConfig;
public OpenWeatherService(IOptions<OpenWeather> opts)
{
_openWeatherConfig = opts.Value;
}
// [...]
}
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 }";
// [...]
}
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;
}
// 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,
});
}
}
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;
}
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