Skip to content

Commit

Permalink
Minor fix in credentials being overriden by SetJob (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul authored Feb 20, 2024
1 parent 7dc7bfc commit 98556fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
20 changes: 20 additions & 0 deletions QuantConnect.Polygon.Tests/PolygonDataQueueHandlerCommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ public void CanInitializeUsingJobPacket()
Config.Set("polygon-api-key", _apiKey);
}

[Test]
public void JobPacketWontOverrideCredentials()
{
var job = new LiveNodePacket
{
BrokerageData = new Dictionary<string, string>() { { "polygon-api-key", "InvalidApiKeyThatWontBeUsed" } }
};

// This should pick up the API key from the configuration
using var polygon = new PolygonDataQueueHandler();

Assert.IsFalse(polygon.IsConnected);

// This should not override the API key
polygon.SetJob(job);

// Since API key was not overriden, this should work
AssertConnection(polygon);
}

private SubscriptionDataConfig GetSubscriptionDataConfig<T>(Symbol symbol, Resolution resolution)
{
return new SubscriptionDataConfig(
Expand Down
14 changes: 11 additions & 3 deletions QuantConnect.Polygon/PolygonDataQueueHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public partial class PolygonDataQueueHandler : IDataQueueHandler
private readonly MarketHoursDatabase _marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
private readonly Dictionary<Symbol, DateTimeZone> _symbolExchangeTimeZones = new();

private bool _initialized;
private bool _disposed;

private bool _unsupportedSecurityTypeMessageLogged;
Expand Down Expand Up @@ -130,6 +131,7 @@ public PolygonDataQueueHandler(string apiKey, int maxSubscriptionsPerWebSocket,
/// </summary>
private void Initialize(int maxSubscriptionsPerWebSocket, bool streamingEnabled = true)
{
_initialized = true;
_dataAggregator = new PolygonAggregationManager();
RestApiClient = new PolygonRestApiClient(_apiKey);
_optionChainProvider = new CachingOptionChainProvider(new PolygonOptionChainProvider(RestApiClient, _symbolMapper));
Expand Down Expand Up @@ -163,12 +165,18 @@ private void Initialize(int maxSubscriptionsPerWebSocket, bool streamingEnabled
/// <param name="job">Job we're subscribing for</param>
public void SetJob(LiveNodePacket job)
{
job.BrokerageData.TryGetValue("polygon-api-key", out _apiKey);
if (string.IsNullOrEmpty(_apiKey))
if (_initialized)
{
throw new ArgumentException("The PolygonDataQueueHandler requires an API key");
return;
}

if (!job.BrokerageData.TryGetValue("polygon-api-key", out var apiKey))
{
throw new ArgumentException("The Polygon.io API key is missing from the brokerage data.");
}

_apiKey = apiKey;

var maxSubscriptionsPerWebSocket = 0;
if (!job.BrokerageData.TryGetValue("polygon-max-subscriptions-per-websocket", out var maxSubscriptionsPerWebSocketStr) ||
!int.TryParse(maxSubscriptionsPerWebSocketStr, out maxSubscriptionsPerWebSocket))
Expand Down

0 comments on commit 98556fa

Please sign in to comment.