Skip to content

Commit

Permalink
Implement Dynamic DataSource registration
Browse files Browse the repository at this point in the history
Signed-off-by: Bill DeRusha <[email protected]>
  • Loading branch information
bderusha committed Jun 29, 2023
1 parent b3b3189 commit fbbd9af
Show file tree
Hide file tree
Showing 50 changed files with 671 additions and 610 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"uriFormat": "%s/swagger"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE": "false"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
Expand Down
4 changes: 4 additions & 0 deletions src/CarbonAware.CLI/src/CarbonAware.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<ProjectReference Include="..\..\CarbonAware\src\CarbonAware.csproj" />
<ProjectReference Include="..\..\CarbonAware.LocationSources\src\CarbonAware.LocationSources.csproj" />
<ProjectReference Include="..\..\GSF.CarbonAware\src\GSF.CarbonAware.csproj" />
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.Json\src\CarbonAware.DataSources.Json.csproj" />
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.WattTime\src\CarbonAware.DataSources.WattTime.csproj" />
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.ElectricityMaps\src\CarbonAware.DataSources.ElectricityMaps.csproj" />
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.ElectricityMapsFree\src\CarbonAware.DataSources.ElectricityMapsFree.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<ItemGroup>
<ProjectReference
Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.Json\mock\CarbonAware.DataSources.Json.Mocks.csproj" />
<ProjectReference
Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.Registration\CarbonAware.DataSources.Registration.csproj" />
<ProjectReference
Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.WattTime\mock\CarbonAware.DataSources.WattTime.Mocks.csproj" />
<ProjectReference
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using CarbonAware.DataSources.Configuration;
using NUnit.Framework;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CarbonAware.DataSources.Configuration;
using NUnit.Framework;
using NUnit.Framework;
using System.Text.Json.Nodes;

namespace CarbonAware.CLI.IntegrationTests.Commands.EmissionsForecasts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CarbonAware.DataSources.Configuration;
using NUnit.Framework;
using NUnit.Framework;
using System.Text.Json;

namespace CarbonAware.CLI.IntegrationTests.Commands.Location;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CarbonAware.DataSources.Configuration;
using CarbonAware.Interfaces;
using CarbonAware.Interfaces;
using CarbonAware.DataSources.ElectricityMaps.Mocks;
using CarbonAware.DataSources.ElectricityMapsFree.Mocks;
using CarbonAware.DataSources.Json.Mocks;
Expand All @@ -11,6 +10,15 @@

namespace CarbonAware.CLI.IntegrationTests;

public enum DataSourceType
{
None,
WattTime,
JSON,
ElectricityMaps,
ElectricityMapsFree,
}

/// <summary>
/// A base class that does all the common setup for the Integration Testing
/// Overrides WebAPI factory by switching out different configurations via _datasource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<ItemGroup>
<InternalsVisibleTo Include="CarbonAware.DataSources.ElectricityMaps.Mocks" />
<InternalsVisibleTo Include="CarbonAware.DataSources.ElectricityMaps.Tests" />
<InternalsVisibleTo Include="CarbonAware.DataSources.Registration" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using CarbonAware.Configuration;
using CarbonAware.DataSources.ElectricityMaps.Client;
using CarbonAware.DataSources.ElectricityMaps.Configuration;
using CarbonAware.DataSources.ElectricityMaps.Model;
using CarbonAware.Exceptions;
using CarbonAware.Interfaces;
using CarbonAware.Model;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Net;

namespace CarbonAware.DataSources.ElectricityMaps;

Expand Down Expand Up @@ -40,6 +45,21 @@ public ElectricityMapsDataSource(ILogger<ElectricityMapsDataSource> logger, IEle
this._locationSource = locationSource;
}

public static IServiceCollection ConfigureDI<T>(IServiceCollection services, DataSourcesConfiguration dataSourcesConfig)
where T : IDataSource
{
var configSection = dataSourcesConfig.ConfigurationSection<T>();
AddElectricityMapsClient(services, configSection);
try
{
services.TryAddSingleton(typeof(T), typeof(ElectricityMapsDataSource));
} catch (Exception ex)
{
throw new ArgumentException($"ElectricityMapsDataSource is not a supported {typeof(T).Name} data source.", ex);
}
return services;
}

/// <inheritdoc />
public async Task<EmissionsForecast> GetCurrentCarbonIntensityForecastAsync(Location location)
{
Expand Down Expand Up @@ -176,4 +196,33 @@ private TimeSpan GetDurationFromHistoryDataPoints(IEnumerable<CarbonIntensity> d
// the absolute value of the TimeSpan between the two points.
return first.DateTime.Subtract(second.DateTime).Duration();
}

private static void AddElectricityMapsClient(IServiceCollection services, IConfigurationSection configSection)
{
services.Configure<ElectricityMapsClientConfiguration>(c =>
{
configSection.Bind(c);
});

var httpClientBuilder = services.AddHttpClient<ElectricityMapsClient>(IElectricityMapsClient.NamedClient);

var Proxy = configSection.GetSection("Proxy").Get<WebProxyConfiguration>();
if (Proxy?.UseProxy == true)
{
if (String.IsNullOrEmpty(Proxy.Url))
{
throw new ConfigurationException("Proxy Url is not configured.");
}
httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() =>
new HttpClientHandler() {
Proxy = new WebProxy {
Address = new Uri(Proxy.Url),
Credentials = new NetworkCredential(Proxy.Username, Proxy.Password),
BypassProxyOnLocal = true
}
}
);
}
services.TryAddSingleton<IElectricityMapsClient, ElectricityMapsClient>();
}
}

This file was deleted.

Loading

0 comments on commit fbbd9af

Please sign in to comment.