Skip to content

Commit

Permalink
Reuse HttpClient for AniDB Http Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Mar 21, 2024
1 parent 56b0d3a commit 9aa76a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 234 deletions.
224 changes: 0 additions & 224 deletions Shoko.Server/ImageDownload/ImageDownloadRequest.cs

This file was deleted.

38 changes: 38 additions & 0 deletions Shoko.Server/ImageDownload/ImageDownloadResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
namespace Shoko.Server.ImageDownload;

/// <summary>
/// Represents the result of an image download operation.
/// </summary>
public enum ImageDownloadResult
{
/// <summary>
/// The image was successfully downloaded and saved.
/// </summary>
Success = 1,

/// <summary>
/// The image was not downloaded because it was already available in the cache.
/// </summary>
Cached = 2,

/// <summary>
/// The image could not be downloaded due to not being able to get the
/// source or destination.
/// </summary>
Failure = 3,

/// <summary>
/// The image was not downloaded because the resource has been removed or is
/// no longer available, but we could not remove the local entry because of
/// its type.
/// </summary>
InvalidResource = 4,

/// <summary>
/// The image was not downloaded because the resource has been removed or is
/// no longer available, and thus have also been removed from the local
/// database.
/// </summary>
RemovedResource = 5,
}
20 changes: 10 additions & 10 deletions Shoko.Server/Providers/AniDB/HTTP/AniDBHttpConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Shoko.Server.Providers.AniDB.HTTP;

public class AniDBHttpConnectionHandler : ConnectionHandler, IHttpConnectionHandler
{
private readonly HttpClient _httpClient;
public override double BanTimerResetLength => 12;

public override string Type => "HTTP";
Expand All @@ -18,6 +19,14 @@ public class AniDBHttpConnectionHandler : ConnectionHandler, IHttpConnectionHand

public AniDBHttpConnectionHandler(ILoggerFactory loggerFactory, HttpRateLimiter rateLimiter) : base(loggerFactory, rateLimiter)
{
_httpClient = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
});
_httpClient.Timeout = TimeSpan.FromSeconds(20);
_httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
_httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("deflate"));
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
}

public async Task<HttpResponse<string>> GetHttp(string url)
Expand All @@ -39,16 +48,7 @@ public async Task<HttpResponse<string>> GetHttpDirectly(string url)

RateLimiter.EnsureRate();

var client = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
});
client.Timeout = TimeSpan.FromSeconds(20);
client.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");

using var response = await client.GetAsync(url);
using var response = await _httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();

var responseStream = await response.Content.ReadAsStreamAsync();
Expand Down

0 comments on commit 9aa76a7

Please sign in to comment.