using System; using System.Threading.Tasks; using System.Threading; using System.Collections.Generic; using Newtonsoft.Json.Linq; namespace raven_integration { public class CredRepo { static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1,1); // private readonly object valueLock = new object(); private static Dictionary authDict = new Dictionary(); public CredRepo() { } public static async Task GetTokenAsync(string login, string password = null) { await semaphoreSlim.WaitAsync(); try{ if (password == null) password = login; if (!authDict.ContainsKey(login)) { dynamic creds = new JObject(); creds.login = login; creds.password = password; ApiResponse a = await Util.PostAsync("auth", null, creds.ToString()); //Put this in when having concurrency issue during auth and old style dl token creation during login // ValidateDataReturnResponseOk(a); authDict[login] = a.ObjectResponse["data"]["token"].Value(); } return authDict[login]; } finally{ semaphoreSlim.Release(); } } // public uint GetNext() // { // lock (valueLock) // { // currentValue += 1; // if (currentValue == 0) // currentValue += 1; // return currentValue; // } // } } }