More HttpClientFactory rework.

This commit is contained in:
Steven Hildreth 2022-01-17 14:13:15 -06:00
parent 0c3d978dfb
commit 4940112b9b
3 changed files with 10 additions and 6 deletions

View file

@ -705,7 +705,7 @@ namespace Roadie.Library.Engines
{
var sw2 = Stopwatch.StartNew();
var imageBag = new ConcurrentBag<IImage>();
var i = artistImageUrls.Select(async url => imageBag.Add(await WebHelper.GetImageFromUrlAsync(url).ConfigureAwait(false)));
var i = artistImageUrls.Select(async url => imageBag.Add(await WebHelper.GetImageFromUrlAsync(HttpClientFactory, url).ConfigureAwait(false)));
await Task.WhenAll(i).ConfigureAwait(false);
result.Images = imageBag.Where(x => x?.Bytes != null)
.GroupBy(x => x.Signature)

View file

@ -915,7 +915,7 @@ namespace Roadie.Library.Engines
{
var sw2 = Stopwatch.StartNew();
var imageBag = new ConcurrentBag<IImage>();
var i = releaseImageUrls.Select(async url => imageBag.Add(await WebHelper.GetImageFromUrlAsync(url).ConfigureAwait(false)));
var i = releaseImageUrls.Select(async url => imageBag.Add(await WebHelper.GetImageFromUrlAsync(HttpClientFactory, url).ConfigureAwait(false)));
await Task.WhenAll(i).ConfigureAwait(false);
releaseImages = imageBag.Where(x => x?.Bytes != null)
.GroupBy(x => x.Signature)

View file

@ -24,6 +24,7 @@ namespace Roadie.Library.Utility
{
var client = httpclientFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("User-Agent", UserAgent);
var response = await client.SendAsync(request).ConfigureAwait(false);
if(response.IsSuccessStatusCode)
{
@ -58,15 +59,18 @@ namespace Roadie.Library.Utility
return null;
}
public static async Task<IImage> GetImageFromUrlAsync(string url)
public static async Task<IImage> GetImageFromUrlAsync(IHttpClientFactory httpclientFactory, string url)
{
byte[] imageBytes = null;
try
{
using (var webClient = new WebClient())
var client = httpclientFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("User-Agent", UserAgent);
var response = await client.SendAsync(request).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
webClient.Headers.Add("user-agent", UserAgent);
imageBytes = await webClient.DownloadDataTaskAsync(new Uri(url)).ConfigureAwait(false);
imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
}
catch