ClientFactory moved to Wabbajack.Lib

This commit is contained in:
Justin Swanson 2020-06-26 12:08:30 -05:00
parent b62e89f9fd
commit d3b72af7a8
27 changed files with 47 additions and 46 deletions

View File

@ -18,20 +18,20 @@ namespace Wabbajack.Lib.AuthorApi
return new Client(client); return new Client(client);
} }
private Client(Common.Http.Client client) private Client(Wabbajack.Lib.Http.Client client)
{ {
_client = client; _client = client;
} }
public static async Task<Common.Http.Client> GetAuthorizedClient(string? apiKey = null) public static async Task<Wabbajack.Lib.Http.Client> GetAuthorizedClient(string? apiKey = null)
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
client.Headers.Add(("X-API-KEY", await GetAPIKey(apiKey))); client.Headers.Add(("X-API-KEY", await GetAPIKey(apiKey)));
return client; return client;
} }
public static string? ApiKeyOverride = null; public static string? ApiKeyOverride = null;
private Common.Http.Client _client; private Wabbajack.Lib.Http.Client _client;
public static async ValueTask<string> GetAPIKey(string? apiKey = null) public static async ValueTask<string> GetAPIKey(string? apiKey = null)
{ {

View File

@ -45,9 +45,9 @@ using Wabbajack.Lib.Downloaders;
public class ClientAPI public class ClientAPI
{ {
public static async Task<Common.Http.Client> GetClient() public static async Task<Wabbajack.Lib.Http.Client> GetClient()
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
client.Headers.Add((Consts.MetricsKeyHeader, await Metrics.GetMetricsKey())); client.Headers.Add((Consts.MetricsKeyHeader, await Metrics.GetMetricsKey()));
return client; return client;
} }

View File

@ -26,7 +26,7 @@ namespace Wabbajack.Lib.Downloaders
// ToDo // ToDo
// Remove null assignment. Either add nullability to type, or figure way to prepare it safely // Remove null assignment. Either add nullability to type, or figure way to prepare it safely
public Common.Http.Client AuthedClient { get; private set; } = null!; public Wabbajack.Lib.Http.Client AuthedClient { get; private set; } = null!;
public ReactiveCommand<Unit, Unit> TriggerLogin { get; } public ReactiveCommand<Unit, Unit> TriggerLogin { get; }
public ReactiveCommand<Unit, Unit> ClearLogin { get; } public ReactiveCommand<Unit, Unit> ClearLogin { get; }
@ -86,7 +86,7 @@ namespace Wabbajack.Lib.Downloaders
return cookies; return cookies;
} }
public async Task<Common.Http.Client> GetAuthedClient() public async Task<Wabbajack.Lib.Http.Client> GetAuthedClient()
{ {
Helpers.Cookie[] cookies; Helpers.Cookie[] cookies;
try try

View File

@ -216,13 +216,13 @@ namespace Wabbajack.Lib.Downloaders
return true; return true;
} }
private async Task<(Common.Http.Client, CDPTree, CollectedBNetInfo)> ResolveDownloadInfo() private async Task<(Wabbajack.Lib.Http.Client, CDPTree, CollectedBNetInfo)> ResolveDownloadInfo()
{ {
var info = new CollectedBNetInfo(); var info = new CollectedBNetInfo();
var login_info = await Utils.FromEncryptedJson<BethesdaNetData>(DataName); var login_info = await Utils.FromEncryptedJson<BethesdaNetData>(DataName);
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
client.Headers.Add(("User-Agent", "bnet")); client.Headers.Add(("User-Agent", "bnet"));
foreach (var header in login_info.headers.Where(h => h.Key.ToLower().StartsWith("x-"))) foreach (var header in login_info.headers.Where(h => h.Key.ToLower().StartsWith("x-")))

View File

@ -59,7 +59,7 @@ namespace Wabbajack.Lib.Downloaders
private async Task<HTTPDownloader.State> ToHttpState() private async Task<HTTPDownloader.State> ToHttpState()
{ {
var initialURL = $"https://drive.google.com/uc?id={Id}&export=download"; var initialURL = $"https://drive.google.com/uc?id={Id}&export=download";
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
using var response = await client.GetAsync(initialURL); using var response = await client.GetAsync(initialURL);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
throw new HttpException((int)response.StatusCode, response.ReasonPhrase); throw new HttpException((int)response.StatusCode, response.ReasonPhrase);

View File

@ -54,7 +54,7 @@ namespace Wabbajack.Lib.Downloaders
public List<string> Headers { get; } = new List<string>(); public List<string> Headers { get; } = new List<string>();
[JsonIgnore] [JsonIgnore]
public Common.Http.Client? Client { get; set; } public Wabbajack.Lib.Http.Client? Client { get; set; }
[JsonIgnore] [JsonIgnore]
public override object[] PrimaryKey => new object[] { Url }; public override object[] PrimaryKey => new object[] { Url };

View File

@ -115,7 +115,7 @@ namespace Wabbajack.Lib.Downloaders
TriggerLogin = ReactiveCommand.Create(() => { }, TriggerLogin = ReactiveCommand.Create(() => { },
IsLoggedIn.Select(b => !b).ObserveOnGuiThread()); IsLoggedIn.Select(b => !b).ObserveOnGuiThread());
ClearLogin = ReactiveCommand.Create(() => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson(DataName)), ClearLogin = ReactiveCommand.CreateFromTask(() => Utils.CatchAndLog(async () => await Utils.DeleteEncryptedJson(DataName)),
IsLoggedIn.ObserveOnGuiThread()); IsLoggedIn.ObserveOnGuiThread());
} }

View File

@ -50,7 +50,7 @@ namespace Wabbajack.Lib.Downloaders
private async Task<HTTPDownloader.State?> Resolve() private async Task<HTTPDownloader.State?> Resolve()
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
var body = await client.GetHtmlAsync(Url); var body = await client.GetHtmlAsync(Url);
var node = body.DocumentNode.DescendantsAndSelf().First(d => d.HasClass("input") && d.HasClass("popsok") && var node = body.DocumentNode.DescendantsAndSelf().First(d => d.HasClass("input") && d.HasClass("popsok") &&
d.GetAttributeValue("aria-label", "") == "Download file"); d.GetAttributeValue("aria-label", "") == "Download file");

View File

@ -46,8 +46,8 @@ namespace Wabbajack.Lib.Downloaders
TriggerLogin = ReactiveCommand.CreateFromTask( TriggerLogin = ReactiveCommand.CreateFromTask(
execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey), execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey),
canExecute: IsLoggedIn.Select(b => !b).ObserveOnGuiThread()); canExecute: IsLoggedIn.Select(b => !b).ObserveOnGuiThread());
ClearLogin = ReactiveCommand.Create( ClearLogin = ReactiveCommand.CreateFromTask(
execute: () => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson("nexusapikey")), execute: () => Utils.CatchAndLog(async () => await Utils.DeleteEncryptedJson("nexusapikey")),
canExecute: IsLoggedIn.ObserveOnGuiThread()); canExecute: IsLoggedIn.ObserveOnGuiThread());
} }

View File

@ -16,7 +16,7 @@ namespace Wabbajack.Lib.Downloaders.UrlDownloaders
var state = YouTubeDownloader.UriToState(uri) as YouTubeDownloader.State; var state = YouTubeDownloader.UriToState(uri) as YouTubeDownloader.State;
if (state == null) return null; if (state == null) return null;
var client = new YoutubeClient(Common.Http.ClientFactory.Client); var client = new YoutubeClient(Wabbajack.Lib.Http.ClientFactory.Client);
var video = await client.Videos.GetAsync(state.Key); var video = await client.Videos.GetAsync(state.Key);
var desc = video.Description; var desc = video.Description;

View File

@ -59,7 +59,7 @@ namespace Wabbajack.Lib.Downloaders
var definition = await GetDefinition(); var definition = await GetDefinition();
await using var fs = await destination.Create(); await using var fs = await destination.Create();
using var mmfile = MemoryMappedFile.CreateFromFile(fs, null, definition.Size, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); using var mmfile = MemoryMappedFile.CreateFromFile(fs, null, definition.Size, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false);
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
using var queue = new WorkQueue(); using var queue = new WorkQueue();
await definition.Parts.PMap(queue, async part => await definition.Parts.PMap(queue, async part =>
{ {
@ -81,7 +81,7 @@ namespace Wabbajack.Lib.Downloaders
private async Task<CDNFileDefinition> GetDefinition() private async Task<CDNFileDefinition> GetDefinition()
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
using var data = await client.GetAsync(Url + "/definition.json.gz"); using var data = await client.GetAsync(Url + "/definition.json.gz");
await using var gz = new GZipStream(await data.Content.ReadAsStreamAsync(), CompressionMode.Decompress); await using var gz = new GZipStream(await data.Content.ReadAsStreamAsync(), CompressionMode.Decompress);
return gz.FromJson<CDNFileDefinition>(); return gz.FromJson<CDNFileDefinition>();

View File

@ -61,7 +61,7 @@ namespace Wabbajack.Lib.Downloaders
public override async Task<bool> Verify(Archive archive) public override async Task<bool> Verify(Archive archive)
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
var result = await client.GetAsync(Url, errorsAsExceptions: false); var result = await client.GetAsync(Url, errorsAsExceptions: false);
return result.IsSuccessStatusCode; return result.IsSuccessStatusCode;
} }

View File

@ -104,7 +104,7 @@ namespace Wabbajack.Lib.Downloaders
using var queue = new WorkQueue(); using var queue = new WorkQueue();
await using var folder = await TempFolder.Create(); await using var folder = await TempFolder.Create();
folder.Dir.Combine("tracks").CreateDirectory(); folder.Dir.Combine("tracks").CreateDirectory();
var client = new YoutubeClient(Common.Http.ClientFactory.Client); var client = new YoutubeClient(Wabbajack.Lib.Http.ClientFactory.Client);
var meta = await client.Videos.GetAsync(Key); var meta = await client.Videos.GetAsync(Key);
var video = await client.Videos.Streams.GetManifestAsync(Key); var video = await client.Videos.Streams.GetManifestAsync(Key);
var stream = video.Streams.OfType<AudioOnlyStreamInfo>().Where(f => f.AudioCodec.StartsWith("mp4a")).OrderByDescending(a => a.Bitrate) var stream = video.Streams.OfType<AudioOnlyStreamInfo>().Where(f => f.AudioCodec.StartsWith("mp4a")).OrderByDescending(a => a.Bitrate)
@ -210,7 +210,7 @@ namespace Wabbajack.Lib.Downloaders
{ {
try try
{ {
var client = new YoutubeClient(Common.Http.ClientFactory.Client); var client = new YoutubeClient(Wabbajack.Lib.Http.ClientFactory.Client);
var video = await client.Videos.GetAsync(Key); var video = await client.Videos.GetAsync(Key);
return true; return true;
} }

View File

@ -29,9 +29,9 @@ namespace Wabbajack.Lib.FileUploader
return apiKey ?? (await Consts.LocalAppDataPath.Combine(Consts.AuthorAPIKeyFile).ReadAllTextAsync()).Trim(); return apiKey ?? (await Consts.LocalAppDataPath.Combine(Consts.AuthorAPIKeyFile).ReadAllTextAsync()).Trim();
} }
public static async Task<Common.Http.Client> GetAuthorizedClient(string? apiKey = null) public static async Task<Wabbajack.Lib.Http.Client> GetAuthorizedClient(string? apiKey = null)
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
client.Headers.Add(("X-API-KEY", await GetAPIKey(apiKey))); client.Headers.Add(("X-API-KEY", await GetAPIKey(apiKey)));
return client; return client;
} }

View File

@ -6,9 +6,10 @@ using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using HtmlAgilityPack; using HtmlAgilityPack;
using Wabbajack.Common;
using Wabbajack.Common.Exceptions; using Wabbajack.Common.Exceptions;
namespace Wabbajack.Common.Http namespace Wabbajack.Lib.Http
{ {
public class Client public class Client
{ {

View File

@ -1,7 +1,8 @@
using System; using System;
using System.Net; using System.Net;
using SysHttp = System.Net.Http; using SysHttp = System.Net.Http;
namespace Wabbajack.Common.Http
namespace Wabbajack.Lib.Http
{ {
public static class ClientFactory public static class ClientFactory
{ {

View File

@ -18,9 +18,9 @@ namespace Wabbajack.Lib.LibCefHelpers
{ {
public static class Helpers public static class Helpers
{ {
public static Common.Http.Client GetClient(IEnumerable<Cookie> cookies, string referer) public static Wabbajack.Lib.Http.Client GetClient(IEnumerable<Cookie> cookies, string referer)
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
client.Headers.Add(("Referrer", referer)); client.Headers.Add(("Referrer", referer));
client.Cookies.AddRange(cookies.Select(cookie => new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain))); client.Cookies.AddRange(cookies.Select(cookie => new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain)));
return client; return client;

View File

@ -7,9 +7,10 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Win32; using Microsoft.Win32;
using Wabbajack.Common;
using Wabbajack.Common.Exceptions; using Wabbajack.Common.Exceptions;
namespace Wabbajack.Common namespace Wabbajack.Lib
{ {
public class Metrics public class Metrics
{ {

View File

@ -62,7 +62,7 @@ namespace Wabbajack.Lib.ModListRegistry
public static async Task<List<ModlistMetadata>> LoadFromGithub() public static async Task<List<ModlistMetadata>> LoadFromGithub()
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
Utils.Log("Loading ModLists from GitHub"); Utils.Log("Loading ModLists from GitHub");
var metadataResult = client.GetStringAsync(Consts.ModlistMetadataURL); var metadataResult = client.GetStringAsync(Consts.ModlistMetadataURL);
var summaryResult = client.GetStringAsync(Consts.ModlistSummaryURL); var summaryResult = client.GetStringAsync(Consts.ModlistSummaryURL);

View File

@ -11,7 +11,6 @@ using Wabbajack.Common;
using Wabbajack.Lib.Downloaders; using Wabbajack.Lib.Downloaders;
using System.Threading; using System.Threading;
using Wabbajack.Common.Exceptions; using Wabbajack.Common.Exceptions;
using Wabbajack.Common.Http;
using Wabbajack.Lib.WebAutomation; using Wabbajack.Lib.WebAutomation;
namespace Wabbajack.Lib.NexusApi namespace Wabbajack.Lib.NexusApi
@ -20,7 +19,7 @@ namespace Wabbajack.Lib.NexusApi
{ {
private static readonly string API_KEY_CACHE_FILE = "nexus.key_cache"; private static readonly string API_KEY_CACHE_FILE = "nexus.key_cache";
public Common.Http.Client HttpClient { get; } = new Common.Http.Client(); public Wabbajack.Lib.Http.Client HttpClient { get; } = new Wabbajack.Lib.Http.Client();
#region Authentication #region Authentication
@ -255,7 +254,7 @@ namespace Wabbajack.Lib.NexusApi
return new NexusApiClient(apiKey); return new NexusApiClient(apiKey);
} }
public async Task<T> Get<T>(string url, Client? client = null) public async Task<T> Get<T>(string url, Wabbajack.Lib.Http.Client? client = null)
{ {
client ??= HttpClient; client ??= HttpClient;
int retries = 0; int retries = 0;

View File

@ -46,7 +46,7 @@ namespace Wabbajack.Lib.NexusApi
private static async Task<IEnumerable<UpdateRecord>> GetFeed(Uri uri) private static async Task<IEnumerable<UpdateRecord>> GetFeed(Uri uri)
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
var data = await client.GetStringAsync(uri); var data = await client.GetStringAsync(uri);
var reader = XmlReader.Create(new StringReader(data)); var reader = XmlReader.Create(new StringReader(data));
var results = SyndicationFeed.Load(reader); var results = SyndicationFeed.Load(reader);

View File

@ -12,8 +12,8 @@ namespace Wabbajack.Lib
public override string ShortDescription { get; } = string.Empty; public override string ShortDescription { get; } = string.Empty;
public override string ExtendedDescription { get; } = string.Empty; public override string ExtendedDescription { get; } = string.Empty;
private TaskCompletionSource<(Uri, Common.Http.Client)> _tcs = new TaskCompletionSource<(Uri, Common.Http.Client)>(); private TaskCompletionSource<(Uri, Wabbajack.Lib.Http.Client)> _tcs = new TaskCompletionSource<(Uri, Wabbajack.Lib.Http.Client)>();
public Task<(Uri, Common.Http.Client)> Task => _tcs.Task; public Task<(Uri, Wabbajack.Lib.Http.Client)> Task => _tcs.Task;
private ManuallyDownloadFile(ManualDownloader.State state) private ManuallyDownloadFile(ManualDownloader.State state)
{ {
@ -30,7 +30,7 @@ namespace Wabbajack.Lib
_tcs.SetCanceled(); _tcs.SetCanceled();
} }
public void Resume(Uri s, Common.Http.Client client) public void Resume(Uri s, Wabbajack.Lib.Http.Client client)
{ {
_tcs.SetResult((s, client)); _tcs.SetResult((s, client));
} }

View File

@ -24,7 +24,7 @@ namespace Wabbajack.Lib.Validation
public async Task LoadListsFromGithub() public async Task LoadListsFromGithub()
{ {
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
Utils.Log("Loading server whitelist"); Utils.Log("Loading server whitelist");
using (var response = await client.GetAsync(Consts.ServerWhitelistURL)) using (var response = await client.GetAsync(Consts.ServerWhitelistURL))

View File

@ -7,7 +7,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Wabbajack.Common; using Wabbajack.Common;
using Wabbajack.Common.Http;
using Wabbajack.Common.StatusFeed; using Wabbajack.Common.StatusFeed;
using Wabbajack.Lib; using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders; using Wabbajack.Lib.Downloaders;
@ -145,10 +144,10 @@ namespace Wabbajack.BuildServer.Test
[Collection("ServerTests")] [Collection("ServerTests")]
public class ABuildServerSystemTest : XunitContextBase, IClassFixture<SingletonAdaptor<BuildServerFixture>> public class ABuildServerSystemTest : XunitContextBase, IClassFixture<SingletonAdaptor<BuildServerFixture>>
{ {
protected readonly Client _client; protected readonly Wabbajack.Lib.Http.Client _client;
private readonly IDisposable _unsubMsgs; private readonly IDisposable _unsubMsgs;
private readonly IDisposable _unsubErr; private readonly IDisposable _unsubErr;
protected Client _authedClient; protected Wabbajack.Lib.Http.Client _authedClient;
protected WorkQueue _queue; protected WorkQueue _queue;
private Random _random; private Random _random;
@ -159,8 +158,8 @@ namespace Wabbajack.BuildServer.Test
_unsubMsgs = Utils.LogMessages.OfType<IInfo>().Subscribe(onNext: msg => XunitContext.WriteLine(msg.ShortDescription)); _unsubMsgs = Utils.LogMessages.OfType<IInfo>().Subscribe(onNext: msg => XunitContext.WriteLine(msg.ShortDescription));
_unsubErr = Utils.LogMessages.OfType<IUserIntervention>().Subscribe(msg => _unsubErr = Utils.LogMessages.OfType<IUserIntervention>().Subscribe(msg =>
XunitContext.WriteLine("ERROR: User intervention required: " + msg.ShortDescription)); XunitContext.WriteLine("ERROR: User intervention required: " + msg.ShortDescription));
_client = new Client(); _client = new Wabbajack.Lib.Http.Client();
_authedClient = new Client(); _authedClient = new Wabbajack.Lib.Http.Client();
Fixture = fixture.Deref(); Fixture = fixture.Deref();
_authedClient.Headers.Add(("x-api-key", Fixture.APIKey)); _authedClient.Headers.Add(("x-api-key", Fixture.APIKey));
AuthorAPI.ApiKeyOverride = Fixture.APIKey; AuthorAPI.ApiKeyOverride = Fixture.APIKey;

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper; using Dapper;
using Wabbajack.Common; using Wabbajack.Lib;
using Wabbajack.Server.DataLayer; using Wabbajack.Server.DataLayer;
using Wabbajack.Server.DTOs; using Wabbajack.Server.DTOs;
using Xunit; using Xunit;

View File

@ -49,7 +49,7 @@ namespace Wabbajack.Server.Services
}; };
if (url == null) return; if (url == null) return;
var client = new Common.Http.Client(); var client = new Wabbajack.Lib.Http.Client();
await client.PostAsync(url, new StringContent(message.ToJson(true), Encoding.UTF8, "application/json")); await client.PostAsync(url, new StringContent(message.ToJson(true), Encoding.UTF8, "application/json"));
} }
catch (Exception ex) catch (Exception ex)

View File

@ -84,7 +84,7 @@ namespace Wabbajack
{ {
if (Login.IconUri == null) return; if (Login.IconUri == null) return;
using var img = await new Common.Http.Client().GetAsync(Login.IconUri, errorsAsExceptions:false); using var img = await new Wabbajack.Lib.Http.Client().GetAsync(Login.IconUri, errorsAsExceptions:false);
if (!img.IsSuccessStatusCode) return; if (!img.IsSuccessStatusCode) return;
var icoData = new MemoryStream(await img.Content.ReadAsByteArrayAsync()); var icoData = new MemoryStream(await img.Content.ReadAsByteArrayAsync());