From 8b78a180a4222838d2c93f06c22a09f13672d77b Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 11 May 2025 15:56:38 -0700 Subject: [PATCH] Save point --- Polygon.Client/AllTickersRequest.cs | 16 +++++ Polygon.Client/AllTickersResponse.cs | 9 +++ Polygon.Client/AllTickersResponseItem.cs | 3 + Polygon.Client/MarketEnum.cs | 13 +++++ Polygon.Client/OrderEnum.cs | 7 +++ Polygon.Client/PolygonClient.AllTickers.cs | 68 ++++++++++++++++++++++ Polygon.Client/PolygonClient.cs | 22 ++++++- Polygon.Client/SortEnum.cs | 20 +++++++ 8 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 Polygon.Client/AllTickersRequest.cs create mode 100644 Polygon.Client/AllTickersResponse.cs create mode 100644 Polygon.Client/AllTickersResponseItem.cs create mode 100644 Polygon.Client/MarketEnum.cs create mode 100644 Polygon.Client/OrderEnum.cs create mode 100644 Polygon.Client/PolygonClient.AllTickers.cs create mode 100644 Polygon.Client/SortEnum.cs diff --git a/Polygon.Client/AllTickersRequest.cs b/Polygon.Client/AllTickersRequest.cs new file mode 100644 index 0000000..47fde89 --- /dev/null +++ b/Polygon.Client/AllTickersRequest.cs @@ -0,0 +1,16 @@ +namespace Polygon.Client; + +public record AllTickersRequest( + string Ticker, + string Type, + Market Market, + string Exchange, + string Cusip, + string Cik, + DateOnly? Date, + string Search, + bool Active, + Order Order, + int Limit, + Sort Sort +); diff --git a/Polygon.Client/AllTickersResponse.cs b/Polygon.Client/AllTickersResponse.cs new file mode 100644 index 0000000..cad01df --- /dev/null +++ b/Polygon.Client/AllTickersResponse.cs @@ -0,0 +1,9 @@ +namespace Polygon.Client; + +public record class AllTickersResponse( + int Count, + string? Next_Url, + string? Request_Id, + IEnumerable Results, + string Status +); diff --git a/Polygon.Client/AllTickersResponseItem.cs b/Polygon.Client/AllTickersResponseItem.cs new file mode 100644 index 0000000..2ff35dd --- /dev/null +++ b/Polygon.Client/AllTickersResponseItem.cs @@ -0,0 +1,3 @@ +namespace Polygon.Client; + +public record AllTickersResponseItem(); diff --git a/Polygon.Client/MarketEnum.cs b/Polygon.Client/MarketEnum.cs new file mode 100644 index 0000000..51e5b5d --- /dev/null +++ b/Polygon.Client/MarketEnum.cs @@ -0,0 +1,13 @@ +namespace Polygon.Client; + +/// +/// Market type. +/// +public enum Market +{ + Stock, + Crypto, + Fx, + Otc, + Indices +} diff --git a/Polygon.Client/OrderEnum.cs b/Polygon.Client/OrderEnum.cs new file mode 100644 index 0000000..04ecf4a --- /dev/null +++ b/Polygon.Client/OrderEnum.cs @@ -0,0 +1,7 @@ +namespace Polygon.Client; + +public enum Order +{ + Asc, + Desc +} \ No newline at end of file diff --git a/Polygon.Client/PolygonClient.AllTickers.cs b/Polygon.Client/PolygonClient.AllTickers.cs new file mode 100644 index 0000000..33671a1 --- /dev/null +++ b/Polygon.Client/PolygonClient.AllTickers.cs @@ -0,0 +1,68 @@ +namespace Polygon.Client; + +using System.Net; +using System.Text.Json; + +/// +/// The PolygonClient class provides methods to interact with the Polygon API. +/// +public partial class PolygonClient +{ + /// + /// Get all the ticker symbols. + /// + /// AllTickersRequest. + /// CancellationToken. + /// + public async Task GetAllTickersAsync( + AllTickersRequest request, + CancellationToken cancellationToken = default + ) + { + var tickerList = new List(); + var tickerUrl = + $"/v3/reference/tickers" + + $"?ticker={request.Ticker}&type={request.Type}&market={request.Market}" + + $"&exchange={request.Exchange}&cusip={request.Cusip}&cik={request.Cik}" + + $"&date={request.Date}&search={request.Search}&active={request.Active}" + + $"&order={request.Order}&sort={request.Sort}&limit={request.Limit}"; + + while (tickerUrl != null) + { + var response = await _client.GetAsync(tickerUrl, cancellationToken); + + if (!response.IsSuccessStatusCode) + { + if (tickerList.Count != 0) + { + break; + } + + return new AllTickersResponse( + Count: 0, + Next_Url: null, + Request_Id: null, + Results: [], + Status: response.StatusCode.ToString() + ); + } + + var content = await response.Content.ReadAsStringAsync(cancellationToken); + + var scanResponse = JsonSerializer.Deserialize(content); + if (scanResponse != null) + { + tickerList.AddRange(scanResponse.Results); + tickerUrl = scanResponse.Next_Url; + } + } + + return new AllTickersResponse( + Count: tickerList.Count, + Next_Url: null, + Request_Id: null, + Status: HttpStatusCode.OK.ToString(), + Results: tickerList + ); + } +} diff --git a/Polygon.Client/PolygonClient.cs b/Polygon.Client/PolygonClient.cs index be95541..2c2af04 100644 --- a/Polygon.Client/PolygonClient.cs +++ b/Polygon.Client/PolygonClient.cs @@ -1,5 +1,25 @@ namespace Polygon.Client; -public class PolygonClient +using System.Net.Http; + +public partial class PolygonClient { + /// + /// The HttpClient instance used to make requests to the Polygon API. + /// + private HttpClient _client; + + /// + /// Initializes a new instance of the class with the specified API key. + /// + /// Polygon API key. + public PolygonClient(string apiKey) + { + _client = new HttpClient + { + BaseAddress = new Uri("https://api.polygon.io"), + }; + + _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey); + } } diff --git a/Polygon.Client/SortEnum.cs b/Polygon.Client/SortEnum.cs new file mode 100644 index 0000000..8fcd1ac --- /dev/null +++ b/Polygon.Client/SortEnum.cs @@ -0,0 +1,20 @@ +namespace Polygon.Client; + +public enum Sort +{ + Ticker, + Name, + Market, + Locale, + Primary_Exchange, + Type, + Currency_Symbol, + Currency_Name, + Base_Currency_Symbol, + Base_Currency_Name, + CIK, + Composite_Figi, + Share_Class_Figi, + Last_Updated_UTC, + Delisted_UTC +} \ No newline at end of file