From d899f074d197ff22db4a56191cf27f6e001142b2 Mon Sep 17 00:00:00 2001 From: Alistair Chapman Date: Wed, 2 Sep 2020 01:52:38 +1000 Subject: [PATCH] Fix goddamn idiotic hard-coding mistake; Improve error handling of adding files to mod; --- src/NexusUploader/FileOptions.cs | 1 + src/NexusUploader/Services/ApiClient.cs | 2 +- src/NexusUploader/Services/ManageClient.cs | 20 +++++++++++++++++--- src/NexusUploader/UploadCommand.cs | 15 ++++++++++----- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/NexusUploader/FileOptions.cs b/src/NexusUploader/FileOptions.cs index 3652dbb..b1ceda7 100644 --- a/src/NexusUploader/FileOptions.cs +++ b/src/NexusUploader/FileOptions.cs @@ -12,6 +12,7 @@ public FileOptions(string name, string version, string category = "Main Files") public string Description {get;set;} = string.Empty; // public string Category {get;set;} public bool UpdateMainVersion {get;set;} = true; + public bool SetAsMainVortex {get;set;} = true; public int? PreviousFileId {get;set;} public override string ToString() { diff --git a/src/NexusUploader/Services/ApiClient.cs b/src/NexusUploader/Services/ApiClient.cs index 9b23173..49c16fb 100644 --- a/src/NexusUploader/Services/ApiClient.cs +++ b/src/NexusUploader/Services/ApiClient.cs @@ -18,7 +18,7 @@ public ApiClient(HttpClient httpClient) public async Task GetGameId(string gameName, string apiKey) { - using (var req = new HttpRequestMessage(HttpMethod.Get, "games/site.json")) + using (var req = new HttpRequestMessage(HttpMethod.Get, $"games/{gameName}.json")) { req.Headers.Add("apikey", apiKey); var resp = await _httpClient.SendAsync(req); diff --git a/src/NexusUploader/Services/ManageClient.cs b/src/NexusUploader/Services/ManageClient.cs index b4a20cc..95e73fe 100644 --- a/src/NexusUploader/Services/ManageClient.cs +++ b/src/NexusUploader/Services/ManageClient.cs @@ -5,6 +5,7 @@ using System.Web; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace NexusUploader.Nexus.Services { @@ -12,11 +13,13 @@ public class ManageClient { private readonly HttpClient _httpClient; private readonly CookieService _cookies; + private readonly ILogger _logger; - public ManageClient(HttpClient httpClient, CookieService cookieService) + public ManageClient(HttpClient httpClient, CookieService cookieService, ILogger logger) { _httpClient = httpClient; _cookies = cookieService; + _logger = logger; } public async Task CheckValidSession() { @@ -70,7 +73,7 @@ public async Task AddChangelog(GameRef game, int modId, string version, st return false; } - public async Task AddFile(GameRef game, int modId, UploadedFile upload, FileOptions options) + public async Task AddFile(GameRef game, int modId, UploadedFile upload, FileOptions options) { var uri = "/Core/Libs/Common/Managers/Mods?AddFile"; var message = new HttpRequestMessage(HttpMethod.Post, uri); @@ -81,6 +84,7 @@ public async Task AddFile(GameRef game, int modId, Uploaded content.Add(options.Name.ToContent(), "name"); content.Add(options.Version.ToContent(), "file-version"); content.Add(options.UpdateMainVersion ? 1.ToContent() : 0.ToContent(), "update-version"); + content.Add(options.UpdateMainVersion ? 1.ToContent() : 0.ToContent(), "set_as_main_nmm"); content.Add(1.ToContent(), "category"); if (options.PreviousFileId.HasValue) { content.Add(1.ToContent(), "new-existing"); @@ -96,7 +100,17 @@ public async Task AddFile(GameRef game, int modId, Uploaded content.Add(upload.OriginalFile.ToContent(), "original_file"); message.Content = content; var resp = await _httpClient.SendAsync(message); - return resp; + if (resp.IsSuccessStatusCode) { + var strResponse = await resp.Content.ReadAsStringAsync(); + var data = System.Text.Json.JsonSerializer.Deserialize>(strResponse); + var success = data.ContainsKey("status") && data["status"].ToString() == true.ToString(); + if (success) { + return true; + } else { + _logger.LogWarning("Response received from Nexus Mods: " + data["message"].ToString()); + } + } + return false; } } } diff --git a/src/NexusUploader/UploadCommand.cs b/src/NexusUploader/UploadCommand.cs index 30e2110..58416e2 100644 --- a/src/NexusUploader/UploadCommand.cs +++ b/src/NexusUploader/UploadCommand.cs @@ -62,10 +62,15 @@ public override async Task ExecuteAsync(CommandContext context, Settings se _logger.LogDebug($"File '{upload.Id}' confirmed as assembled: {available}"); _logger.LogInformation($"Adding uploaded file to mod {config.ModId}"); _logger.LogDebug($"Using file options: {fileOpts.ToString()}"); - await _manager.AddFile(game, config.ModId, upload, fileOpts); - _logger.LogInformation($"{upload.OriginalFile} successfully uploaded and added to mod {config.ModId}!"); - _logger.LogInformation("Now go ask @Pickysaurus when a real API will be available! ;)"); - return 0; + var success = await _manager.AddFile(game, config.ModId, upload, fileOpts); + if (success) { + _logger.LogInformation($"{upload.OriginalFile} successfully uploaded and added to mod {config.ModId}!"); + _logger.LogInformation("Now go ask @Pickysaurus when a real API will be available! ;)"); + return 0; + } else { + _logger.LogWarning($"There was an error adding {upload.OriginalFile} to mod {config.ModId}!"); + return 1; + } } private bool IsConfigurationValid(Settings settings) { @@ -113,7 +118,7 @@ private bool IsSettingsValid() { && FileVersion.IsSet() && (ApiKey.IsSet || _config.ApiKey.IsSet()) && (FileName.IsSet || _config.FileName.IsSet()) - && ModId != default(int) + && (ModId != default(int) || _config.ModId != default(int)) && _config.Game.IsSet(); }