From 400621d5d5be4ab57191efc3319989edefe5c29b Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Fri, 8 May 2020 23:45:13 +0000 Subject: [PATCH] --- CredRepo.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 CredRepo.cs diff --git a/CredRepo.cs b/CredRepo.cs new file mode 100644 index 0000000..2d6091d --- /dev/null +++ b/CredRepo.cs @@ -0,0 +1,58 @@ +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; + // } + // } + } +} \ No newline at end of file