Add AddLicense to ASF API

This commit is contained in:
Łukasz Domeradzki
2024-05-19 20:07:41 +02:00
parent b5da72eb40
commit 416ec920cb
4 changed files with 223 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
namespace ArchiSteamFarm.IPC.Requests;
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
public sealed class BotAddLicenseRequest {
/// <summary>
/// A collection (set) of apps (appIDs) to ask license for.
/// </summary>
[JsonInclude]
public ImmutableList<uint>? Apps { get; private init; }
/// <summary>
/// A collection (set) of packages (subIDs) to ask license for.
/// </summary>
[JsonInclude]
public ImmutableList<uint>? Packages { get; private init; }
[JsonConstructor]
private BotAddLicenseRequest() { }
}

View File

@@ -0,0 +1,55 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using SteamKit2;
namespace ArchiSteamFarm.IPC.Responses;
public sealed class AddLicenseResult {
[JsonInclude]
[JsonRequired]
[Required]
public EPurchaseResultDetail PurchaseResultDetail { get; private init; }
[JsonInclude]
[JsonRequired]
[Required]
public EResult Result { get; private init; }
internal AddLicenseResult(EResult result, EPurchaseResultDetail purchaseResultDetail) {
if (!Enum.IsDefined(result)) {
throw new InvalidEnumArgumentException(nameof(result), (int) result, typeof(EResult));
}
if (!Enum.IsDefined(purchaseResultDetail)) {
throw new InvalidEnumArgumentException(nameof(purchaseResultDetail), (int) purchaseResultDetail, typeof(EPurchaseResultDetail));
}
Result = result;
PurchaseResultDetail = purchaseResultDetail;
}
}

View File

@@ -0,0 +1,47 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;
namespace ArchiSteamFarm.IPC.Responses;
public sealed class BotAddLicenseResponse {
/// <summary>
/// A collection (set) of apps (appIDs) to ask license for.
/// </summary>
[JsonInclude]
public ImmutableDictionary<uint, AddLicenseResult>? Apps { get; private init; }
/// <summary>
/// A collection (set) of packages (subIDs) to ask license for.
/// </summary>
[JsonInclude]
public ImmutableDictionary<uint, AddLicenseResult>? Packages { get; private init; }
internal BotAddLicenseResponse(IReadOnlyDictionary<uint, AddLicenseResult>? apps, IReadOnlyDictionary<uint, AddLicenseResult>? packages) {
Apps = apps?.ToImmutableDictionary();
Packages = packages?.ToImmutableDictionary();
}
}