basic weather request working

This commit is contained in:
Ryan Peters 2023-02-09 22:33:02 -05:00
parent a1081dcb2b
commit 3d0a0bd7cd
17 changed files with 118 additions and 94 deletions

View File

@ -7,6 +7,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WeatherDashboard\WeatherDashboard.csproj" />
</ItemGroup>

View File

@ -1,21 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using WeatherDashboard.Web.Models;
using WeatherDashboard.Web.Services;
namespace WeatherDashboard.Web.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IForecastService forecastService;
public HomeController(ILogger<HomeController> logger)
public HomeController(IForecastService forecastService)
{
_logger = logger;
this.forecastService = forecastService;
}
public IActionResult Index()
public async Task<IActionResult> Index()
{
return View();
var weather = await forecastService.GetWeatherAsync();
return View(weather);
}
public IActionResult Privacy()

View File

@ -1,7 +1,11 @@
using WeatherDashboard.Web.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient();
builder.Services.AddTransient<IForecastService, WeatherApiForecastService>();
var app = builder.Build();

View File

@ -0,0 +1,9 @@
using System.Text.Json;
namespace WeatherDashboard.Web.Services
{
public interface IForecastService
{
Task<WeatherSet> GetWeatherAsync();
}
}

View File

@ -0,0 +1,36 @@
using Newtonsoft.Json;
namespace WeatherDashboard.Web.Services
{
public class WeatherApiForecastService : IForecastService
{
private readonly IConfiguration configuration;
private readonly string? apiBaseUrl;
private readonly string? apiKey;
private readonly HttpClient httpClient;
public WeatherApiForecastService(IConfiguration configuration, IHttpClientFactory httpClientFactory)
{
this.configuration = configuration;
apiBaseUrl = configuration["ApiBaseUrl"];
apiKey = configuration["ApiKey"];
httpClient = httpClientFactory.CreateClient();
}
public Task<WeatherSet> GetWeatherAsync() => InvokeRequestAsync<WeatherSet>("v1/forecast.json");
private async Task<T> InvokeRequestAsync<T>(string method)
{
var url = new UriBuilder(apiBaseUrl);
url.Path = method;
url.Query = $"key={apiKey}&q=21144";
var response = await httpClient.GetStringAsync(url.ToString());
return JsonConvert.DeserializeObject<T>(response);
}
}
}

View File

@ -1,8 +1,12 @@
@{
@model WeatherSet
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<div>
@Model.Location.Region, @Model.Location.Name
</div>
<div>
@Model.Current.LastUpdated @Model.Current.Temperature
</div>

View File

@ -4,43 +4,20 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WeatherDashboard.Web</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@100;300;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/WeatherDashboard.Web.styles.css" asp-append-version="true" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">WeatherDashboard.Web</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2023 - WeatherDashboard.Web - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>

View File

@ -1,48 +0,0 @@
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
for details on configuring this project to bundle and minify static web assets. */
a.navbar-brand {
white-space: normal;
text-align: center;
word-break: break-all;
}
a {
color: #0077cc;
}
.btn-primary {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
.border-top {
border-top: 1px solid #e5e5e5;
}
.border-bottom {
border-bottom: 1px solid #e5e5e5;
}
.box-shadow {
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
}
button.accept-policy {
font-size: 1rem;
line-height: inherit;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
white-space: nowrap;
line-height: 60px;
}

View File

@ -1,3 +1,4 @@
@using WeatherDashboard.Web
@using WeatherDashboard
@using WeatherDashboard.Web
@using WeatherDashboard.Web.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

@ -6,6 +6,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WeatherDashboard\WeatherDashboard.csproj" />
</ItemGroup>

View File

@ -5,5 +5,7 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ApiBaseUrl": "https://api.weatherapi.com",
"ApiKey": "829fbbe5beb2424aa1021928230702"
}

View File

@ -18,5 +18,6 @@ html {
}
body {
margin-bottom: 60px;
font-family: 'Montserrat', sans-serif;
color: #777;
}

View File

@ -1,7 +0,0 @@
namespace WeatherDashboard
{
public class Class1
{
}
}

View File

@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace WeatherDashboard
{
public class CurrentForecast
{
[JsonProperty("last_updated")]
public DateTime LastUpdated { get; set; }
[JsonProperty("temp_f")]
public decimal Temperature { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace WeatherDashboard
{
public class Location
{
public string Name { get; set; }
public string Region { get; set; }
public string Country { get; set; }
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,8 @@
namespace WeatherDashboard
{
public class WeatherSet
{
public Location Location { get; set; }
public CurrentForecast Current { get; set; }
}
}