Got query to return data.
This commit is contained in:
@@ -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();
|
||||||
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polygon.Client", "Polygon.Client\Polygon.Client.csproj", "{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polygon.Client", "Polygon.Client\Polygon.Client.csproj", "{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppTest", "ConsoleAppTest\ConsoleAppTest.csproj", "{0FD8AE9A-A1E3-4018-B6C1-D987CB1D493B}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -23,6 +25,10 @@ Global
|
|||||||
{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
|
||||||
{B6CA2C9B-AADB-4039-F459-2ED89AFA0461}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -1,16 +1,64 @@
|
|||||||
namespace Polygon.Client;
|
namespace Polygon.Client;
|
||||||
|
|
||||||
public record AllTickersRequest(
|
public class AllTickersRequest
|
||||||
string Ticker,
|
{
|
||||||
string Type,
|
/// <summary>
|
||||||
Market Market,
|
///
|
||||||
string Exchange,
|
/// </summary>
|
||||||
string Cusip,
|
public string? Ticker { get; set; }
|
||||||
string Cik,
|
|
||||||
DateOnly? Date,
|
/// <summary>
|
||||||
string Search,
|
///
|
||||||
bool Active,
|
/// </summary>
|
||||||
Order Order,
|
public string? Type { get; set; }
|
||||||
int Limit,
|
|
||||||
Sort Sort
|
/// <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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,17 @@
|
|||||||
namespace Polygon.Client;
|
namespace Polygon.Client;
|
||||||
|
|
||||||
public record AllTickersResponseItem();
|
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
|
||||||
|
);
|
||||||
|
|||||||
@@ -20,12 +20,20 @@ public partial class PolygonClient
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
var tickerList = new List<AllTickersResponseItem>();
|
var tickerList = new List<AllTickersResponseItem>();
|
||||||
var tickerUrl =
|
var tickerUrl = new RequestQueryBuilder("/v3/reference/tickers")
|
||||||
$"/v3/reference/tickers"
|
.Add("ticker", request.Ticker)
|
||||||
+ $"?ticker={request.Ticker}&type={request.Type}&market={request.Market}"
|
.Add("type", request.Type)
|
||||||
+ $"&exchange={request.Exchange}&cusip={request.Cusip}&cik={request.Cik}"
|
.Add("market", request.Market)
|
||||||
+ $"&date={request.Date}&search={request.Search}&active={request.Active}"
|
.Add("exchange", request.Exchange)
|
||||||
+ $"&order={request.Order}&sort={request.Sort}&limit={request.Limit}";
|
.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)
|
while (tickerUrl != null)
|
||||||
{
|
{
|
||||||
@@ -34,9 +42,7 @@ public partial class PolygonClient
|
|||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
if (tickerList.Count != 0)
|
if (tickerList.Count != 0)
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
return new AllTickersResponse(
|
return new AllTickersResponse(
|
||||||
Count: 0,
|
Count: 0,
|
||||||
@@ -49,12 +55,16 @@ public partial class PolygonClient
|
|||||||
|
|
||||||
var content = await response.Content.ReadAsStringAsync(cancellationToken);
|
var content = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
|
||||||
var scanResponse = JsonSerializer.Deserialize<AllTickersResponse>(content);
|
var scanResponse = JsonSerializer.Deserialize<AllTickersResponse>(
|
||||||
if (scanResponse != null)
|
content,
|
||||||
{
|
_jsonOptions
|
||||||
tickerList.AddRange(scanResponse.Results);
|
);
|
||||||
tickerUrl = scanResponse.Next_Url;
|
|
||||||
}
|
if (scanResponse == null)
|
||||||
|
break;
|
||||||
|
|
||||||
|
tickerList.AddRange(scanResponse.Results);
|
||||||
|
tickerUrl = scanResponse.Next_Url;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new AllTickersResponse(
|
return new AllTickersResponse(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
namespace Polygon.Client;
|
namespace Polygon.Client;
|
||||||
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
public partial class PolygonClient
|
public partial class PolygonClient
|
||||||
{
|
{
|
||||||
@@ -15,11 +16,14 @@ public partial class PolygonClient
|
|||||||
/// <param name="apiKey">Polygon API key.</param>
|
/// <param name="apiKey">Polygon API key.</param>
|
||||||
public PolygonClient(string apiKey)
|
public PolygonClient(string apiKey)
|
||||||
{
|
{
|
||||||
_client = new HttpClient
|
_client = new HttpClient { BaseAddress = new Uri("https://api.polygon.io") };
|
||||||
{
|
|
||||||
BaseAddress = new Uri("https://api.polygon.io"),
|
|
||||||
};
|
|
||||||
|
|
||||||
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey);
|
_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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user