2023-03-19 02:11:53 +00:00
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Hosting ;
2023-03-19 03:00:14 +00:00
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
{
2023-03-19 03:00:14 +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
2023-03-19 03:00:14 +00:00
var weatherSet = await httpClient . GetFromJsonAsync < WeatherSet > ( apiUrl ) ;
2023-06-02 20:47:47 +00:00
var todaysForecast = weatherSet . Forecast . FirstOrDefault ( ) . Summary ;
2023-03-19 03:00:14 +00:00
2023-05-21 02:06:16 +00:00
var notificationResponse = await httpClient . PostAsJsonAsync ( gotifyUrl , new
{
title = "Weather Conditions" ,
2023-06-02 20:47:47 +00:00
message = $"Today will be {todaysForecast.ConditionName} a high of {todaysForecast.HighTemp} and a low of {todaysForecast.LowTemp}. Currently, it is {weatherSet.Current.ConditionName} and {weatherSet.Current.Temperature} degrees." ,
2023-05-21 02:06:16 +00:00
priority = 5
} ) ;