WeatherDashboard/WeatherDashboard.Console/Program.cs

40 lines
1.4 KiB
C#
Raw Normal View History

2023-03-19 02:11:53 +00:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Net.Http.Json;
using WeatherDashboard;
2023-03-19 02:11:53 +00:00
var builder = Host.CreateDefaultBuilder(args);
2023-05-21 02:06:16 +00:00
builder.ConfigureAppConfiguration((context, config) =>
2023-03-19 02:11:53 +00:00
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
config.AddEnvironmentVariables();
config.AddJsonFile("appsettings.json");
config.AddJsonFile($"appsettings.{environment}.json", true);
2023-03-19 02:11:53 +00:00
});
2023-05-21 02:06:16 +00:00
builder.ConfigureServices(services =>
2023-03-19 02:11:53 +00:00
{
services.AddHttpClient();
});
var app = builder.Build();
var configuration = app.Services.GetService<IConfiguration>();
var httpClientFactory = app.Services.GetService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
var apiUrl = configuration["ApiUrl"];
2023-05-21 02:06:16 +00:00
var gotifyUrl = configuration["GotifyUrl"];
2023-03-19 02:11:53 +00:00
var weatherSet = await httpClient.GetFromJsonAsync<WeatherSet>(apiUrl);
2023-06-02 20:47:47 +00:00
var todaysForecast = weatherSet.Forecast.FirstOrDefault().Summary;
2023-05-21 02:06:16 +00:00
var notificationResponse = await httpClient.PostAsJsonAsync(gotifyUrl, new
{
title = "Weather Conditions",
2023-06-14 03:38:56 +00:00
message = $"Today: {todaysForecast.ConditionName} with a high of {todaysForecast.HighTemp} and a low of {todaysForecast.LowTemp}. Currently: {weatherSet.Current.ConditionName} and {weatherSet.Current.Temperature} degrees.",
2023-05-21 02:06:16 +00:00
priority = 5
});