<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using CoinBot.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CoinBot.Clients.Kraken
{
    public sealed class KrakenClient : CoinClientBase, IMarketClient
    {
        private const string HTTP_CLIENT_NAME = @"Kraken";

        /// <summary>
        ///     The <see cref="Uri" /> of the CoinMarketCap endpoint.
        /// </summary>
        private static readonly Uri Endpoint = new Uri(uriString: "https://api.kraken.com/0/public/", UriKind.Absolute);

        /// <summary>
        ///     The <see cref="CurrencyManager" />.
        /// </summary>
        private readonly CurrencyManager _currencyManager;

        /// <summary>
        ///     The <see cref="JsonSerializerSettings" />.
        /// </summary>
        private readonly JsonSerializerSettings _serializerSettings;

        public KrakenClient(IHttpClientFactory httpClientFactory, ILogger<KrakenClient> logger, CurrencyManager currencyManager)
            : base(httpClientFactory, HTTP_CLIENT_NAME, logger)
        {
            this._currencyManager = currencyManager ?? throw new ArgumentNullException(nameof(currencyManager));

            this._serializerSettings = new JsonSerializerSettings
                                       {
                                           Error = (sender, args) =>
                                                   {
                                                       EventId eventId = new EventId(args.ErrorContext.Error.HResult);
                                                       Exception ex = args.ErrorContext.Error.GetBaseException();
                                                       this.Logger.LogError(eventId, ex, ex.Message);
                                                   }
                                       };
        }

        /// <summary>
        ///     The Exchange name.
        /// </summary>
        public string Name => "Kraken";

        /// <inheritdoc />
        public async Task<IReadOnlyCollection<MarketSummaryDto>> GetAsync()
        {
            try
            {
                List<KrakenAsset> assets = await this.GetAssetsAsync();
                List<KrakenPair> pairs = await this.GetPairsAsync();
                List<KrakenTicker> tickers = new List<KrakenTicker>();

                foreach (KrakenPair pair in pairs)
                {
                    // todo: can't get kraken details on these markets
                    if (pair.PairId.EndsWith(value: ".d", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    tickers.Add(await this.GetTickerAsync(pair));
                }

                return tickers.Select(selector: m =>
                                                {
                                                    string baseCurrency = assets.Find(match: a => StringComparer.InvariantCultureIgnoreCase.Equals(a.Id, m.BaseCurrency))
                                                                                .Altname;
                                                    string quoteCurrency = assets.Find(match: a => StringComparer.InvariantCultureIgnoreCase.Equals(a.Id, m.QuoteCurrency))
                                                                                 .Altname;

                                                    // Workaround for kraken
                                                    if (baseCurrency.Equals(value: "xbt", StringComparison.OrdinalIgnoreCase))
                                                    {
                                                        baseCurrency = "btc";
                                                    }

                                                    if (quoteCurrency.Equals(value: "xbt", StringComparison.OrdinalIgnoreCase))
                                                    {
                                                        quoteCurrency = "btc";
                                                    }

                                                    return new MarketSummaryDto
                                                           {
                                                               BaseCurrrency = this._currencyManager.Get(baseCurrency),
                                                               MarketCurrency = this._currencyManager.Get(quoteCurrency),<commit_msg>Used WhenAll<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using CoinBot.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CoinBot.Clients.Kraken
{
    public sealed class KrakenClient : CoinClientBase, IMarketClient
    {
        private const string HTTP_CLIENT_NAME = @"Kraken";

        /// <summary>
        ///     The <see cref="Uri" /> of the CoinMarketCap endpoint.
        /// </summary>
        private static readonly Uri Endpoint = new Uri(uriString: "https://api.kraken.com/0/public/", UriKind.Absolute);

        /// <summary>
        ///     The <see cref="CurrencyManager" />.
        /// </summary>
        private readonly CurrencyManager _currencyManager;

        /// <summary>
        ///     The <see cref="JsonSerializerSettings" />.
        /// </summary>
        private readonly JsonSerializerSettings _serializerSettings;

        public KrakenClient(IHttpClientFactory httpClientFactory, ILogger<KrakenClient> logger, CurrencyManager currencyManager)
            : base(httpClientFactory, HTTP_CLIENT_NAME, logger)
        {
            this._currencyManager = currencyManager ?? throw new ArgumentNullException(nameof(currencyManager));

            this._serializerSettings = new JsonSerializerSettings
                                       {
                                           Error = (sender, args) =>
                                                   {
                                                       EventId eventId = new EventId(args.ErrorContext.Error.HResult);
                                                       Exception ex = args.ErrorContext.Error.GetBaseException();
                                                       this.Logger.LogError(eventId, ex, ex.Message);
                                                   }
                                       };
        }

        /// <summary>
        ///     The Exchange name.
        /// </summary>
        public string Name => "Kraken";

        /// <inheritdoc />
        public async Task<IReadOnlyCollection<MarketSummaryDto>> GetAsync()
        {
            try
            {
                List<KrakenAsset> assets = await this.GetAssetsAsync();
                List<KrakenPair> pairs = await this.GetPairsAsync();

                static bool IsValid(KrakenPair pair)
                {
                    // todo: can't get kraken details on these markets
                    if (pair.PairId.EndsWith(value: ".d", StringComparison.Ordinal))
                    {
                        return false;
                    }

                    return true;
                }

                KrakenTicker[] tickers = await Task.WhenAll(pairs.Where(IsValid)
                                                                 .Select(this.GetTickerAsync));

                return tickers.Select(selector: m =>
                                                {
                                                    string baseCurrency = assets.Find(match: a => StringComparer.InvariantCultureIgnoreCase.Equals(a.Id, m.BaseCurrency))
                                                                                .Altname;
                                                    string quoteCurrency = assets.Find(match: a => StringComparer.InvariantCultureIgnoreCase.Equals(a.Id, m.QuoteCurrency))
                                                                                 .Altname;

                                                    // Workaround for kraken
                                                    if (baseCurrency.Equals(value: "xbt", StringComparison.OrdinalIgnoreCase))
                                                    {
                                                        baseCurrency = "btc";
                                                    }

                                                    if (quoteCurrency.Equals(value: "xbt", StringComparison.OrdinalIgnoreCase))
                                                    {
                                                        quoteCurrency = "btc";
                                                    }

                                                    return new MarketSummaryDto
                                                           {
                                                               BaseCurrrency = this._currencyManager.Get(baseCurrency),
                                                               MarketCurrency = this._currencyManager.Get(quoteCurrency),
{'fraction_non_alphanumeric': 0.07526072465326308, 'fraction_numerical': 0.00021503064186646596, 'mean_word_length': 0.9914365232284308, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 106, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4141580', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1753, 'n_words': 373}