Skip to content

Commit

Permalink
refactor: minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikostiha committed Dec 27, 2024
1 parent d489d90 commit 6bcd444
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 68 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Add [nuget package](https://www.nuget.org/packages/SyncAPIConnect) to the projec
```csharp
var client = XApiClient.Create("81.2.190.163", 5112, 5113);
await client.ConnectAsync();
await client.LoginAsync(new Credentials("login", "password"));
await client.LoginAsync(new Credentials("accountId", "password"));
var openTrades = await client.GetTradesAsync(true);
```

Expand Down
38 changes: 10 additions & 28 deletions src/SyncAPIConnector/XApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@ public class XApiClient : IXApiClient, IDisposable
/// <returns>A new instance of <see cref="XApiClient"/>.</returns>
public static XApiClient Create(string address, int requestingPort, int streamingPort, IStreamingListener? streamingListener = null)
{
var apiConnector = ApiConnector.Create(address, requestingPort, streamingPort, streamingListener);
return new XApiClient(apiConnector)
{
IsApiConnectorOwner = true
};
var requestingEndpoint = new IPEndPoint(IPAddress.Parse(address), requestingPort);
var streamingEndpoint = new IPEndPoint(IPAddress.Parse(address), streamingPort);

return Create(requestingEndpoint, streamingEndpoint, streamingListener);
}

/// <summary>
/// Creates a new <see cref="XApiClient"/> instance using the provided endpoints.
/// </summary>
/// <param name="endpoint">The endpoint for requesting data.</param>
/// <param name="requestingEndpoint">The endpoint for requesting data.</param>
/// <param name="streamingEndpoint">The endpoint for streaming data.</param>
/// <param name="streamingListener">Optional streaming listener for handling streamed data.</param>
/// <returns>A new instance of <see cref="XApiClient"/>.</returns>
public static XApiClient Create(IPEndPoint endpoint, IPEndPoint streamingEndpoint, IStreamingListener? streamingListener = null)
public static XApiClient Create(IPEndPoint requestingEndpoint, IPEndPoint streamingEndpoint, IStreamingListener? streamingListener = null)
{
var streamingApiConnector = new StreamingApiConnector(streamingEndpoint, streamingListener);
var apiConnector = new ApiConnector(endpoint, streamingApiConnector);
var apiConnector = ApiConnector.Create(requestingEndpoint, streamingEndpoint, streamingListener);

return new XApiClient(apiConnector)
{
IsApiConnectorOwner = true
Expand All @@ -51,8 +50,9 @@ public static XApiClient Create(IPEndPoint endpoint, IPEndPoint streamingEndpoin

private Credentials? _credentials;

/// Create a new instance.
/// <summary>
/// Initializes a new instance of the <see cref="XApiClient"/> class using the specified <see cref="ApiConnector"/>.
/// Create a new instance.
/// </summary>
/// <param name="apiConnector">An instance of <see cref="ApiConnector"/> to manage the connection.</param>
public XApiClient(ApiConnector apiConnector)
Expand Down Expand Up @@ -90,24 +90,6 @@ public event EventHandler? Disconnected
remove { ApiConnector.Disconnected -= value; }
}

/// <summary>
/// Occurs when a message is received from the API.
/// </summary>
public event EventHandler<MessageEventArgs>? MessageReceived
{
add { ApiConnector.MessageReceived += value; }
remove { ApiConnector.MessageReceived -= value; }
}

/// <summary>
/// Occurs when a message is sent to the API.
/// </summary>
public event EventHandler<MessageEventArgs>? MessageSent
{
add { ApiConnector.MessageSent += value; }
remove { ApiConnector.MessageSent -= value; }
}

#endregion Events

/// <summary>
Expand Down
17 changes: 17 additions & 0 deletions src/SyncAPIConnector/abstraction/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
using System.Threading;
using System.Threading.Tasks;

namespace Xtb.XApi;

/// <summary>
/// Remote client interface for single endpoint.
/// </summary>
public interface IClient : IConnectable, ISender, IReceiver
{
/// <summary>
/// Send a message to the remote endpoint and wait for response.
/// </summary>
/// <param name="message">Message to send.</param>
/// <returns>Response from the endpoint.</returns>
string SendMessageWaitResponse(string message);

/// <summary>
/// Send a message to the remote endpoint.
/// </summary>
/// <param name="message">A message to send.</param>
/// <param name="cancellationToken">Token to cancel operation.</param>
/// <returns>Response from the endpoint.</returns>
Task<string> SendMessageWaitResponseAsync(string message, CancellationToken cancellationToken = default);
}
2 changes: 1 addition & 1 deletion src/SyncAPIConnector/abstraction/IConnectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IConnectable
/// <summary>
/// Endpoint that the connection was established with.
/// </summary>
public IPEndPoint Endpoint { get; }
IPEndPoint Endpoint { get; }

/// <summary>
/// Indicates whether the client is connected to the endpoint.
Expand Down
50 changes: 18 additions & 32 deletions src/SyncAPIConnector/commands/APICommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public static class APICommandFactory
/// <summary> Application type. </summary>
public const string AppType = "dotNET";

/// <summary>
/// Maximum number of redirects (to avoid redirection loops).
/// </summary>
public const int MAX_REDIRECTS = 3;

#region Command creators

public static LoginCommand CreateLoginCommand(string userId, string password, string? appId = null, string? appName = null, bool prettyPrint = false)
Expand Down Expand Up @@ -395,23 +390,29 @@ public static LoginResponse ExecuteLoginCommand(ApiConnector connector, string u
var jsonObj = connector.ExecuteCommand(loginCommand);
var loginResponse = new LoginResponse(jsonObj.ToString());

var redirectCounter = 0;

while (loginResponse.RedirectRecord != null)
{
if (redirectCounter >= MAX_REDIRECTS)
throw new APICommunicationException($"Too many redirects ({redirectCounter}).");

connector.Redirect(new IPEndPoint(IPAddress.Parse(loginResponse.RedirectRecord.Address), loginResponse.RedirectRecord.MainPort ?? -1));
redirectCounter++;
loginResponse = new LoginResponse(connector.ExecuteCommand(loginCommand).ToString());
}

if (loginResponse.StreamSessionId != null)
{
connector.Streaming.StreamSessionId = loginResponse.StreamSessionId;
}

//var redirectCounter = 0;

//while (loginResponse.RedirectRecord != null)
//{
// if (redirectCounter >= MAX_REDIRECTS)
// throw new APICommunicationException($"Too many redirects ({redirectCounter}).");

// var newServer = new Server(loginResponse.RedirectRecord.Address, loginResponse.RedirectRecord.MainPort, loginResponse.RedirectRecord.StreamingPort, true, "Redirected to: " + loginResponse.RedirectRecord.Address + ":" + loginResponse.RedirectRecord.MainPort + "/" + loginResponse.RedirectRecord.StreamingPort);
// connector.Redirect(newServer);
// redirectCounter++;
// loginResponse = new LoginResponse(connector.ExecuteCommand(loginCommand).ToString());
//}

//if (loginResponse.StreamSessionId != null)
//{
// connector.Streaming.StreamSessionId = loginResponse.StreamSessionId;
//}

return loginResponse;
}

Expand All @@ -424,21 +425,6 @@ public static async Task<LoginResponse> ExecuteLoginCommandAsync(ApiConnector co
var jsonObj = await connector.ExecuteCommandAsync(loginCommand, cancellationToken).ConfigureAwait(false);
var loginResponse = new LoginResponse(jsonObj.ToString());

var redirectCounter = 0;

while (loginResponse.RedirectRecord != null)
{
if (redirectCounter >= MAX_REDIRECTS)
throw new APICommunicationException($"Too many redirects ({redirectCounter}).");

await connector.RedirectAsync(
new IPEndPoint(IPAddress.Parse(loginResponse.RedirectRecord.Address), loginResponse.RedirectRecord.MainPort ?? -1),
cancellationToken).ConfigureAwait(false);
redirectCounter++;
var jsonObj2 = await connector.ExecuteCommandAsync(loginCommand, cancellationToken).ConfigureAwait(false);
loginResponse = new LoginResponse(jsonObj2.ToString());
}

if (loginResponse.StreamSessionId != null)
{
connector.Streaming.StreamSessionId = loginResponse.StreamSessionId;
Expand Down
6 changes: 4 additions & 2 deletions src/SystemTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private static void RunSyncTest(XApiClient xApiClient)
Console.WriteLine("----Sync test---");
var syncTest = new SyncTest(xApiClient, _userId, _password)
{
ShallLogTime = true
ShallLogTime = true,
ShallOpenTrades = false,
};
syncTest.Run();
}
Expand All @@ -72,7 +73,8 @@ private static void RunAsyncTest(XApiClient xApiClient)
var asyncTest = new AsyncTest(xApiClient, _userId, _password)
{
MessageFolder = @"\messages\",
ShallLogTime = true
ShallLogTime = true,
ShallOpenTrades = false,
};

using var tokenSource = new CancellationTokenSource();
Expand Down
8 changes: 4 additions & 4 deletions src/SystemTests/XApiClientTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public string? MessageFolder
_messageFolder = value;
if (value == null)
{
Client.MessageReceived -= Connector_MessageReceived;
Client.MessageSent -= Connector_MessageSent;
Client.ApiConnector.MessageReceived -= Connector_MessageReceived;
Client.ApiConnector.MessageSent -= Connector_MessageSent;
}
else
{
Client.MessageReceived += Connector_MessageReceived;
Client.MessageSent += Connector_MessageSent;
Client.ApiConnector.MessageReceived += Connector_MessageReceived;
Client.ApiConnector.MessageSent += Connector_MessageSent;
}
}
}
Expand Down

0 comments on commit 6bcd444

Please sign in to comment.