Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b7c9ed2904 | |||
| 8b78a180a4 | |||
| 808d1cc96e |
@@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Polygon.Client\Polygon.Client.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using Polygon.Client;
|
||||||
|
|
||||||
|
Console.WriteLine("Start");
|
||||||
|
|
||||||
|
var client = new PolygonClient("");
|
||||||
|
var request = new AllTickersRequest();
|
||||||
|
var result = await client.GetAllTickersAsync(request);
|
||||||
|
var a = result.Results.ToList();
|
||||||
|
|
||||||
|
Console.WriteLine(result.Status);
|
||||||
|
Console.WriteLine("End");
|
||||||
|
Console.ReadKey();
|
||||||
@@ -11,7 +11,25 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||||||
README.md = README.md
|
README.md = README.md
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polygon.Client", "Polygon.Client\Polygon.Client.csproj", "{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppTest", "ConsoleAppTest\ConsoleAppTest.csproj", "{0FD8AE9A-A1E3-4018-B6C1-D987CB1D493B}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{0FD8AE9A-A1E3-4018-B6C1-D987CB1D493B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{0FD8AE9A-A1E3-4018-B6C1-D987CB1D493B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{0FD8AE9A-A1E3-4018-B6C1-D987CB1D493B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{0FD8AE9A-A1E3-4018-B6C1-D987CB1D493B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
namespace Polygon.Client;
|
||||||
|
|
||||||
|
public class AllTickersRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public string? Ticker { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public string? Type { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public Market Market { get; set; } = Market.Stock;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public string? Exchange { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public string? Cusip { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public string? Cik { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public DateOnly? Date { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public string? Search { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public bool Active { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public Order Order { get; set; } = Order.Asc;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public int Limit { get; set; } = 100;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public Sort Sort { get; set; } = Sort.Ticker;
|
||||||
|
}
|
||||||
@@ -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,17 @@
|
|||||||
|
namespace Polygon.Client;
|
||||||
|
|
||||||
|
public record AllTickersResponseItem(
|
||||||
|
bool Active,
|
||||||
|
string Cik,
|
||||||
|
string Composite_Figi,
|
||||||
|
string Currency_Name,
|
||||||
|
string Delisted_Utc,
|
||||||
|
string Last_Updated_Utc,
|
||||||
|
string Locale,
|
||||||
|
string Market,
|
||||||
|
string Name,
|
||||||
|
string Primary_Exchange,
|
||||||
|
string Share_Class_Figi,
|
||||||
|
string Ticker,
|
||||||
|
string Type
|
||||||
|
);
|
||||||
@@ -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,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
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 = new RequestQueryBuilder("/v3/reference/tickers")
|
||||||
|
.Add("ticker", request.Ticker)
|
||||||
|
.Add("type", request.Type)
|
||||||
|
.Add("market", request.Market)
|
||||||
|
.Add("exchange", request.Exchange)
|
||||||
|
.Add("cusip", request.Cusip)
|
||||||
|
.Add("cik", request.Cik)
|
||||||
|
.Add("date", request.Date)
|
||||||
|
.Add("search", request.Search)
|
||||||
|
.Add("active", request.Active)
|
||||||
|
.Add("order", request.Order)
|
||||||
|
.Add("sort", request.Sort)
|
||||||
|
.Add("limit", request.Limit)
|
||||||
|
.ToString();
|
||||||
|
|
||||||
|
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,
|
||||||
|
_jsonOptions
|
||||||
|
);
|
||||||
|
|
||||||
|
if (scanResponse == null)
|
||||||
|
break;
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
namespace Polygon.Client;
|
||||||
|
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
namespace Polygon.Client;
|
||||||
|
|
||||||
|
using System.Text;
|
||||||
|
using System.Web;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="baseUrl">Base URL. Default is empty string.</param>
|
||||||
|
internal class RequestQueryBuilder(string? baseUrl = null)
|
||||||
|
{
|
||||||
|
private readonly StringBuilder _builder = new();
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return (baseUrl ?? string.Empty) + _builder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a string parameter to the query string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">Name of parameter.</param>
|
||||||
|
/// <param name="value">Value of parameter.</param>
|
||||||
|
/// <returns>RequestQueryBuilder.</returns>
|
||||||
|
public RequestQueryBuilder Add(string name, string? value)
|
||||||
|
{
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
_builder.Append(_builder.Length == 0 ? '?' : '&');
|
||||||
|
_builder.Append(name);
|
||||||
|
_builder.Append(':');
|
||||||
|
_builder.Append(HttpUtility.UrlEncode(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RequestQueryBuilder Add(string name, DateOnly? value)
|
||||||
|
{
|
||||||
|
//// TODO: Convert date correctly.
|
||||||
|
return this.Add(name, value?.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public RequestQueryBuilder Add(string name, object? value)
|
||||||
|
{
|
||||||
|
return this.Add(name, value?.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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