Skip to content

Commit

Permalink
Merge pull request Green-Software-Foundation#217 from microsoft/220/M…
Browse files Browse the repository at this point in the history
…oveWattTimeClient

Move WattTime Client to live within WattTime Datasource
  • Loading branch information
vaughanknight authored Jan 10, 2023
2 parents 4ee5b41 + 223dc81 commit 23d2e63
Show file tree
Hide file tree
Showing 32 changed files with 182 additions and 236 deletions.
1 change: 0 additions & 1 deletion docs/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ The current package include 8 projects from the SDK:
6. "CarbonAware.DataSources.Registration"
7. "CarbonAware.DataSources.WattTime"
8. "CarbonAware.LocationSources.Azure"
9. "CarbonAware.Tools.WattTimeClient"

These 8 projects enable users of the library to consume the current endpoints exposed by the library. The package that needs to be added to a new C# project is `GSF.CarbonAware`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<ProjectReference Include="..\..\CarbonAware.DataSources.Mocks\mock\CarbonAware.DataSources.Mocks.csproj" />
<ProjectReference Include="..\..\..\CarbonAware.Tools\CarbonAware.Tools.WattTimeClient\src\CarbonAware.Tools.WattTimeClient.csproj" />
<ProjectReference Include="..\src\CarbonAware.DataSources.WattTime.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CarbonAware.DataSources.Mocks;
using CarbonAware.Tools.WattTimeClient.Constants;
using CarbonAware.Tools.WattTimeClient.Model;
using CarbonAware.DataSources.WattTime.Constants;
using CarbonAware.DataSources.WattTime.Model;
using System.Net;
using System.Net.Mime;
using System.Text.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -9,7 +9,27 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\CarbonAware.Tools\CarbonAware.Tools.WattTimeClient\src\CarbonAware.Tools.WattTimeClient.csproj" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>CarbonAware.DataSources.WattTime.Client.Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>CarbonAware.DataSources.WattTime.Mocks</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>CarbonAware.DataSources.WattTime.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
<PackageReference Include="WireMock.Net" Version="1.4.43"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\CarbonAware.LocationSources\src\CarbonAware.LocationSources.csproj" />
<ProjectReference Include="..\..\..\CarbonAware\src\CarbonAware.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using CarbonAware.Tools.WattTimeClient.Model;
using CarbonAware.DataSources.WattTime.Model;

namespace CarbonAware.Tools.WattTimeClient;
namespace CarbonAware.DataSources.WattTime.Client;

/// <summary>
/// An interface for interacting with the WattTime API.
/// </summary>
public interface IWattTimeClient
{
public const string NamedClient = "WattTimeClient";

/// <summary>
/// Async method to get observed emission data for a given balancing authority and time period.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using Microsoft.Extensions.Options;
using CarbonAware.DataSources.WattTime.Configuration;
using CarbonAware.DataSources.WattTime.Constants;
using CarbonAware.DataSources.WattTime.Model;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using CarbonAware.Tools.WattTimeClient.Model;
using System.Web;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using System.Net.Mime;
using System.Net;
using CarbonAware.Tools.WattTimeClient.Configuration;
using CarbonAware.Tools.WattTimeClient.Constants;
using System.Globalization;
using Microsoft.Extensions.Caching.Memory;

namespace CarbonAware.Tools.WattTimeClient;
namespace CarbonAware.DataSources.WattTime.Client;

public class WattTimeClient : IWattTimeClient
{
private static readonly JsonSerializerOptions options = new JsonSerializerOptions(JsonSerializerDefaults.Web);

private static readonly HttpStatusCode[] RetriableStatusCodes = new HttpStatusCode[]
{
HttpStatusCode.Unauthorized,
HttpStatusCode.Unauthorized,
HttpStatusCode.Forbidden
};

Expand Down Expand Up @@ -291,7 +291,7 @@ private string BuildUrlWithQueryString(string url, IDictionary<string, string> q
// this will get a specialized namevalue collection for formatting query strings.
var query = HttpUtility.ParseQueryString(string.Empty);

foreach(var kvp in queryStringParams)
foreach (var kvp in queryStringParams)
{
query[kvp.Key] = kvp.Value;
}
Expand All @@ -308,7 +308,7 @@ private string BuildUrlWithQueryString(string url, IDictionary<string, string> q

private async Task<BalancingAuthority> GetBalancingAuthorityFromCacheAsync(string latitude, string longitude)
{
var key = new Tuple<string, string>( latitude, longitude );
var key = new Tuple<string, string>(latitude, longitude);
var balancingAuthority = await this.memoryCache.GetOrCreateAsync(key, async entry =>
{
var parameters = new Dictionary<string, string>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CarbonAware.Exceptions;

namespace CarbonAware.Tools.WattTimeClient;
namespace CarbonAware.DataSources.WattTime;

public class WattTimeClientException : CarbonAwareException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CarbonAware.Exceptions;
using CarbonAware.Interfaces;

namespace CarbonAware.Tools.WattTimeClient;
namespace CarbonAware.DataSources.WattTime.Client;

public class WattTimeClientHttpException : CarbonAwareException, IHttpResponseException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using CarbonAware.Configuration;
using CarbonAware.DataSources.WattTime.Client;
using CarbonAware.Exceptions;
using CarbonAware.Interfaces;
using CarbonAware.Tools.WattTimeClient;
using CarbonAware.Tools.WattTimeClient.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

namespace CarbonAware.Tools.WattTimeClient.Configuration;
using CarbonAware.Exceptions;

namespace CarbonAware.DataSources.WattTime.Configuration;

/// <summary>
/// A configuration class for holding WattTime client config values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CarbonAware.Tools.WattTimeClient.Constants;
namespace CarbonAware.DataSources.WattTime.Constants;

internal class AuthenticationHeaderTypes
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CarbonAware.Tools.WattTimeClient.Constants;
namespace CarbonAware.DataSources.WattTime.Constants;

internal class Paths
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CarbonAware.Tools.WattTimeClient.Constants;
namespace CarbonAware.DataSources.WattTime.Constants;

internal class QueryStrings
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace CarbonAware.Tools.WattTimeClient.Model;
namespace CarbonAware.DataSources.WattTime.Model;

/// <summary>
/// The details of the balancing authority (BA) serving a particular location.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace CarbonAware.Tools.WattTimeClient.Model;
namespace CarbonAware.DataSources.WattTime.Model;

/// <summary>
/// An emissions forecast for a given time period.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace CarbonAware.Tools.WattTimeClient.Model;
namespace CarbonAware.DataSources.WattTime.Model;

/// <summary>
/// An object describing the emissions for a given time period and balancing authority.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace CarbonAware.Tools.WattTimeClient.Model;
namespace CarbonAware.DataSources.WattTime.Model;

/// <summary>
/// Serializable object describing the WattTime login response object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using CarbonAware.Exceptions;
using CarbonAware.DataSources.WattTime.Client;
using CarbonAware.DataSources.WattTime.Model;
using CarbonAware.Interfaces;
using CarbonAware.LocationSources.Exceptions;
using CarbonAware.Model;
using CarbonAware.Tools.WattTimeClient;
using CarbonAware.Tools.WattTimeClient.Model;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Globalization;

namespace CarbonAware.DataSources.WattTime;

/// <summary>
/// Reprsents a wattime data source.
/// Represents a WattTime data source.
/// </summary>
public class WattTimeDataSource : IEmissionsDataSource, IForecastDataSource
{
Expand All @@ -35,7 +34,6 @@ public class WattTimeDataSource : IEmissionsDataSource, IForecastDataSource
const double LBS_TO_GRAMS_CONVERSION_FACTOR = 453.59237;
public double MinSamplingWindow => 120; // 2hrs of data


/// <summary>
/// Creates a new instance of the <see cref="WattTimeDataSource"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;

namespace CarbonAware.Tools.WattTimeClient.Tests;
namespace CarbonAware.DataSources.WattTime.Client.Tests;

public static class TestData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using CarbonAware.Tools.WattTimeClient.Configuration;
using CarbonAware.Tools.WattTimeClient.Model;
using CarbonAware.DataSources.WattTime.Configuration;
using CarbonAware.DataSources.WattTime.Model;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -18,7 +15,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace CarbonAware.Tools.WattTimeClient.Tests;
namespace CarbonAware.DataSources.WattTime.Client.Tests;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class WattTimeClientTests
Expand Down Expand Up @@ -521,40 +518,6 @@ public async Task GetHistoricalDataAsync_RefreshesTokenWhenNoneSet()
}
}

[Test]
public void TestClient_With_Proxy_Failure()
{
var key1 = $"{CarbonAwareVariablesConfiguration.Key}:Proxy:UseProxy";
var key2 = $"{CarbonAwareVariablesConfiguration.Key}:Proxy:Url";
var settings = new Dictionary<string, string> {
{key1, "true"},
{key2, "http://fakeproxy:8080"},
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(settings)
.Build();
var serviceCollection = new ServiceCollection();
serviceCollection.ConfigureWattTimeClient(configuration);
serviceCollection.AddMemoryCache();
var serviceProvider = serviceCollection.BuildServiceProvider();
var client = serviceProvider.GetRequiredService<IWattTimeClient>();
Assert.ThrowsAsync<HttpRequestException>(async () => await client.GetBalancingAuthorityAsync("lat", "long"));
}

[Test]
public void TestClient_With_Missing_Proxy_URL()
{
var key1 = $"{CarbonAwareVariablesConfiguration.Key}:Proxy:UseProxy";
var settings = new Dictionary<string, string> {
{key1, "true"},
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(settings)
.Build();
var serviceCollection = new ServiceCollection();
Assert.Throws<ConfigurationException>(() => serviceCollection.ConfigureWattTimeClient(configuration));
}

private void CreateHttpClient(Func<HttpRequestMessage, Task<HttpResponseMessage>> requestDelegate)
{
this.MessageHandler = new MockHttpMessageHandler(requestDelegate);
Expand Down Expand Up @@ -609,5 +572,4 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
Loading

0 comments on commit 23d2e63

Please sign in to comment.