Renewing session using ISession wrapper

Hello, we are currently using the PlatformSession C# implementation to login to the RDP Api. We would like to maintain our session for a long period of time and query multiple instruments through it. This is our current implementation, obtained from parts unknown, as it was done before my time.
I saw a previous answer which used the POST mechanism to extend sessions. Is there a way to modify our current implementation that uses PlatformSession, or would you recommend moving completely over to the POST approach?
using System.Data;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Math.EC;
using Refinitiv.Data.Content.Data;
using Refinitiv.Data.Content.HistoricalPricing;
using Refinitiv.Data.Core;
using ReutersRdpApi.Enums;
using ReutersRdpApi.Interfaces;
using ReutersShared;
namespace ReutersRdpApi.Classes;
public class ReutersApi : IReutersApi
{
private ISession _session;
private ISession _openSession;
private readonly string _appKey;
private readonly string _userName;
private readonly string _password;
private Session.State _state;
private readonly ILogger _log;
private static readonly string _confFileName = "refinitiv-data.config.json";
private static readonly object _lock = new object();
public ReutersApi(string apiKey, string userName, string password, ILogger log = null)
{
this._log = log;
this._appKey = apiKey;
this._userName = userName;
this._password = password;
if (!File.Exists(ReutersApi._confFileName)) CreateConfigFile();
_openSession = CreateSession();
}
public static void CreateConfigFile()
{
var config = @"{""sessions"": {
""default"": ""platform"",
""platform"": {
""default"": ""libsession"",
""libsession"": {
""apis"": {
""data"": {
""historical-pricing"": {
""url"": ""https://api.refinitiv.com/data/historical-pricing/v1""
}
}
}
}
}
}
}";
lock (_lock)
{
File.WriteAllText(ReutersApi._confFileName, config);
}
}
private ISession CreateSession()
{
return PlatformSession.Definition().AppKey(this._appKey)
.OAuthGrantType(new GrantPassword().UserName(this._userName).Password(this._password))
.TakeSignonControl(true).GetSession().OnState(this.OnStateUpdate)
.OnEvent(this.OnEventUpdate);
}
private ISession GetSession()
{
return _openSession;
}
/// <summary>
/// Gets the bars from reuters using the full string Ric with expiry Tenor i.e CLZ2 - CME WTI Dec22.
/// </summary>
/// <param name="fullRic"></param>
/// <param name="barInterval"></param>
/// <param name="numberOfBars"></param>
/// <returns></returns>
public List<ReutersBar>? GetBars(string fullRic, Summaries.Interval barInterval, int numberOfBars)
{
this._session = this.GetSession();
var bars = new List<ReutersBar>();
var fields = new string[]
{
ReutersFields.TimeStamp, ReutersFields.Open, ReutersFields.High, ReutersFields.Low, ReutersFields.Close
};
try
{
this._log?.LogInformation($"Opening Session.");
var sw = new Stopwatch();
sw.Start();
while (this._state != Session.State.Opened)
{
this._session.Open();
if (this._state != Session.State.Opened)
{
var newSession = CreateSession();
this._session = newSession;
this._openSession = newSession;
var lastCode = this._session.LastEventCode;
this._log?.LogWarning($"Session failed to open. Last event code: {lastCode.ToString()} Retrying. ");
Task.Delay(1000);
}
if (sw.Elapsed > TimeSpan.FromSeconds(20))
{
this._log?.LogError($"Session connection timed out! Returning null!");
return bars;
}
}
this._log?.LogInformation($"Getting bars for {fullRic}");
var result = Summaries.Definition(fullRic).Interval(barInterval).Fields(fields).Count((uint)numberOfBars).GetData();
if (result.IsSuccess)
{
this._log?.LogInformation($"Bar request returned success.");
if (result.Data.Records.Count == 0)
{
this._log?.LogInformation("No bars returned.");
return bars;
}
var table = result.Data.Table;
var cols = table.Columns;
var barRecords = table.Rows;
var ricInd = cols.IndexOf("Instrument");
var openInd = cols.IndexOf(ReutersFields.Open);
var highInd = cols.IndexOf(ReutersFields.High);
var lowInd = cols.IndexOf(ReutersFields.Low);
var closeInd = cols.IndexOf(ReutersFields.Close);
var timeInd = cols.IndexOf(ReutersFields.TimeStamp);
if(timeInd == -1) timeInd = cols.IndexOf("DATE");
bars.AddRange(from DataRow data in barRecords
select data.ItemArray
into array
select new ReutersBar
{
Ric = (string) array[ricInd],
Timestamp = DateTime.Parse((string) array[timeInd], CultureInfo.InvariantCulture).ToUniversalTime(),
Open = (double) array[openInd],
High = (double) array[highInd],
Low = (double) array[lowInd],
Close = (double) array[closeInd]
});
}
}
catch (Exception e)
{
this._log?.LogInformation($"Failed to get bars for {fullRic} with error {e}");
}
finally
{
}
return bars;
}
private void OnStateUpdate(Session.State state, string msg, ISession session)
{
this._state = state;
this._log?.LogInformation($"OnStateUpdate: state {state}, msg {msg}.");
}
private void OnEventUpdate(Session.EventCode eventCode, JObject msg, ISession arg3)
{
this._log?.LogInformation($"OnEvent: code {eventCode}, msg {msg}.");
}
Thanks!
Best Answers
-
Thanks, I will peruse through those!
On immediate first glance, it looks like we have an implementation extremely similar if not identical to that.Do you know whether the below call logs the user in again? This is where we are encountering our troubles - attempting to query once more causes us to login.
// Create the platform session.
using ISession session = Sessions.GetSession();
// Open the session
session.Open();
0 -
Great, that really clears things up. So our only option is to use the HttpClient and submit a refresh/renew request every few mins?
0
Answers
-
Thank you for reaching out to us.
Typically, as far as I know, the library will maintain the session as long as the application is running and the account is not used by other applications.
Another option is using the HTTP Client directly to send request messages to RDP. You can refer to the Postman examples available on Data Platform APIs.
You can generate the C# snippet code from Postman examples. Please refer to this Generate code for a REST API call using Postman in just a few clicks article.
0 -
Thanks for your response. Do you have any documentation or examples I can read about the Platform library? I would rather stick to our original method rather than rebuild via REST from scratch, but will change everything if needed.
0 -
Please check this Refinitiv Data Library for .Net. The examples are on GitHub.
0 -
I tested it and found that calling the Open method with a new session will create another login request (session) to the platform. RDP V1 supports only one active session at a time.
You can check the behavior by enabling the debug log.
0 -
You can use only one session or close the old one before creating a new session.
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛