klhztrader/KLHZ.Trader.Service/Controllers/ExchangeDataController.cs

67 lines
1.9 KiB
C#

using Grpc.Core;
using Microsoft.AspNetCore.Mvc;
using Tinkoff.InvestApi;
using Tinkoff.InvestApi.V1;
namespace KLHZ.Trader.Service.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class ExchangeDataController : ControllerBase
{
private readonly InvestApiClient _investApiClient;
public ExchangeDataController(InvestApiClient investApiClient)
{
_investApiClient = investApiClient;
}
[HttpGet]
public async Task<string?> GetFigi([FromQuery] string ticker, [FromQuery] string? classCode = "TQBR")
{
var req = new InstrumentRequest()
{
ClassCode = classCode,
IdType = InstrumentIdType.Ticker,
Id = ticker,
};
try
{
var res = await _investApiClient.Instruments.GetInstrumentByAsync(req);
return res.Instrument.Figi;
}
catch (RpcException ex)
{
if (ex.StatusCode == Grpc.Core.StatusCode.NotFound && classCode == "TQBR")
{
return await GetFigi(ticker, "SPBFUT");
}
}
return null;
}
[HttpGet]
public async Task<string?> GetTicker([FromQuery] string figi)
{
var req = new InstrumentRequest()
{
ClassCode = "figi",
IdType = InstrumentIdType.Figi,
Id = figi,
};
try
{
var res = await _investApiClient.Instruments.GetInstrumentByAsync(req);
return res.Instrument.Ticker;
}
catch (Exception ex)
{
}
return null;
}
}
}