Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class AudnexusService : IAudnexusService
private readonly HttpClient _httpClient;
private readonly ILogger<AudnexusService> _logger;
private const string BASE_URL = "https://api.audnex.us";
private const int RequestTimeoutSeconds = 10;

public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
{
Expand Down Expand Up @@ -60,7 +61,8 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)

_logger.LogInformation("Fetching audiobook metadata from Audnexus: {Url}", url);

var response = await _httpClient.GetAsync(url);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(RequestTimeoutSeconds));
var response = await _httpClient.GetAsync(url, cts.Token);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -69,7 +71,7 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
return null;
}

var json = await response.Content.ReadAsStringAsync();
var json = await response.Content.ReadAsStringAsync(cts.Token);

_logger.LogDebug("Audnexus raw JSON response for ASIN {Asin}: {Json}", LogRedaction.SanitizeText(asin), json.Length > 500 ? json.Substring(0, 500) + "..." : json);

Expand Down Expand Up @@ -103,6 +105,12 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)

return result;
}
catch (TaskCanceledException ex)
{
_logger.LogWarning(ex, "Audnexus request timed out after {TimeoutSeconds}s fetching metadata for ASIN {Asin}",
RequestTimeoutSeconds, LogRedaction.SanitizeText(asin));
return null;
}
catch (Exception ex) when (ex is not OperationCanceledException && ex is not OutOfMemoryException && ex is not StackOverflowException)
{
_logger.LogError(ex, "Error fetching metadata from Audnexus for ASIN {Asin}", LogRedaction.SanitizeText(asin));
Expand All @@ -123,7 +131,8 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
var url = $"{BASE_URL}/authors?name={Uri.EscapeDataString(name)}&region={region}";
_logger.LogInformation("Searching Audnexus authors: {Url}", LogRedaction.SanitizeUrl(url));

var response = await _httpClient.GetAsync(url);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(RequestTimeoutSeconds));
var response = await _httpClient.GetAsync(url, cts.Token);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -132,7 +141,7 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
return null;
}

var json = await response.Content.ReadAsStringAsync();
var json = await response.Content.ReadAsStringAsync(cts.Token);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand All @@ -145,6 +154,12 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
LogRedaction.SanitizeText(name), result?.Count ?? 0);
return result;
}
catch (TaskCanceledException ex)
{
_logger.LogWarning(ex, "Audnexus request timed out after {TimeoutSeconds}s searching for author {Name}",
RequestTimeoutSeconds, LogRedaction.SanitizeText(name));
return null;
}
catch (Exception ex) when (ex is not OperationCanceledException && ex is not OutOfMemoryException && ex is not StackOverflowException)
{
_logger.LogError(ex, "Error searching Audnexus for author {Name}", LogRedaction.SanitizeText(name));
Expand All @@ -168,7 +183,8 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)

_logger.LogInformation("Fetching author from Audnexus: {Url}", LogRedaction.SanitizeUrl(url));

var response = await _httpClient.GetAsync(url);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(RequestTimeoutSeconds));
var response = await _httpClient.GetAsync(url, cts.Token);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -177,7 +193,7 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
return null;
}

var json = await response.Content.ReadAsStringAsync();
var json = await response.Content.ReadAsStringAsync(cts.Token);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand Down Expand Up @@ -222,7 +238,8 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)

_logger.LogInformation("Fetching chapters from Audnexus: {Url}", LogRedaction.SanitizeUrl(url));

var response = await _httpClient.GetAsync(url);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(RequestTimeoutSeconds));
var response = await _httpClient.GetAsync(url, cts.Token);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -231,7 +248,7 @@ public AudnexusService(HttpClient httpClient, ILogger<AudnexusService> logger)
return null;
}

var json = await response.Content.ReadAsStringAsync();
var json = await response.Content.ReadAsStringAsync(cts.Token);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand Down