Save point

This commit is contained in:
Dan
2025-05-11 15:56:38 -07:00
parent 808d1cc96e
commit 8b78a180a4
8 changed files with 157 additions and 1 deletions
+16
View File
@@ -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
);
+9
View File
@@ -0,0 +1,9 @@
namespace Polygon.Client;
public record class AllTickersResponse(
int Count,
string? Next_Url,
string? Request_Id,
IEnumerable<AllTickersResponseItem> Results,
string Status
);
+3
View File
@@ -0,0 +1,3 @@
namespace Polygon.Client;
public record AllTickersResponseItem();
+13
View File
@@ -0,0 +1,13 @@
namespace Polygon.Client;
/// <summary>
/// Market type.
/// </summary>
public enum Market
{
Stock,
Crypto,
Fx,
Otc,
Indices
}
+7
View File
@@ -0,0 +1,7 @@
namespace Polygon.Client;
public enum Order
{
Asc,
Desc
}
@@ -0,0 +1,68 @@
namespace Polygon.Client;
using System.Net;
using System.Text.Json;
/// <summary>
/// The PolygonClient class provides methods to interact with the Polygon API.
/// </summary>
public partial class PolygonClient
{
/// <summary>
/// Get all the ticker symbols.
/// </summary>
/// <param name="request">AllTickersRequest.</param>
/// <param name="cancellationToken">CancellationToken.</param>
/// <returns></returns>
public async Task<AllTickersResponse> GetAllTickersAsync(
AllTickersRequest request,
CancellationToken cancellationToken = default
)
{
var tickerList = new List<AllTickersResponseItem>();
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<AllTickersResponse>(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
);
}
}
+21 -1
View File
@@ -1,5 +1,25 @@
namespace Polygon.Client;
public class PolygonClient
using System.Net.Http;
public partial class PolygonClient
{
/// <summary>
/// The HttpClient instance used to make requests to the Polygon API.
/// </summary>
private HttpClient _client;
/// <summary>
/// Initializes a new instance of the <see cref="PolygonClient"/> class with the specified API key.
/// </summary>
/// <param name="apiKey">Polygon API key.</param>
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);
}
}
+20
View File
@@ -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
}