This commit is contained in:
Timothy Baldridge 2019-10-14 15:04:12 -06:00
parent fc60eadab3
commit e4c1e264cd
4 changed files with 23 additions and 3 deletions

View File

@ -61,5 +61,7 @@ namespace Wabbajack.Common
return headerString;
}
}
public static TimeSpan NexusCacheExpiry = new TimeSpan(1, 0, 0, 0);
}
}

View File

@ -128,6 +128,8 @@ namespace Wabbajack.Test
var converted = state.ViaJSON();
Assert.IsTrue(converted.Verify());
// Exercise the cache code
Assert.IsTrue(converted.Verify());
var filename = Guid.NewGuid().ToString();
Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List<string> () }));

View File

@ -103,8 +103,8 @@ namespace Wabbajack.Downloaders
{
try
{
new NexusApiClient().GetNexusDownloadLink(this, true);
return true;
var info = new NexusApiClient().GetFileInfo(this);
return info.category_name != null;
}
catch (Exception ex)
{

View File

@ -9,7 +9,9 @@ using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Downloaders;
@ -190,6 +192,20 @@ namespace Wabbajack.NexusApi
}
}
private T GetCached<T>(string url)
{
var code = Encoding.UTF8.GetBytes(url).ToHEX();
var cache_file = Path.Combine(Consts.NexusCacheDirectory, code + ".json");
if (File.Exists(cache_file) && DateTime.Now - File.GetLastWriteTime(cache_file) < Consts.NexusCacheExpiry)
{
return cache_file.FromJSON<T>();
}
var result = Get<T>(url);
result.ToJSON(cache_file);
return result;
}
public string GetNexusDownloadLink(NexusDownloader.State archive, bool cache = false)
{
@ -223,7 +239,7 @@ namespace Wabbajack.NexusApi
public NexusFileInfo GetFileInfo(NexusDownloader.State mod)
{
var url = $"https://api.nexusmods.com/v1/games/{ConvertGameName(mod.GameName)}/mods/{mod.ModID}/files/{mod.FileID}.json";
return Get<NexusFileInfo>(url);
return GetCached<NexusFileInfo>(url);
}
public ModInfo GetModInfo(string gameName, string modId)