public async Task <ActionResult <GenericResponse <IReadOnlyDictionary <string, bool> > > > BotPost(string botNames, [FromBody] BotRequest request) { if (string.IsNullOrEmpty(botNames) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botNames) + " || " + nameof(request)); return(BadRequest(new GenericResponse <IReadOnlyDictionary <string, bool> >(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames) + " || " + nameof(request))))); } (bool valid, string errorMessage) = request.BotConfig.CheckValidation(); if (!valid) { return(BadRequest(new GenericResponse <IReadOnlyDictionary <string, bool> >(false, errorMessage))); } request.BotConfig.ShouldSerializeEverything = false; request.BotConfig.ShouldSerializeHelperProperties = false; string originalSteamLogin = request.BotConfig.SteamLogin; string originalDecryptedSteamPassword = request.BotConfig.DecryptedSteamPassword; string originalSteamParentalCode = request.BotConfig.SteamParentalCode; HashSet <string> bots = botNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(botName => botName != SharedInfo.ASF).ToHashSet(); Dictionary <string, bool> result = new Dictionary <string, bool>(bots.Count); foreach (string botName in bots) { bool botExists = Bot.Bots.TryGetValue(botName, out Bot bot); if (botExists) { if (!request.BotConfig.IsSteamLoginSet && bot.BotConfig.IsSteamLoginSet) { request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin; } if (!request.BotConfig.IsSteamPasswordSet && bot.BotConfig.IsSteamPasswordSet) { request.BotConfig.DecryptedSteamPassword = bot.BotConfig.DecryptedSteamPassword; } if (!request.BotConfig.IsSteamParentalCodeSet && bot.BotConfig.IsSteamParentalCodeSet) { request.BotConfig.SteamParentalCode = bot.BotConfig.SteamParentalCode; } } string filePath = Path.Combine(SharedInfo.ConfigDirectory, botName + SharedInfo.ConfigExtension); result[botName] = await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false); if (botExists) { request.BotConfig.SteamLogin = originalSteamLogin; request.BotConfig.DecryptedSteamPassword = originalDecryptedSteamPassword; request.BotConfig.SteamParentalCode = originalSteamParentalCode; } } return(Ok(new GenericResponse <IReadOnlyDictionary <string, bool> >(result.Values.All(value => value), result))); }
public async Task<ActionResult<GenericResponse>> BotPost(string botName, [FromBody] BotRequest request) { if (string.IsNullOrEmpty(botName) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botName) + " || " + nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request)))); } (bool valid, string errorMessage) = request.BotConfig.CheckValidation(); if (!valid) { return BadRequest(new GenericResponse(false, errorMessage)); } if (Bot.Bots.TryGetValue(botName, out Bot bot)) { if (!request.BotConfig.IsSteamLoginSet && bot.BotConfig.IsSteamLoginSet) { request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin; } if (!request.BotConfig.IsSteamPasswordSet && bot.BotConfig.IsSteamPasswordSet) { request.BotConfig.DecryptedSteamPassword = bot.BotConfig.DecryptedSteamPassword; } if (!request.BotConfig.IsSteamParentalCodeSet && bot.BotConfig.IsSteamParentalCodeSet) { request.BotConfig.SteamParentalCode = bot.BotConfig.SteamParentalCode; } } request.BotConfig.ShouldSerializeEverything = false; string filePath = Path.Combine(SharedInfo.ConfigDirectory, botName + SharedInfo.ConfigExtension); bool result = await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false); return Ok(new GenericResponse(result)); }
public async Task<ActionResult<GenericResponse>> BotPost(string botNames, [FromBody] BotRequest request) { if (string.IsNullOrEmpty(botNames) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botNames) + " || " + nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames) + " || " + nameof(request)))); } (bool valid, string errorMessage) = request.BotConfig.CheckValidation(); if (!valid) { return BadRequest(new GenericResponse(false, errorMessage)); } request.BotConfig.ShouldSerializeEverything = false; request.BotConfig.ShouldSerializeHelperProperties = false; HashSet<string> bots = botNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(botName => botName != SharedInfo.ASF).ToHashSet(Bot.BotsComparer); Dictionary<string, bool> result = new Dictionary<string, bool>(bots.Count, Bot.BotsComparer); foreach (string botName in bots) { if (Bot.Bots.TryGetValue(botName, out Bot bot)) { if (!request.BotConfig.IsSteamLoginSet && bot.BotConfig.IsSteamLoginSet) { request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin; } if (!request.BotConfig.IsSteamPasswordSet && bot.BotConfig.IsSteamPasswordSet) { request.BotConfig.DecryptedSteamPassword = bot.BotConfig.DecryptedSteamPassword; } if (!request.BotConfig.IsSteamParentalCodeSet && bot.BotConfig.IsSteamParentalCodeSet) { request.BotConfig.SteamParentalCode = bot.BotConfig.SteamParentalCode; } if ((bot.BotConfig.AdditionalProperties != null) && (bot.BotConfig.AdditionalProperties.Count > 0)) { if (request.BotConfig.AdditionalProperties == null) { request.BotConfig.AdditionalProperties = new Dictionary<string, JToken>(bot.BotConfig.AdditionalProperties.Count, bot.BotConfig.AdditionalProperties.Comparer); } foreach ((string key, JToken value) in bot.BotConfig.AdditionalProperties.Where(property => !request.BotConfig.AdditionalProperties.ContainsKey(property.Key))) { request.BotConfig.AdditionalProperties.Add(key, value); } request.BotConfig.AdditionalProperties.TrimExcess(); } } string filePath = Bot.GetFilePath(botName, Bot.EFileType.Config); if (string.IsNullOrEmpty(filePath)) { ASF.ArchiLogger.LogNullError(filePath); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsInvalid, nameof(filePath)))); } result[botName] = await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false); } return Ok(new GenericResponse<IReadOnlyDictionary<string, bool>>(result.Values.All(value => value), result)); }
public async Task <ActionResult <GenericResponse> > Post(string botName, [FromBody] BotRequest request) { if (string.IsNullOrEmpty(botName) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botName) + " || " + nameof(request)); return(BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request))))); } if (request.KeepSensitiveDetails && Bot.Bots.TryGetValue(botName, out Bot bot)) { if (string.IsNullOrEmpty(request.BotConfig.SteamLogin) && !string.IsNullOrEmpty(bot.BotConfig.SteamLogin)) { request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin; } if (string.IsNullOrEmpty(request.BotConfig.SteamParentalPIN) && !string.IsNullOrEmpty(bot.BotConfig.SteamParentalPIN)) { request.BotConfig.SteamParentalPIN = bot.BotConfig.SteamParentalPIN; } if (string.IsNullOrEmpty(request.BotConfig.SteamPassword) && !string.IsNullOrEmpty(bot.BotConfig.SteamPassword)) { request.BotConfig.SteamPassword = bot.BotConfig.SteamPassword; } } request.BotConfig.ShouldSerializeEverything = false; string filePath = Path.Combine(SharedInfo.ConfigDirectory, botName + SharedInfo.ConfigExtension); if (!await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false)) { return(BadRequest(new GenericResponse(false, Strings.WarningFailed))); } return(Ok(new GenericResponse(true))); }
public async Task <ActionResult <GenericResponse> > BotPost(string botNames, [FromBody] BotRequest request) { if (string.IsNullOrEmpty(botNames)) { throw new ArgumentNullException(nameof(botNames)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } if (Bot.Bots == null) { throw new InvalidOperationException(nameof(Bot.Bots)); } (bool valid, string?errorMessage) = request.BotConfig.CheckValidation(); if (!valid) { return(BadRequest(new GenericResponse(false, errorMessage))); } request.BotConfig.ShouldSerializeDefaultValues = false; request.BotConfig.ShouldSerializeHelperProperties = false; request.BotConfig.ShouldSerializeSensitiveDetails = true; HashSet <string> bots = botNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet(Bot.BotsComparer); if (bots.Any(botName => !ASF.IsValidBotName(botName))) { return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(botNames))))); } Dictionary <string, bool> result = new(bots.Count, Bot.BotsComparer); foreach (string botName in bots) { if (Bot.Bots.TryGetValue(botName, out Bot? bot)) { if (!request.BotConfig.IsSteamLoginSet && bot.BotConfig.IsSteamLoginSet) { request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin; } if (!request.BotConfig.IsSteamPasswordSet && bot.BotConfig.IsSteamPasswordSet) { request.BotConfig.DecryptedSteamPassword = bot.BotConfig.DecryptedSteamPassword; } if (!request.BotConfig.IsSteamParentalCodeSet && bot.BotConfig.IsSteamParentalCodeSet) { request.BotConfig.SteamParentalCode = bot.BotConfig.SteamParentalCode; } if (bot.BotConfig.AdditionalProperties?.Count > 0) { request.BotConfig.AdditionalProperties ??= new Dictionary <string, JToken>(bot.BotConfig.AdditionalProperties.Count, bot.BotConfig.AdditionalProperties.Comparer); foreach ((string key, JToken value) in bot.BotConfig.AdditionalProperties.Where(property => !request.BotConfig.AdditionalProperties.ContainsKey(property.Key))) { request.BotConfig.AdditionalProperties.Add(key, value); } request.BotConfig.AdditionalProperties.TrimExcess(); } } string filePath = Bot.GetFilePath(botName, Bot.EFileType.Config); if (string.IsNullOrEmpty(filePath)) { ASF.ArchiLogger.LogNullError(filePath); return(BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(filePath))))); } result[botName] = await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false); } return(Ok(new GenericResponse <IReadOnlyDictionary <string, bool> >(result.Values.All(value => value), result))); }