public string GetDeliveryPlanFromMemoryCache(string authToken, HTTPWrapper hTTPWrapper) { if (Cache.TryGetValue("DeliveryPlanData", out string value)) { return(value); } else { string DeliveryPlanData; lock (dpLock) { if (Cache.TryGetValue("DeliveryPlanData", out string newValue)) { return(newValue); } DeliveryPlanData = hTTPWrapper.GetDeliveryPlans(authToken).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(DeliveryPlanData)) { return(null); } SetToMemoryCache("DeliveryPlanData", DeliveryPlanData); } return(DeliveryPlanData); } }
public string GetDispatchDataFromMemoryCache(string authToken, HTTPWrapper hTTPWrapper) { if (Cache.TryGetValue("DispatchData", out string value)) { return(value); } else { string DispatchData; lock (dispatchLock) { if (Cache.TryGetValue("DispatchData", out string newValue)) { return(newValue); } DispatchData = hTTPWrapper.GetAllDispatchInfo(authToken).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(DispatchData)) { return(null); } SetToMemoryCache("DispatchData", DispatchData); } return(DispatchData); } }
public string GetQuestionnaireFromMemoryCache(string authToken, HTTPWrapper hTTPWrapper) { if (Cache.TryGetValue("SurveyQuestionnaires", out string value)) { return(value); } else { string SurveyQuestionnaires; lock (questionniareLock) { if (Cache.TryGetValue("SurveyQuestionnaires", out string newValue)) { return(newValue); } SurveyQuestionnaires = hTTPWrapper.GetSurveyQuestionnaire(authToken).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(SurveyQuestionnaires)) { return(null); } SetToMemoryCache("SurveyQuestionnaires", SurveyQuestionnaires); } return(SurveyQuestionnaires); } }
public string GetSettingsFromMemoryCache(string authToken, HTTPWrapper hTTPWrapper) { if (Cache.TryGetValue("settings", out string value)) { return(value); } else { string Settings; lock (settingsLock) { if (Cache.TryGetValue("settings", out string newValue)) { return(newValue); } Settings = hTTPWrapper.GetSettings(authToken).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(Settings)) { return(null); } SetToMemoryCache("settings", Settings); } return(Settings); } }
public string GetActiveQuestionsFromMemoryCache(string authToken, HTTPWrapper hTTPWrapper) { if (Cache.TryGetValue("ActiveQuestions", out string value)) { return(value); } else { string ActiveQuestions; lock (questionsLock) { if (Cache.TryGetValue("ActiveQuestions", out string newValue)) { return(newValue); } ActiveQuestions = hTTPWrapper.GetActiveQuestions(authToken).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(ActiveQuestions)) { return(null); } SetToMemoryCache("ActiveQuestions", ActiveQuestions); } return(ActiveQuestions); } }
public bool ValidateBearerToken(string authToken, AccountConfiguration accConfiguration) { try { if (accConfiguration == null) { return(false); } if (string.IsNullOrWhiteSpace(authToken)) { return(false); } if (!(authToken.StartsWith("Bearer ") || authToken.StartsWith("Basic "))) { return(false); } if (InvitationsMemoryCache.GetInstance().GetFromMemoryCache(authToken) != null) { return(true); } else { lock (authTokenLock) { if (InvitationsMemoryCache.GetInstance().GetFromMemoryCache(authToken) != null) { return(true); } var settings = new HTTPWrapper().GetSettings(authToken).GetAwaiter().GetResult(); if (string.IsNullOrWhiteSpace(settings)) { return(false); } Settings settingsRes = JsonConvert.DeserializeObject <Settings>(settings); if (!settingsRes.user.Equals(accConfiguration?.WXMAdminUser, StringComparison.OrdinalIgnoreCase)) { return(false); } InvitationsMemoryCache.GetInstance().SetToMemoryCache("settings", settings); InvitationsMemoryCache.GetInstance().SetAuthTokenToMemoryCache(authToken); } return(true); } } catch (Exception) { return(false); } }
public ProcessInvitations(string AuthToken, ViaMongoDB viaMongo, string batchid, EventLogList eventLogList, AccountConfiguration accConfig) { FinalToken = AuthToken; mongoDBConn = viaMongo; hTTPWrapper = new HTTPWrapper(batchid, eventLogList); BatchId = batchid; EventLogList = eventLogList; accountConfiguration = accConfig; }
public async Task ReadQueue(CancellationToken cancellationToken) { EventLogList eventLog = new EventLogList(); try { DateTime stopTime = DateTime.Now.AddSeconds(30); Dictionary <string, RequestBulkToken> finalBulkStorage = new Dictionary <string, RequestBulkToken>(); while (DateTime.Now < stopTime) { RequestBulkToken requestBulkToken; if (!SingletonConcurrentQueue <RequestBulkToken> .Instance.TryPeek(out requestBulkToken)) { break; } else if (SingletonConcurrentQueue <RequestBulkToken> .Instance.TryDequeue(out requestBulkToken)) { if (finalBulkStorage.ContainsKey(requestBulkToken.DispatchId)) { finalBulkStorage[requestBulkToken.DispatchId].PrefillReponse.AddRange(requestBulkToken.PrefillReponse); } else { finalBulkStorage.Add(requestBulkToken.DispatchId, new RequestBulkToken() { DispatchId = requestBulkToken.DispatchId, UUID = requestBulkToken.UUID, Batchid = requestBulkToken.Batchid, PrefillReponse = requestBulkToken.PrefillReponse }); } } } if (finalBulkStorage.Count != 0) { HTTPWrapper hTTPWrapper = new HTTPWrapper(string.Empty, eventLog); string authToken = InvitationsMemoryCache.GetInstance().GetFromMemoryCache("AuthToken"); if (authToken == null) { AccountConfiguration accountConfiguration; string accountConfigurationCache = InvitationsMemoryCache.GetInstance().GetFromMemoryCache("accountconfig"); if (accountConfigurationCache == null) { accountConfiguration = await viaMongoDB.GetAccountConfiguration(); } else { accountConfiguration = Newtonsoft.Json.JsonConvert.DeserializeObject <AccountConfiguration>(accountConfigurationCache); } string username = accountConfiguration.WXMAdminUser; string apikey = accountConfiguration.WXMAPIKey; string responseBody = await hTTPWrapper.GetLoginToken(username, apikey); if (!string.IsNullOrEmpty(responseBody)) { BearerToken loginToken = Newtonsoft.Json.JsonConvert.DeserializeObject <BearerToken>(responseBody); authToken = "Bearer " + loginToken.AccessToken; var Expirationtime = loginToken.ExpiresIn - 300; // Expire 5 min before for uninterrupted token creation InvitationsMemoryCache.GetInstance().SetBulkTokenAuthToMemoryCache("AuthToken", authToken, Expirationtime); } else { //when login token api failed. eventLog.AddEventByLevel(1, SharedSettings.BearerTokenNotGenerated, null); await eventLog.AddEventLogs(viaMongoDB); } } // Calling bulk token api sequentially if (!string.IsNullOrWhiteSpace(authToken)) { List <(string, List <BulkTokenResult>)> status = new List <(string, List <BulkTokenResult>)>(); foreach (var request in finalBulkStorage) { var response = await hTTPWrapper.BulkTokenAPI(authToken, request.Value); status.Add(response); Thread.Sleep(1000); // Sleep for 1 second before making another call } /* * var bulkTokenAPITasks = finalBulkStorage.Values.ToList().Select(v => * { * return hTTPWrapper.BulkTokenAPI(authToken, v); * }); * * (string, List<BulkTokenResult>)[] status = await Task.WhenAll(bulkTokenAPITasks); * */ Dictionary <LogEvent, InvitationLogEvent> events = new Dictionary <LogEvent, InvitationLogEvent>(); DateTime utcNow = DateTime.UtcNow; //Update tokens in DB foreach (var item in status) { if (item.Item2 != null) { foreach (var perinvite in item.Item2) { var logEvent = new LogEvent() { DispatchId = item.Item1, BatchId = perinvite.Batchid, Updated = utcNow, TokenId = perinvite.Token, TargetHashed = perinvite.UUID }; var invitationEvent = new InvitationLogEvent() { Action = InvitationLogEvent.EventAction.TokenCreated, Channel = InvitationLogEvent.EventChannel.DispatchAPI, TimeStamp = utcNow, TargetId = perinvite.UUID }; events.Add(logEvent, invitationEvent); } } } if (events.Count() > 0) { await viaMongoDB.UpdateBulkEventLog(events); } eventLog.AddEventByLevel(5, $"{SharedSettings.DBUpdateCompleted} {events.Count()}", null); await eventLog.AddEventLogs(viaMongoDB); } } } catch (Exception ex) { eventLog.AddExceptionEvent(ex, null, null, null, null, SharedSettings.BulkTokenException); await eventLog.AddEventLogs(viaMongoDB); return; } }