Skip to content

Commit 68e6f35

Browse files
committed
Implement Dynamic DataSource registration
Signed-off-by: Bill DeRusha <[email protected]>
1 parent b3b3189 commit 68e6f35

File tree

50 files changed

+671
-610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+671
-610
lines changed

Diff for: .vscode/launch.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"uriFormat": "%s/swagger"
3838
},
3939
"env": {
40-
"ASPNETCORE_ENVIRONMENT": "Development"
40+
"ASPNETCORE_ENVIRONMENT": "Development",
41+
"DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE": "false"
4142
},
4243
"sourceFileMap": {
4344
"/Views": "${workspaceFolder}/Views"

Diff for: src/CarbonAware.CLI/src/CarbonAware.CLI.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<ProjectReference Include="..\..\CarbonAware\src\CarbonAware.csproj" />
2828
<ProjectReference Include="..\..\CarbonAware.LocationSources\src\CarbonAware.LocationSources.csproj" />
2929
<ProjectReference Include="..\..\GSF.CarbonAware\src\GSF.CarbonAware.csproj" />
30+
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.JSON\src\CarbonAware.DataSources.JSON.csproj" />
31+
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.WattTime\src\CarbonAware.DataSources.WattTime.csproj" />
32+
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.ElectricityMaps\src\CarbonAware.DataSources.ElectricityMaps.csproj" />
33+
<ProjectReference Include="..\..\CarbonAware.DataSources\CarbonAware.DataSources.ElectricityMapsFree\src\CarbonAware.DataSources.ElectricityMapsFree.csproj" />
3034
</ItemGroup>
3135

3236
<ItemGroup>

Diff for: src/CarbonAware.CLI/test/integrationTests/CarbonAware.CLI.IntegrationTests.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
<ItemGroup>
1616
<ProjectReference
1717
Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.Json\mock\CarbonAware.DataSources.Json.Mocks.csproj" />
18-
<ProjectReference
19-
Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.Registration\CarbonAware.DataSources.Registration.csproj" />
2018
<ProjectReference
2119
Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.WattTime\mock\CarbonAware.DataSources.WattTime.Mocks.csproj" />
2220
<ProjectReference

Diff for: src/CarbonAware.CLI/test/integrationTests/Commands/Emissions/EmissionsCommandTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CarbonAware.DataSources.Configuration;
21
using NUnit.Framework;
32
using System.Text.Json.Nodes;
43
using System.Text.RegularExpressions;

Diff for: src/CarbonAware.CLI/test/integrationTests/Commands/EmissionsForecasts/EmissionsForecastsCommandTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CarbonAware.DataSources.Configuration;
2-
using NUnit.Framework;
1+
using NUnit.Framework;
32
using System.Text.Json.Nodes;
43

54
namespace CarbonAware.CLI.IntegrationTests.Commands.EmissionsForecasts;

Diff for: src/CarbonAware.CLI/test/integrationTests/Commands/Location/LocationCommandTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CarbonAware.DataSources.Configuration;
2-
using NUnit.Framework;
1+
using NUnit.Framework;
32
using System.Text.Json;
43

54
namespace CarbonAware.CLI.IntegrationTests.Commands.Location;

Diff for: src/CarbonAware.CLI/test/integrationTests/IntegrationTestingBase.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CarbonAware.DataSources.Configuration;
2-
using CarbonAware.Interfaces;
1+
using CarbonAware.Interfaces;
32
using CarbonAware.DataSources.ElectricityMaps.Mocks;
43
using CarbonAware.DataSources.ElectricityMapsFree.Mocks;
54
using CarbonAware.DataSources.Json.Mocks;
@@ -11,6 +10,15 @@
1110

1211
namespace CarbonAware.CLI.IntegrationTests;
1312

13+
public enum DataSourceType
14+
{
15+
None,
16+
WattTime,
17+
JSON,
18+
ElectricityMaps,
19+
ElectricityMapsFree,
20+
}
21+
1422
/// <summary>
1523
/// A base class that does all the common setup for the Integration Testing
1624
/// Overrides WebAPI factory by switching out different configurations via _datasource

Diff for: src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/CarbonAware.DataSources.ElectricityMaps.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<ItemGroup>
1111
<InternalsVisibleTo Include="CarbonAware.DataSources.ElectricityMaps.Mocks" />
1212
<InternalsVisibleTo Include="CarbonAware.DataSources.ElectricityMaps.Tests" />
13-
<InternalsVisibleTo Include="CarbonAware.DataSources.Registration" />
1413
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
1514
</ItemGroup>
1615

Diff for: src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/Configuration/ServiceCollectionExtensions.cs

-56
This file was deleted.

Diff for: src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/ElectricityMapsDataSource.cs

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
using CarbonAware.Configuration;
12
using CarbonAware.DataSources.ElectricityMaps.Client;
3+
using CarbonAware.DataSources.ElectricityMaps.Configuration;
24
using CarbonAware.DataSources.ElectricityMaps.Model;
35
using CarbonAware.Exceptions;
46
using CarbonAware.Interfaces;
57
using CarbonAware.Model;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.DependencyInjection.Extensions;
611
using Microsoft.Extensions.Logging;
7-
using System.Diagnostics;
12+
using System.Net;
813

914
namespace CarbonAware.DataSources.ElectricityMaps;
1015

@@ -40,6 +45,21 @@ public ElectricityMapsDataSource(ILogger<ElectricityMapsDataSource> logger, IEle
4045
this._locationSource = locationSource;
4146
}
4247

48+
public static IServiceCollection ConfigureDI<T>(IServiceCollection services, DataSourcesConfiguration dataSourcesConfig)
49+
where T : IDataSource
50+
{
51+
var configSection = dataSourcesConfig.ConfigurationSection<T>();
52+
AddElectricityMapsClient(services, configSection);
53+
try
54+
{
55+
services.TryAddSingleton(typeof(T), typeof(ElectricityMapsDataSource));
56+
} catch (Exception ex)
57+
{
58+
throw new ArgumentException($"ElectricityMapsDataSource is not a supported {typeof(T).Name} data source.", ex);
59+
}
60+
return services;
61+
}
62+
4363
/// <inheritdoc />
4464
public async Task<EmissionsForecast> GetCurrentCarbonIntensityForecastAsync(Location location)
4565
{
@@ -176,4 +196,33 @@ private TimeSpan GetDurationFromHistoryDataPoints(IEnumerable<CarbonIntensity> d
176196
// the absolute value of the TimeSpan between the two points.
177197
return first.DateTime.Subtract(second.DateTime).Duration();
178198
}
199+
200+
private static void AddElectricityMapsClient(IServiceCollection services, IConfigurationSection configSection)
201+
{
202+
services.Configure<ElectricityMapsClientConfiguration>(c =>
203+
{
204+
configSection.Bind(c);
205+
});
206+
207+
var httpClientBuilder = services.AddHttpClient<ElectricityMapsClient>(IElectricityMapsClient.NamedClient);
208+
209+
var Proxy = configSection.GetSection("Proxy").Get<WebProxyConfiguration>();
210+
if (Proxy?.UseProxy == true)
211+
{
212+
if (String.IsNullOrEmpty(Proxy.Url))
213+
{
214+
throw new ConfigurationException("Proxy Url is not configured.");
215+
}
216+
httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() =>
217+
new HttpClientHandler() {
218+
Proxy = new WebProxy {
219+
Address = new Uri(Proxy.Url),
220+
Credentials = new NetworkCredential(Proxy.Username, Proxy.Password),
221+
BypassProxyOnLocal = true
222+
}
223+
}
224+
);
225+
}
226+
services.TryAddSingleton<IElectricityMapsClient, ElectricityMapsClient>();
227+
}
179228
}

Diff for: src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/Configuration/ServiceCollectionExtensionTests.cs

-81
This file was deleted.

0 commit comments

Comments
 (0)