Пример #1
0
        /// <summary>
        /// Fetch stock information and data
        /// </summary>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public async Task <Result <StockInfoClientResponseDto> > GetStockInfo(string symbol)
        {
            _logger.LogInformation("Fetching data for ticker {0}", symbol);

            AddQueryParam("t", symbol);

            var response = await _http.GetAsync(BuildQuery());

            _logger.LogInformation("Data fetched for ticker {0}", symbol);

            var result = await HandleClientResponse(response);

            if (!result.Succedded)
            {
                _logger.LogInformation("Fetchind data for ticker {0} failed -> {1}", symbol, result.Message);
                return(ResultExtensions.Failure <StockInfoClientResponseDto>(result.Message));
            }

            if (!ContainsTickerId(result.Instance, symbol))
            {
                _logger.LogInformation("Ticker {0} not found", symbol);
                return(ResultExtensions.Failure <StockInfoClientResponseDto>("Ticker not found"));
            }

            var fetchedInstance = ParseInstanceFromHtmlContent(result.Instance);

            _logger.LogInformation("Data for ticker fetched and prepared");

            return(ResultExtensions.Success(fetchedInstance));
        }
Пример #2
0
        public async Task <ResultArray <StockInfoResponseDto> > GetStockInfos(string symbol)
        {
            var clients = _factory.GetAllClients();

            if (!clients.Any())
            {
                return(ResultExtensions.FailureArray <StockInfoResponseDto>("Unexpected exception"));
            }

            var clientTasks = await LaunchClientsCall(clients, symbol);

            var finishedTasks = clientTasks.Cast <Task <Result <StockInfoClientResponseDto> > >();

            var results = new List <StockInfoResponseDto>();

            foreach (var finshedTask in finishedTasks)
            {
                var instance = HandleClientResponse(finshedTask.Result);

                if (instance == null)
                {
                    var dto = _mapper.Map <StockInfoResponseDto>(instance);
                    results.Add(dto);
                }
            }

            return(ResultExtensions.Success(instances: results));
        }
Пример #3
0
        private async Task <Result <T> > HandleGoodRequest <T>(HttpResponseMessage response)
        {
            var content = await ReadResponseContent(response);

            var instnace = JsonConvert.DeserializeObject <T>(content);

            return(ResultExtensions.Success(instnace));
        }
Пример #4
0
        protected async Task <Result <string> > HandleClientResponse(HttpResponseMessage response)
        {
            if (response.IsSuccessStatusCode)
            {
                var content = await ReadResponseContent(response);

                return(ResultExtensions.Success(content));
            }

            return(HandleBadRequest <string>(response));
        }
Пример #5
0
        public async Task <Result <PremarketDataResponseDto> > FetchPremarketData(string symbol)
        {
            var result = await _client.GetPremarketData(symbol, StonkType.Stock);

            if (!result.Succedded)
            {
                return(ResultExtensions.Failure <PremarketDataResponseDto>("Stock with given symbol not found"));
            }

            if (!ValidPage(result.Instance, "intraday__data"))
            {
                return(ResultExtensions.Failure <PremarketDataResponseDto>("Stock with given symbol not found"));
            }

            var instance = CreateInstanceBasedOnHtmlContent(result.Instance);

            return(ResultExtensions.Success(instance));
        }