Exemplo n.º 1
0
        public async Task <InitDataLiveDemoClientResponse> InitData(string clientId)
        {
            var marginTradingEnabled = await _marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId);

            if (!marginTradingEnabled.Demo && !marginTradingEnabled.Live)
            {
                throw new Exception("Margin trading is not available");
            }

            var initData        = new InitDataLiveDemoClientResponse();
            var clientIdRequest = new ClientIdBackendRequest {
                ClientId = clientId
            };

            var initDataResponses = await _httpRequestService.RequestIfAvailableAsync <InitDataBackendResponse>(clientIdRequest, "init.data", () => null, marginTradingEnabled);

            initData.Live = initDataResponses.Live?.ToClientContract();
            initData.Demo = initDataResponses.Demo?.ToClientContract();

            var initAssetsResponses = await _httpRequestService.RequestIfAvailableAsync(clientIdRequest, "init.assets", Array.Empty <AssetPairBackendContract>, marginTradingEnabled);

            initData.Assets = initAssetsResponses.Live.Concat(initAssetsResponses.Demo).GroupBy(a => a.Id)
                              .Select(g => g.First().ToClientContract()).ToArray();

            var initPricesResponse =
                await _httpRequestService.RequestWithRetriesAsync <Dictionary <string, InstrumentBidAskPairContract> >(
                    clientIdRequest, "init.prices");

            initData.Prices = initPricesResponse.ToDictionary(p => p.Key, p => p.Value.ToClientContract());

            return(initData);
        }