From e95ff3914a40991f0efb2575dd2240bce10e5438 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Sat, 9 Nov 2024 22:02:11 +0200 Subject: [PATCH] fixed uri building --- code/Executable/DependencyInjection.cs | 5 +-- .../FrozenTorrent/FrozenTorrentHandler.cs | 36 +++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/code/Executable/DependencyInjection.cs b/code/Executable/DependencyInjection.cs index e61b84ca..5bbd388b 100644 --- a/code/Executable/DependencyInjection.cs +++ b/code/Executable/DependencyInjection.cs @@ -1,4 +1,4 @@ -using Common.Configuration; +using Common.Configuration; using Executable.Jobs; using Infrastructure.Verticals.FrozenTorrent; @@ -9,7 +9,7 @@ public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) => services - .AddLogging(builder => builder.AddConsole()) + .AddLogging(builder => builder.ClearProviders().AddConsole()) .AddHttpClient() .AddConfiguration(configuration) .AddServices() @@ -23,6 +23,7 @@ public static class DependencyInjection private static IServiceCollection AddServices(this IServiceCollection services) => services + .AddTransient() .AddTransient(); private static IServiceCollection AddQuartzServices(this IServiceCollection services, IConfiguration configuration) => diff --git a/code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs b/code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs index 51450e79..5c2df104 100644 --- a/code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs +++ b/code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs @@ -1,5 +1,6 @@ using Common.Configuration; using Domain.Sonarr.Queue; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; @@ -43,14 +44,22 @@ public sealed class FrozenTorrentHandler do { - UriBuilder uriBuilder = new UriBuilder(sonarrInstance.Url); - uriBuilder.Path = string.Format(SonarListUriTemplate, page); + Uri sonarrUri = new(sonarrInstance.Url, string.Format(SonarListUriTemplate, page)); - HttpRequestMessage sonarrRequest = new(HttpMethod.Get, uriBuilder.Uri); + HttpRequestMessage sonarrRequest = new(HttpMethod.Get, sonarrUri); sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey); HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest); - response.EnsureSuccessStatusCode(); + + try + { + response.EnsureSuccessStatusCode(); + } + catch + { + _logger.LogWarning("queue list failed | {uri}", sonarrUri); + throw; + } string responseBody = await response.Content.ReadAsStringAsync(); QueueListResponse? queueResponse = JsonConvert.DeserializeObject(responseBody); @@ -67,19 +76,24 @@ public sealed class FrozenTorrentHandler if (torrent is not { CompletionOn: not null, Downloaded: null or 0 }) { - // TODO log skip + _logger.LogInformation("skip | {torrent}", record.Title); continue; } - - // delete and block from sonarr - uriBuilder = new(sonarrInstance.Url); - uriBuilder.Path = string.Format(SonarDeleteUriTemplate, record.Id); - sonarrRequest = new(HttpMethod.Delete, uriBuilder.Uri); + sonarrUri = new(sonarrInstance.Url, string.Format(SonarDeleteUriTemplate, record.Id)); + sonarrRequest = new(HttpMethod.Delete, sonarrUri); sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey); response = await _httpClient.SendAsync(sonarrRequest); - response.EnsureSuccessStatusCode(); + + try + { + response.EnsureSuccessStatusCode(); + } + catch + { + _logger + } } if (queueResponse.Records.Count is 0)