Skip to content

Commit

Permalink
Refactor Weather Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Torkelsen committed Nov 21, 2024
1 parent 23ce658 commit 23bd680
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
/handlenett-backend/web-api/HandlenettAPI/bin/Release/net9.0
/handlenett-backend/web-api/HandlenettAPITests/obj
/handlenett-backend/web-api/HandlenettAPITests/bin/Release/net9.0
/handlenett-backend/web-api/HandlenettAPITests/bin/Debug/net9.0
33 changes: 17 additions & 16 deletions handlenett-backend/web-api/HandlenettAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Microsoft.Graph.ExternalConnectors;
using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models;
using StackExchange.Redis;
using System.Configuration;
using System.Net.Http.Headers;

var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -53,15 +55,6 @@
});
});

//Inject specific HttpClients
builder.Services.AddHttpClient<WeatherService>();
builder.Services.AddHttpClient("SlackClient", client =>
{
client.BaseAddress = new Uri("https://slack.com/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
});

var keyVaultName = builder.Configuration["AzureKeyVaultNameProd"];
if (string.IsNullOrEmpty(keyVaultName))
{
Expand All @@ -87,9 +80,7 @@
.AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph"))
.AddInMemoryTokenCaches();



//Dependency Service Injections
//Dependency Injection
builder.Services.AddScoped<SlackService>();
builder.Services.AddScoped<ICosmosDBService>(provider =>
{
Expand All @@ -101,9 +92,19 @@
settings.ContainerName
);
});

//builder.Services.AddSingleton(graphClient); // A single instance is shared across the entire application lifetime, do not use for databaseconnections (maybe just a static cache)
//TODO: add AzureSQLContext singleton
var redisConnString = builder.Configuration.GetConnectionString("AzureRedisCache") ?? throw new InvalidOperationException("Missing redis config");
builder.Services.AddSingleton<IConnectionMultiplexer>(sp => ConnectionMultiplexer.Connect(redisConnString)); //heavy resource and is designed to be reused
builder.Services.AddHttpClient<WeatherService>();
builder.Services.AddHttpClient("SlackClient", client =>
{
client.BaseAddress = new Uri("https://slack.com/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
});
//builder.Services.AddSingleton(); // A single instance is shared across the entire application lifetime
//builder.Services.AddHttpClient<T>() //DI container registers T as a transient service by default.
//builder.Services.AddScoped<T>() // T
//TODO: add AzureSQLContext



Expand Down Expand Up @@ -133,7 +134,7 @@
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API v1");
c.OAuthClientId("6409e25f-f9b7-4b70-a84c-6c077440d740");
c.OAuthClientId(builder.Configuration["AzureAd:ClientId"]);
c.OAuthAppName("Your API Swagger");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ namespace HandlenettAPI.Services
public class WeatherService
{
private readonly HttpClient _httpClient;
private readonly ConnectionMultiplexer _redis;
private readonly IConnectionMultiplexer _redis;

public WeatherService(HttpClient httpClient, IConfiguration config)
public WeatherService(HttpClient httpClient, IConnectionMultiplexer redis)
{
_httpClient = httpClient;
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Miles Haugesund Handlenett/1.0 ([email protected])");
var redisConnString = config.GetConnectionString("AzureRedisCache") ?? throw new InvalidOperationException("Missing redis config");
_redis = ConnectionMultiplexer.Connect(redisConnString);

_redis = redis;
}

public async Task<Weather> GetWeatherByCoordinatesAsync(double latitude, double longitude)
Expand Down

0 comments on commit 23bd680

Please sign in to comment.