using System;
using RestSharp;
using ZuluTradingSampleApp.Configuration;

namespace ZuluTradingSampleApp.Sender
{
    /// <summary>
    /// Utility class used to send rest calls to a trading server
    /// </summary>
    public class TradingResquestsSender
    {
        /// <summary>
        /// Sends a trading request to the trading server provided
        /// using basic http authentication
        /// </summary>
        /// <param name="config">Holder of Zulu's trading server info</param>
        /// <param name="authenticationsInfo">Holder of Zulu credentials</param>
        /// <param name="tradingRequest">trading request as a rest call string</param>
        /// <returns>he server response as string</returns>
        /// <exception cref="Exception"></exception>
        public static TResult SendRequest<TResult>(ZuluTradingServerConfig config, ZuluTradeAuthenticationInfo authenticationsInfo, string tradingRequest) where TResult : new()
        {
            RestClient client = new RestClient(config.TradingServicesUrl);
            client.Authenticator = new HttpBasicAuthenticator(authenticationsInfo.ZuluAccountUsername, authenticationsInfo.ZuluAccountPassword);
            var request = new RestRequest(tradingRequest, Method.GET);
            var response = client.Execute<TResult>(request);
            return response.Data;
        }

    }
}
