I am trying to develop a weather Nuget package that can be used in any .netcore project, the library should give me the current weatherfor a specific city that I chose in Celsius or Fahrenheit.
I will use the weatherapi.com to get the current weather.
The library should support the dependency injection (DI) throw a service calledIGetCurrentWeather with one method that’s take a city name and optionalparameter to chose whether I want the result in Celsius or Fahrenheit.
I should be able to register the library in the startup of any .NET CORE project andprovide the necessary configurations like the image below
the interface:
public interface IGetCurrentWeather { public async Task<string> GetCurrentWeather() { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("http://api.weatherapi.com/v1/current.json?key=d1398cba8a894feb9f7180821232602&q=London&aqi=no\r\n"); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } }
AddWeather Function:
public static class WeatherApi { public static WeatherOptions Options { get; } //set only via Secret Manager public static IServiceCollection AddWeatherApi(this IServiceCollection services, Action<WeatherOptions> configure) { services.Configure(configure) ; return services; }
WeatherOptions:
public class WeatherOptions { public string ApiKey { get; set; } }
how can i use the ApiKey when call GetCurrentWeather() function to access weatherapi.com account?