Skip to content

Commit

Permalink
Fix goddamn idiotic hard-coding mistake;
Browse files Browse the repository at this point in the history
Improve error handling of adding files to mod;
  • Loading branch information
agc93 committed Sep 1, 2020
1 parent 9ba30da commit d899f07
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/NexusUploader/FileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/NexusUploader/Services/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ApiClient(HttpClient httpClient)

public async Task<int> 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);
Expand Down
20 changes: 17 additions & 3 deletions src/NexusUploader/Services/ManageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
using System.Web;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace NexusUploader.Nexus.Services
{
public class ManageClient
{
private readonly HttpClient _httpClient;
private readonly CookieService _cookies;
private readonly ILogger<ManageClient> _logger;

public ManageClient(HttpClient httpClient, CookieService cookieService)
public ManageClient(HttpClient httpClient, CookieService cookieService, ILogger<ManageClient> logger)
{
_httpClient = httpClient;
_cookies = cookieService;
_logger = logger;
}

public async Task<bool> CheckValidSession() {
Expand Down Expand Up @@ -70,7 +73,7 @@ public async Task<bool> AddChangelog(GameRef game, int modId, string version, st
return false;
}

public async Task<HttpResponseMessage> AddFile(GameRef game, int modId, UploadedFile upload, FileOptions options)
public async Task<bool> AddFile(GameRef game, int modId, UploadedFile upload, FileOptions options)
{
var uri = "/Core/Libs/Common/Managers/Mods?AddFile";
var message = new HttpRequestMessage(HttpMethod.Post, uri);
Expand All @@ -81,6 +84,7 @@ public async Task<HttpResponseMessage> 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");
Expand All @@ -96,7 +100,17 @@ public async Task<HttpResponseMessage> 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<Dictionary<string, object>>(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;
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/NexusUploader/UploadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ public override async Task<int> 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) {
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit d899f07

Please sign in to comment.