wabbajack/Wabbajack.Lib/ClientAPI.cs

132 lines
4.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-05-20 03:25:41 +00:00
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Common.Exceptions;
2020-05-20 03:25:41 +00:00
using Wabbajack.Common.Serialization.Json;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack.Lib
{
2020-05-20 03:25:41 +00:00
[JsonName("ModUpgradeRequest")]
public class ModUpgradeRequest
{
public Archive OldArchive { get; set; }
public Archive NewArchive { get; set; }
public ModUpgradeRequest(Archive oldArchive, Archive newArchive)
{
OldArchive = oldArchive;
NewArchive = newArchive;
}
public bool IsValid
{
get
{
2020-05-23 21:03:25 +00:00
if (OldArchive.Size > 2_500_000_000 || NewArchive.Size > 2_500_000_000) return false;
2020-05-20 03:25:41 +00:00
if (OldArchive.Hash == NewArchive.Hash && OldArchive.State.PrimaryKeyString == NewArchive.State.PrimaryKeyString) return false;
if (OldArchive.State.GetType() != NewArchive.State.GetType())
return false;
if (OldArchive.State is IUpgradingState u)
{
return u.ValidateUpgrade(NewArchive.State);
}
return false;
}
}
}
public class ClientAPI
{
public static async Task<Common.Http.Client> GetClient()
{
var client = new Common.Http.Client();
if (Utils.HaveEncryptedJson(Consts.MetricsKeyHeader))
client.Headers.Add((Consts.MetricsKeyHeader, await Utils.FromEncryptedJson<string>(Consts.MetricsKeyHeader)));
return client;
}
2020-05-20 03:25:41 +00:00
public static async Task<Uri> GetModUpgrade(Archive oldArchive, Archive newArchive, TimeSpan? maxWait = null, TimeSpan? waitBetweenTries = null)
{
2020-05-20 03:25:41 +00:00
maxWait ??= TimeSpan.FromMinutes(10);
waitBetweenTries ??= TimeSpan.FromSeconds(15);
var request = new ModUpgradeRequest( oldArchive, newArchive);
var start = DateTime.UtcNow;
RETRY:
var response = await (await GetClient())
2020-05-20 03:25:41 +00:00
.PostAsync($"{Consts.WabbajackBuildServerUri}mod_upgrade", new StringContent(request.ToJson(), Encoding.UTF8, "application/json"));
2020-04-10 03:54:02 +00:00
if (response.IsSuccessStatusCode)
{
2020-05-20 03:25:41 +00:00
switch (response.StatusCode)
{
case HttpStatusCode.OK:
return new Uri(await response.Content.ReadAsStringAsync());
case HttpStatusCode.Accepted:
Utils.Log($"Waiting for patch processing on the server for {oldArchive.Name}, sleeping for another 15 seconds");
await Task.Delay(TimeSpan.FromSeconds(15));
response.Dispose();
if (DateTime.UtcNow - start > maxWait)
throw new HttpException(response);
goto RETRY;
}
2020-04-10 03:54:02 +00:00
}
2020-05-20 03:25:41 +00:00
var ex = new HttpException(response);
response.Dispose();
throw ex;
}
2020-03-31 22:05:36 +00:00
/// <summary>
/// Given an archive hash, search the Wabbajack server for a matching .ini file
/// </summary>
/// <param name="hash"></param>
/// <returns></returns>
public static async Task<string?> GetModIni(Hash hash)
2020-03-31 22:05:36 +00:00
{
var client = new Common.Http.Client();
try
{
return await client.GetStringAsync(
$"{Consts.WabbajackBuildServerUri}indexed_files/{hash.ToHex()}/meta.ini");
}
catch (HttpException)
{
return null;
}
}
2020-04-02 21:16:46 +00:00
public class NexusCacheStats
{
public long CachedCount { get; set; }
public long ForwardCount { get; set; }
public double CacheRatio { get; set; }
}
public static async Task<NexusCacheStats> GetNexusCacheStats()
{
return await (await GetClient())
2020-04-02 21:16:46 +00:00
.GetJsonAsync<NexusCacheStats>($"{Consts.WabbajackBuildServerUri}nexus_cache/stats");
}
public static async Task<Dictionary<RelativePath, Hash>> GetGameFiles(Game game, Version version)
{
// TODO: Disabled for now
return new Dictionary<RelativePath, Hash>();
/*
return await GetClient()
.GetJsonAsync<Dictionary<RelativePath, Hash>>($"{Consts.WabbajackBuildServerUri}game_files/{game}/{version}");
*/
}
}
}