<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace Narochno.Credstash.Configuration
{
    public class CredstashConfigurationProvider : ConfigurationProvider
    {
        private readonly Credstash _credstash;
        private readonly int _degreeOfParallelism;
        private readonly Dictionary<string, string> _encryptionContext;
        private readonly bool _suppressErrors;

        public CredstashConfigurationProvider(Credstash credstash, Dictionary<string, string> encryptionContext)
        {
            _credstash = credstash;
            _degreeOfParallelism = credstash.Options.DegreeOfParallelism;
            _encryptionContext = encryptionContext;
            _suppressErrors = credstash.Options.SuppressErrors;
        }

        public override void Load()
        {
            Data = LoadAsync().Result;
        }

        public async Task<Dictionary<string, string>> LoadAsync()
        {
            var data = new Dictionary<string, string>();

            var entries = (await _credstash.ListAsync()).Select(x => x.Name).Distinct().ToList();

            if (entries.Count() > 10 && _degreeOfParallelism > 1)
            {
                await entries.ForEachAsync(_degreeOfParallelism, async entry =>
                {
                    await SetConfigValueAsync(data, entry).ConfigureAwait(false);

                }).ConfigureAwait(false);
            }
            else
            {
                foreach (var entry in entries)
                {
                    await SetConfigValueAsync(data, entry).ConfigureAwait(false);
                }
            }

            return data;
        }

        private async Task SetConfigValueAsync(Dictionary<string, string> data, string entry)
        {
            try
            {
                var secret = await _credstash.GetSecretAsync(entry, null, _encryptionContext, false).ConfigureAwait(false);

                if (secret.HasValue)
                {
                    data.Add(entry, secret.Value);
                }<commit_msg>Add missing ConfigureAwait(false) to avoid locking in case of parallel execution<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace Narochno.Credstash.Configuration
{
    public class CredstashConfigurationProvider : ConfigurationProvider
    {
        private readonly Credstash _credstash;
        private readonly int _degreeOfParallelism;
        private readonly Dictionary<string, string> _encryptionContext;
        private readonly bool _suppressErrors;

        public CredstashConfigurationProvider(Credstash credstash, Dictionary<string, string> encryptionContext)
        {
            _credstash = credstash;
            _degreeOfParallelism = credstash.Options.DegreeOfParallelism;
            _encryptionContext = encryptionContext;
            _suppressErrors = credstash.Options.SuppressErrors;
        }

        public override void Load()
        {
            Data = LoadAsync().Result;
        }

        public async Task<Dictionary<string, string>> LoadAsync()
        {
            var data = new Dictionary<string, string>();

            var entries = (await _credstash.ListAsync().ConfigureAwait(false)).Select(x => x.Name).Distinct().ToList();

            if (entries.Count() > 10 && _degreeOfParallelism > 1)
            {
                await entries.ForEachAsync(_degreeOfParallelism, async entry =>
                {
                    await SetConfigValueAsync(data, entry).ConfigureAwait(false);

                }).ConfigureAwait(false);
            }
            else
            {
                foreach (var entry in entries)
                {
                    await SetConfigValueAsync(data, entry).ConfigureAwait(false);
                }
            }

            return data;
        }

        private async Task SetConfigValueAsync(Dictionary<string, string> data, string entry)
        {
            try
            {
                var secret = await _credstash.GetSecretAsync(entry, null, _encryptionContext, false).ConfigureAwait(false);

                if (secret.HasValue)
                {
                    data.Add(entry, secret.Value);
                }
{'fraction_non_alphanumeric': 0.0769933744573909, 'fraction_numerical': 0.0013708019191226869, 'mean_word_length': 2.0027434842249656, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6542368', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1054, 'n_words': 234}