Save point
This commit is contained in:
@@ -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
|
||||
);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Polygon.Client;
|
||||
|
||||
public record class AllTickersResponse(
|
||||
int Count,
|
||||
string? Next_Url,
|
||||
string? Request_Id,
|
||||
IEnumerable<AllTickersResponseItem> Results,
|
||||
string Status
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Polygon.Client;
|
||||
|
||||
public record AllTickersResponseItem();
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Polygon.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Market type.
|
||||
/// </summary>
|
||||
public enum Market
|
||||
{
|
||||
Stock,
|
||||
Crypto,
|
||||
Fx,
|
||||
Otc,
|
||||
Indices
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user