/// <summary>
        /// Query
        /// </summary>
        public static IEnumerable <string[]> Query(string dataset, string apiKey, int version)
        {
            if (null == dataset)
            {
                throw new ArgumentNullException(nameof(dataset));
            }
            else if (version <= 0)
            {
                version = 3;
            }

            string address = $"https://www.quandl.com/api/v{version}/datasets/{dataset}.csv";

            if (string.IsNullOrEmpty(apiKey))
            {
                apiKey = ApiKey;
            }

            if (!string.IsNullOrEmpty(apiKey))
            {
                address += $"?api_key={apiKey}";
            }

            HttpClient httpClient = Dependencies.GetServiceRequired <HttpClient>();

            using var response  = httpClient.GetAsync(address).Result;
            using Stream stream = response.Content.ReadAsStreamAsync().Result;

            foreach (var record in CommaSeparatedValues.ParseCsv(stream, ',', '"', Encoding.UTF8))
            {
                yield return(record);
            }
        }
        /// <summary>
        /// Query
        /// </summary>
        public static async Task <string[][]> QueryAsync(string dataset, string apiKey, int version)
        {
            if (null == dataset)
            {
                throw new ArgumentNullException(nameof(dataset));
            }
            else if (version <= 0)
            {
                version = 3;
            }

            string address = $"https://www.quandl.com/api/v{version}/datasets/{dataset}.csv";

            if (string.IsNullOrEmpty(apiKey))
            {
                apiKey = ApiKey;
            }

            if (!string.IsNullOrEmpty(apiKey))
            {
                address += $"?api_key={apiKey}";
            }

            HttpClient httpClient = Dependencies.GetServiceRequired <HttpClient>();

            using var response = await httpClient.GetAsync(address).ConfigureAwait(false);

            using Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            return(await Task <string[][]> .Run(() => CommaSeparatedValues
                                                .ParseCsv(stream, ',', '"', Encoding.UTF8)
                                                .ToArray()).ConfigureAwait(false));

            //return CommaSeparatedValues.ParseCsv(stream, ',', '"', Encoding.UTF8).ToArray();
        }
Esempio n. 3
0
        /// <summary>
        /// Read Csv
        /// </summary>
        public static async IAsyncEnumerable <string[]> ReadCsvAsync(string address,
                                                                     Encoding encoding = null,
                                                                     char delimiter    = ',',
                                                                     char quotation    = '"',
                                                                     [EnumeratorCancellation] CancellationToken token = default)
        {
            if (address is null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            encoding ??= Encoding.Default;

            using Stream stream = await Client.GetStreamAsync(address, token).ConfigureAwait(false);

            using StreamReader reader = new(stream, encoding, true, -1, true);

            IEnumerable <string> Lines()
            {
                for (string line = reader.ReadLine(); line is not null; line = reader.ReadLine())
                {
                    token.ThrowIfCancellationRequested();

                    yield return(line);
                }
            }

            foreach (string[] record in CommaSeparatedValues.ParseCsv(Lines(), delimiter, quotation))
            {
                token.ThrowIfCancellationRequested();

                yield return(record);
            }
        }