using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Net.Http; using System.Reactive.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Wabbajack.Common; using Wabbajack.Lib.Downloaders; using File = Alphaleonis.Win32.Filesystem.File; using Path = Alphaleonis.Win32.Filesystem.Path; namespace Wabbajack.Lib.FileUploader { public class AuthorAPI { public static IObservable HaveAuthorAPIKey => Utils.HaveEncryptedJsonObservable(Consts.AuthorAPIKeyFile); public static string? ApiKeyOverride = null; public static async Task GetAPIKey(string? apiKey = null) { if (ApiKeyOverride != null) return ApiKeyOverride; return apiKey ?? (await Consts.LocalAppDataPath.Combine(Consts.AuthorAPIKeyFile).ReadAllTextAsync()).Trim(); } public static async Task GetAuthorizedClient(string? apiKey = null) { var client = new Wabbajack.Lib.Http.Client(); client.Headers.Add(("X-API-KEY", await GetAPIKey(apiKey))); return client; } public static async Task RunJob(string jobtype) { var client = await GetAuthorizedClient(); return await client.GetStringAsync($"{Consts.WabbajackBuildServerUri}jobs/enqueue_job/{jobtype}"); } public static async Task UpdateNexusCache() { return await RunJob("GetNexusUpdatesJob"); } public static async Task UpdateServerModLists() { return await RunJob("UpdateModLists"); } public static async Task GetServerLog() { return await (await GetAuthorizedClient()).GetStringAsync($"{Consts.WabbajackBuildServerUri}heartbeat/logs"); } public static async Task> GetMyFiles() { return (await (await GetAuthorizedClient()).GetStringAsync($"{Consts.WabbajackBuildServerUri}uploaded_files/list")).FromJsonString(); } public static async Task DeleteFile(string name) { var result = await (await GetAuthorizedClient()) .DeleteStringAsync($"{Consts.WabbajackBuildServerUri}uploaded_files/{name}"); return result; } public static async Task PurgeNexusModInfo(long modId) { return await (await GetAuthorizedClient()).GetStringAsync($"{Consts.WabbajackBuildServerUri}purge_nexus_cache/{modId}"); } public static async Task NoPatch(Hash hash, string rationale) { var client = await GetAuthorizedClient(); return await client.GetStringAsync( $"{Consts.WabbajackBuildServerUri}mod_upgrade/no_patch/{hash.ToHex()}/{Encoding.UTF8.GetBytes(rationale).ToHex()}"); } } }