Skip to content

Commit b443e9e

Browse files
committed
Added base url to the configuration with validation
Added AuthenticationBaseUrl to the configuration and updated Authentication to leverage the v3 authenication path - note the API is not updated and will require further updates.
1 parent e4f1494 commit b443e9e

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

casdk-docs/docs/architecture/decisions/0016-watt-time-v3.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The following is configured at `CarbonAware.DataSources.WattTime/src/Constants/
3838
| Forecast | Get forecast| /forecast | /forecast | **TODO: CHECK IMPACT** <br> No longer be used for historical data <br> _Request_ <li> `ba` is now `region` <li> `extended_forecast` removed <li> `horizon_hours` added <li> `signal_type` added <li> Historical forecasts are now at `/forecast/historical` <br> _Response_ <li> `signal_type` added
3939
| Historical | Get historical forecast data | /historical (?) | /forecast/historical (?) | **We need to validate why historical was being used for the API, and what historical used to be, and whether this should be the new /forecast/historical or not.**
4040
| Balancing Authority From Location | Get balancing authority from location | /ba-from-loc | /region-from-loc | Check if the CA SDK uses BA at all <br><br> _Request_ <li> `name` is now `region_full_name` <li> `abbrev` is now `region` <li> `signal_type` added <br> _Response_ <li> `id` removed <li> `signal_type` added |
41-
| Login | User login | https://api2.watttime.org/v2/login | https://api.watttime.org/login | Path has changed from being version specific to being unique from the API version. <br><br> **TODO: CHECK HOW BASE URL IS DEFINED AS THIS WILL NOW HAVE DIFFERENT VALUES**
41+
| Login | User login | https://api2.watttime.org/v2/login | https://api.watttime.org/login | Path has changed from being version specific to being no longer related to the API version. <br><br> **TODO: CHECK HOW BASE URL IS DEFINED AS THIS WILL NOW HAVE DIFFERENT VALUES**
4242

4343
### Query Strings
4444

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Client/IWattTimeClient.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace CarbonAware.DataSources.WattTime.Client;
88
internal interface IWattTimeClient
99
{
1010
public const string NamedClient = "WattTimeClient";
11+
public const string NamedAuthenticationClient = "WattTimeAuthenticationClient"
1112

1213
/// <summary>
1314
/// Async method to get observed emission data for a given balancing authority and time period.

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Client/WattTimeClient.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal class WattTimeClient : IWattTimeClient
2525
};
2626

2727
private HttpClient _client;
28+
private HttpClient _authenticationClient;
2829

2930
private IOptionsMonitor<WattTimeClientConfiguration> _configurationMonitor { get; }
3031

@@ -37,12 +38,19 @@ internal class WattTimeClient : IWattTimeClient
3738
public WattTimeClient(IHttpClientFactory factory, IOptionsMonitor<WattTimeClientConfiguration> configurationMonitor, ILogger<WattTimeClient> log, IMemoryCache memoryCache)
3839
{
3940
_client = factory.CreateClient(IWattTimeClient.NamedClient);
41+
_authenticationClient = factory.CreateClient(IWattTimeClient.NamedAuthenticationClient);
42+
4043
_configurationMonitor = configurationMonitor;
4144
_log = log;
4245
_configuration.Validate();
4346
_client.BaseAddress = new Uri(this._configuration.BaseUrl);
4447
_client.DefaultRequestHeaders.Accept.Clear();
4548
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
49+
50+
_authenticationClient.BaseAddress = new Uri(this._configuration.AuthenticationBaseUrl);
51+
_authenticationClient.DefaultRequestHeaders.Accept.Clear();
52+
_authenticationClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
53+
4654
_memoryCache = memoryCache;
4755
}
4856

@@ -215,7 +223,7 @@ private async Task UpdateAuthTokenAsync()
215223
_log.LogInformation("Attempting to log in user {username}", this._configuration.Username);
216224

217225
this.SetBasicAuthenticationHeader();
218-
HttpResponseMessage response = await this._client.GetAsync(Paths.Login);
226+
HttpResponseMessage response = await this._authenticationClient.GetAsync(Paths.Login);
219227

220228
LoginResult? data = null;
221229

@@ -239,6 +247,7 @@ private void SetBasicAuthenticationHeader()
239247
{
240248
var authToken = Encoding.UTF8.GetBytes($"{this._configuration.Username}:{this._configuration.Password}");
241249
this._client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthenticationHeaderTypes.Basic, Convert.ToBase64String(authToken));
250+
this._authenticationClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthenticationHeaderTypes.Basic, Convert.ToBase64String(authToken));
242251
}
243252

244253
internal void SetBearerAuthenticationHeader(string token)

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Configuration/WattTimeClientConfiguration.cs

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ internal class WattTimeClientConfiguration
2525
/// </summary>
2626
public string BaseUrl { get; set; } = "https://api2.watttime.org/v2/";
2727

28+
/// <summary>
29+
/// Authentication base url. This changed between v2 and v3
30+
/// to be different to the API base url.
31+
/// </summary>
32+
public string AuthenticationBaseUrl { get; set; } = "https://api.watttime.org/";
33+
2834
/// <summary>
2935
/// Gets or sets the cached expiration time (in seconds) for a BalancingAuthority instance.
3036
/// It defaults to 86400 secs.
@@ -51,6 +57,11 @@ public void Validate()
5157
throw new ConfigurationException($"{Key}:{nameof(this.BaseUrl)} is not a valid absolute url.");
5258
}
5359

60+
if (!Uri.IsWellFormedUriString(this.AuthenticationBaseUrl, UriKind.Absolute))
61+
{
62+
throw new ConfigurationException($"{Key}:{nameof(this.AuthenticationBaseUrl)} is not a valid absolute url.");
63+
}
64+
5465
// Validate credential encoding/decoding with UTF8
5566
if (!Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(this.Username)).Equals(this.Username))
5667
{

0 commit comments

Comments
 (0)