コード例 #1
0
        public async Task LoadPortfolioAsync(string path)
        {
            await SynchronizedConsole.WriteLineAsync($"Beginning to parse file '{path}'.");

            string json = await File.ReadAllTextAsync(path);

            _portfolio = JsonConvert.DeserializeObject <Portfolio>(json);
            _quotes    = await _stockBroker.GetQuotes(GetUniqueSymbols().ToArray());
        }
コード例 #2
0
        public async Task <IDictionary <string, double> > GetQuotes(params string[] symbols)
        {
            string url = $"https://api.iextrading.com/1.0/tops/last?symbols={string.Join(',', symbols)}";
            await SynchronizedConsole.WriteLineAsync(url);

            HttpResponseMessage response = await _httpClient.GetAsync(url);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException($"Received response code {response.StatusCode} from '{url}'.");
            }

            Dictionary <string, double> transformed = new Dictionary <string, double>();
            List <Quote> quotes = JsonConvert.DeserializeObject <List <Quote> >(await response.Content.ReadAsStringAsync());

            for (var i = 0; i < quotes.Count; i++)
            {
                transformed.Add(quotes[i].Symbol, quotes[i].Price);
            }

            return(transformed);
        }