mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-16 08:25:28 +00:00
Add AddLicense to ASF API
This commit is contained in:
@@ -36,12 +36,45 @@ using ArchiSteamFarm.Localization;
|
||||
using ArchiSteamFarm.Steam;
|
||||
using ArchiSteamFarm.Steam.Storage;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SteamKit2;
|
||||
using SteamKit2.Internal;
|
||||
|
||||
namespace ArchiSteamFarm.IPC.Controllers.Api;
|
||||
|
||||
[Route("Api/Bot")]
|
||||
public sealed class BotController : ArchiController {
|
||||
/// <summary>
|
||||
/// Adds (free) licenses on given bots.
|
||||
/// </summary>
|
||||
[Consumes("application/json")]
|
||||
[HttpPost("{botNames:required}/AddLicense")]
|
||||
[ProducesResponseType<GenericResponse<IReadOnlyDictionary<string, BotAddLicenseResponse>>>((int) HttpStatusCode.OK)]
|
||||
[ProducesResponseType<GenericResponse>((int) HttpStatusCode.BadRequest)]
|
||||
public async Task<ActionResult<GenericResponse>> AddLicensePost(string botNames, [FromBody] BotAddLicenseRequest request) {
|
||||
ArgumentException.ThrowIfNullOrEmpty(botNames);
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
|
||||
if ((request.Apps?.IsEmpty != false) && (request.Packages?.IsEmpty != false)) {
|
||||
return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, $"{nameof(request.Apps)} && {nameof(request.Packages)}")));
|
||||
}
|
||||
|
||||
HashSet<Bot>? bots = Bot.GetBots(botNames);
|
||||
|
||||
if ((bots == null) || (bots.Count == 0)) {
|
||||
return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames)));
|
||||
}
|
||||
|
||||
IList<BotAddLicenseResponse> results = await Utilities.InParallel(bots.Select(bot => AddLicense(bot, request))).ConfigureAwait(false);
|
||||
|
||||
Dictionary<string, BotAddLicenseResponse> result = new(bots.Count, Bot.BotsComparer);
|
||||
|
||||
foreach (Bot bot in bots) {
|
||||
result[bot.BotName] = results[result.Count];
|
||||
}
|
||||
|
||||
return Ok(new GenericResponse<IReadOnlyDictionary<string, BotAddLicenseResponse>>(result));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all files related to given bots.
|
||||
/// </summary>
|
||||
@@ -416,4 +449,46 @@ public sealed class BotController : ArchiController {
|
||||
|
||||
return Ok(new GenericResponse(results.All(static result => result.Success), string.Join(Environment.NewLine, results.Select(static result => result.Message))));
|
||||
}
|
||||
|
||||
private static async Task<BotAddLicenseResponse> AddLicense(Bot bot, BotAddLicenseRequest request) {
|
||||
ArgumentNullException.ThrowIfNull(bot);
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
|
||||
Dictionary<uint, AddLicenseResult>? apps = null;
|
||||
Dictionary<uint, AddLicenseResult>? packages = null;
|
||||
|
||||
if (request.Apps != null) {
|
||||
apps = new Dictionary<uint, AddLicenseResult>(request.Apps.Count);
|
||||
|
||||
foreach (uint appID in request.Apps) {
|
||||
if (!bot.IsConnectedAndLoggedOn) {
|
||||
apps[appID] = new AddLicenseResult(EResult.Timeout, EPurchaseResultDetail.Timeout);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
(EResult result, IReadOnlyCollection<uint>? grantedApps, IReadOnlyCollection<uint>? grantedPackages) = await bot.Actions.AddFreeLicenseApp(appID).ConfigureAwait(false);
|
||||
|
||||
apps[appID] = new AddLicenseResult(result, (grantedApps?.Count > 0) || (grantedPackages?.Count > 0) ? EPurchaseResultDetail.NoDetail : EPurchaseResultDetail.InvalidData);
|
||||
}
|
||||
}
|
||||
|
||||
if (request.Packages != null) {
|
||||
packages = new Dictionary<uint, AddLicenseResult>(request.Packages.Count);
|
||||
|
||||
foreach (uint subID in request.Packages) {
|
||||
if (!bot.IsConnectedAndLoggedOn) {
|
||||
packages[subID] = new AddLicenseResult(EResult.Timeout, EPurchaseResultDetail.Timeout);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
(EResult result, EPurchaseResultDetail purchaseResultDetail) = await bot.Actions.AddFreeLicensePackage(subID).ConfigureAwait(false);
|
||||
|
||||
packages[subID] = new AddLicenseResult(result, purchaseResultDetail);
|
||||
}
|
||||
}
|
||||
|
||||
return new BotAddLicenseResponse(apps, packages);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user