Skip to content

Commit

Permalink
Update REST API. Do not return error messages only data
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Gerasimov committed Jan 4, 2018
1 parent 9f91529 commit 96c5a6e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 277 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static partial class ExchangeConnectorServiceExtensions
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<object> GetRatingAsync(this IExchangeConnectorService operations, CancellationToken cancellationToken = default(CancellationToken))
public static async Task<IList<ExchangeRatingModel>> GetRatingAsync(this IExchangeConnectorService operations, CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.GetRatingWithHttpMessagesAsync(null, cancellationToken).ConfigureAwait(false))
{
Expand Down Expand Up @@ -130,7 +130,7 @@ public static partial class ExchangeConnectorServiceExtensions
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<object> GetOrderAsync(this IExchangeConnectorService operations, string id, string exchangeName = default(string), string instrument = default(string), CancellationToken cancellationToken = default(CancellationToken))
public static async Task<ExecutionReport> GetOrderAsync(this IExchangeConnectorService operations, string id, string exchangeName = default(string), string instrument = default(string), CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.GetOrderWithHttpMessagesAsync(id, exchangeName, instrument, null, cancellationToken).ConfigureAwait(false))
{
Expand All @@ -153,7 +153,7 @@ public static partial class ExchangeConnectorServiceExtensions
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<object> CancelOrderAsync(this IExchangeConnectorService operations, string id, string exchangeName = default(string), CancellationToken cancellationToken = default(CancellationToken))
public static async Task<ExecutionReport> CancelOrderAsync(this IExchangeConnectorService operations, string id, string exchangeName = default(string), CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.CancelOrderWithHttpMessagesAsync(id, exchangeName, null, cancellationToken).ConfigureAwait(false))
{
Expand All @@ -177,7 +177,7 @@ public static partial class ExchangeConnectorServiceExtensions
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<object> CreateOrderAsync(this IExchangeConnectorService operations, OrderModel orderModel = default(OrderModel), CancellationToken cancellationToken = default(CancellationToken))
public static async Task<ExecutionReport> CreateOrderAsync(this IExchangeConnectorService operations, OrderModel orderModel = default(OrderModel), CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.CreateOrderWithHttpMessagesAsync(orderModel, null, cancellationToken).ConfigureAwait(false))
{
Expand All @@ -197,7 +197,7 @@ public static partial class ExchangeConnectorServiceExtensions
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<object> GetOpenedPositionAsync(this IExchangeConnectorService operations, string exchangeName = default(string), CancellationToken cancellationToken = default(CancellationToken))
public static async Task<IList<PositionModel>> GetOpenedPositionAsync(this IExchangeConnectorService operations, string exchangeName = default(string), CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.GetOpenedPositionWithHttpMessagesAsync(exchangeName, null, cancellationToken).ConfigureAwait(false))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public partial interface IExchangeConnectorService : System.IDisposable
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<object>> GetRatingWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<HttpOperationResponse<IList<ExchangeRatingModel>>> GetRatingWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Checks service is alive
Expand Down Expand Up @@ -123,7 +123,7 @@ public partial interface IExchangeConnectorService : System.IDisposable
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<object>> GetOrderWithHttpMessagesAsync(string id, string exchangeName = default(string), string instrument = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<HttpOperationResponse<ExecutionReport>> GetOrderWithHttpMessagesAsync(string id, string exchangeName = default(string), string instrument = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Cancels the existing order
Expand All @@ -140,7 +140,7 @@ public partial interface IExchangeConnectorService : System.IDisposable
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<object>> CancelOrderWithHttpMessagesAsync(string id, string exchangeName = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<HttpOperationResponse<ExecutionReport>> CancelOrderWithHttpMessagesAsync(string id, string exchangeName = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Places a new order on the exchange
Expand All @@ -158,7 +158,7 @@ public partial interface IExchangeConnectorService : System.IDisposable
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<object>> CreateOrderWithHttpMessagesAsync(OrderModel orderModel = default(OrderModel), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<HttpOperationResponse<ExecutionReport>> CreateOrderWithHttpMessagesAsync(OrderModel orderModel = default(OrderModel), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Returns information about opened positions
Expand All @@ -172,7 +172,7 @@ public partial interface IExchangeConnectorService : System.IDisposable
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<object>> GetOpenedPositionWithHttpMessagesAsync(string exchangeName = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<HttpOperationResponse<IList<PositionModel>>> GetOpenedPositionWithHttpMessagesAsync(string exchangeName = default(string), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ public AccountController(IApplicationFacade app, AppSettings appSettings)
/// <returns></returns>
[HttpGet("balance")]
[SwaggerOperation("GetBalance")]
[ProducesResponseType(typeof(IEnumerable<AccountBalance>), 200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
[Produces("application/json")]
private async Task<IActionResult> GetBalance([Required][FromQuery]string exchangeName)// Intentionally disabled
private async Task<IEnumerable<AccountBalance>> GetBalance([Required][FromQuery]string exchangeName)// Intentionally disabled
{
if (string.IsNullOrWhiteSpace(exchangeName))
{
return BadRequest($"Invalid {nameof(exchangeName)}");
throw new StatusCodeException(HttpStatusCode.BadRequest, $"Invalid {nameof(exchangeName)}");
}
try
{
return Ok(await Application.GetExchange(exchangeName).GetAccountBalance(_timeout));
return await Application.GetExchange(exchangeName).GetAccountBalance(_timeout);
}
catch (Exception e)
{
Expand All @@ -59,19 +55,15 @@ private async Task<IActionResult> GetBalance([Required][FromQuery]string exchang
/// <returns></returns>
[SwaggerOperation("GetTradeBalance")]
[HttpGet("tradeBalance")]
[ProducesResponseType(typeof(IEnumerable<TradeBalanceModel>), 200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
[Produces("application/json")]
public async Task<IActionResult> GetTradeBalance([FromQuery]string exchangeName)
public async Task<IReadOnlyCollection<TradeBalanceModel>> GetTradeBalance([FromQuery]string exchangeName)
{
if (string.IsNullOrWhiteSpace(exchangeName))
{
return BadRequest($"Invalid {nameof(exchangeName)}");
throw new StatusCodeException(HttpStatusCode.InternalServerError, $"Invalid {nameof(exchangeName)}");
}
try
{
return Ok(await Application.GetExchange(exchangeName).GetTradeBalances(_timeout));
return await Application.GetExchange(exchangeName).GetTradeBalances(_timeout);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Lykke.ExternalExchangesApi.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.SwaggerGen;
using TradingBot.Infrastructure.Monitoring;
using TradingBot.Models;
using TradingBot.Models.Api;

namespace TradingBot.Controllers.Api
Expand All @@ -22,7 +23,6 @@ public ExchangesController(IApplicationFacade app, IExchangeRatingValuer ratingV
/// Get a list of all connected exchanges
/// </summary>
/// <remarks>The names of available exchanges participates in API calls for exchange-specific methods</remarks>
/// <response code="200">An array of strings which are the names of exchanges</response>
[HttpGet]
[SwaggerOperation("GetSupportedExchanges")]
public IEnumerable<string> List()
Expand All @@ -34,27 +34,23 @@ public IEnumerable<string> List()
/// Get information about a specific exchange
/// </summary>
/// <param name="exchangeName">Name of the specific exchange</param>
/// <response code="200">An information about the exchange, such as available trading instruments</response>
/// <response code="400">Bad request response is returned in case of specifying name of unavailable exchange</response>
[SwaggerOperation("GetExchangeInfo")]
[HttpGet("{exchangeName}")]
[ProducesResponseType(typeof(ExchangeInformationModel), 200)]
[Produces("application/json")]
public IActionResult Index(string exchangeName)
public ExchangeInformationModel Index(string exchangeName)
{
var exchange = Application.GetExchange(exchangeName);

if (exchange == null)
{
return BadRequest(new ResponseMessage($"There isn't connected exchange with the name of {exchangeName}. Try GET api/exchanges for the list of all connected exchanges."));
throw new StatusCodeException(HttpStatusCode.InternalServerError, $"There isn't connected exchange with the name of {exchangeName}. Try GET api/exchanges for the list of all connected exchanges.");
}

return Ok(new ExchangeInformationModel
return new ExchangeInformationModel
{
Name = exchangeName,
State = exchange.State,
Instruments = exchange.Instruments
});
};
}


Expand All @@ -63,13 +59,10 @@ public IActionResult Index(string exchangeName)
/// </summary>
/// <returns>A collection of ratings for each enabled exchange</returns>
[HttpGet("rating")]
[ProducesResponseType(typeof(IEnumerable<ExchangeRatingModel>), 200)]
[ProducesResponseType(typeof(ResponseMessage), 500)]
[Produces("application/json")]
[SwaggerOperation("GetRating")]
public IActionResult GetRating()
public IReadOnlyCollection<ExchangeRatingModel> GetRating()
{
return Ok(_ratingValuer.Rating);
return _ratingValuer.Rating;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using TradingBot.Communications;
using TradingBot.Infrastructure.Auth;
using TradingBot.Infrastructure.Configuration;
using TradingBot.Models;
using TradingBot.Models.Api;
using TradingBot.Trading;
using TradingBot.Repositories;
Expand All @@ -34,12 +33,8 @@ public OrdersController(IApplicationFacade app, AppSettings appSettings, Transla
/// Returns information about all OPEN orders on the exchange
/// <param name="exchangeName">The name of the exchange</param>
/// </summary>
/// <response code="200">Active orders</response>
/// <response code="500">Unexpected error</response>
[SwaggerOperation("GetOpenedOrders")]
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ExecutionReport>), 200)]
[ProducesResponseType(typeof(ResponseMessage), 500)]
private async Task<IEnumerable<ExecutionReport>> Index([FromQuery, Required] string exchangeName) // Intentionally disabled
{
try
Expand All @@ -59,12 +54,8 @@ private async Task<IEnumerable<ExecutionReport>> Index([FromQuery, Required] str
/// <param name="id">The order id</param>
/// <param name="instrument">The instrument name of the order</param>
/// <param name="exchangeName">The exchange name</param>
/// <response code="200">The order is found</response>
/// <response code="500">The order either not exist or other server error</response>
[SwaggerOperation("GetOrder")]
[HttpGet("{id}")]
[ProducesResponseType(typeof(ExecutionReport), 200)]
[ProducesResponseType(typeof(ResponseMessage), 500)]
public async Task<ExecutionReport> GetOrder(string id, [FromQuery, Required] string exchangeName, [FromQuery, Required] string instrument)
{
try
Expand All @@ -85,20 +76,15 @@ public async Task<ExecutionReport> GetOrder(string id, [FromQuery, Required] str
///<param name="orderModel">A new order</param>
/// </summary>
/// <remarks>In the location header of successful response placed an URL for getting info about the order</remarks>
/// <response code="200">The order is successfully placed and an order status is returned</response>
/// <response code="400">Can't place the order. The reason is in the response</response>
[SwaggerOperation("CreateOrder")]
[HttpPost]
[ProducesResponseType(typeof(ExecutionReport), 200)]
[ProducesResponseType(typeof(ResponseMessage), 400)]
[ProducesResponseType(typeof(ResponseMessage), 500)]
public async Task<IActionResult> Post([FromBody] OrderModel orderModel)
public async Task<ExecutionReport> Post([FromBody] OrderModel orderModel)
{
try
{
if (orderModel == null)
{
return BadRequest("Order has to be specified");
throw new StatusCodeException(HttpStatusCode.BadRequest, "Order has to be specified");
}
if (string.IsNullOrEmpty(orderModel.ExchangeName))
{
Expand Down Expand Up @@ -135,7 +121,7 @@ public async Task<IActionResult> Post([FromBody] OrderModel orderModel)

translatedSignal.SetExecutionResult(result);

return Ok(result);
return result;
}
catch (Exception e)
{
Expand Down Expand Up @@ -168,20 +154,15 @@ private static string GetUniqueOrderId(OrderModel orderModel)
/// <remarks></remarks>
/// <param name="id">The order id to cancel</param>
/// <param name="exchangeName">The exchange name</param>
/// <response code="200">The order is successfully canceled</response>
/// <response code="400">Can't cancel the order. The reason is in the response</response>
[SwaggerOperation("CancelOrder")]
[HttpDelete("{id}")]
[ProducesResponseType(typeof(ExecutionReport), 200)]
[ProducesResponseType(typeof(ResponseMessage), 400)]
[ProducesResponseType(typeof(ResponseMessage), 500)]
public async Task<IActionResult> CancelOrder(string id, [FromQuery, Required]string exchangeName)
public async Task<ExecutionReport> CancelOrder(string id, [FromQuery, Required]string exchangeName)
{
try
{
if (string.IsNullOrEmpty(exchangeName))
{
return BadRequest("Exchange has to be specified");
throw new StatusCodeException(HttpStatusCode.InternalServerError, "Exchange has to be specified");
}

var instrument = new Instrument(exchangeName, string.Empty);
Expand All @@ -200,7 +181,7 @@ public async Task<IActionResult> CancelOrder(string id, [FromQuery, Required]str
if (result.ExecutionStatus == OrderExecutionStatus.Rejected)
throw new StatusCodeException(HttpStatusCode.BadRequest, $"Exchange return status: {result.ExecutionStatus}", null);

return Ok(result);
return result;
}
catch (Exception e)
{
Expand Down
Loading

0 comments on commit 96c5a6e

Please sign in to comment.