fixed uri building

This commit is contained in:
Flaminel
2024-11-09 22:02:11 +02:00
parent 4cae378e69
commit e95ff3914a
2 changed files with 28 additions and 13 deletions
+3 -2
View File
@@ -1,4 +1,4 @@
using Common.Configuration; using Common.Configuration;
using Executable.Jobs; using Executable.Jobs;
using Infrastructure.Verticals.FrozenTorrent; using Infrastructure.Verticals.FrozenTorrent;
@@ -9,7 +9,7 @@ public static class DependencyInjection
{ {
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) => public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) =>
services services
.AddLogging(builder => builder.AddConsole()) .AddLogging(builder => builder.ClearProviders().AddConsole())
.AddHttpClient() .AddHttpClient()
.AddConfiguration(configuration) .AddConfiguration(configuration)
.AddServices() .AddServices()
@@ -23,6 +23,7 @@ public static class DependencyInjection
private static IServiceCollection AddServices(this IServiceCollection services) => private static IServiceCollection AddServices(this IServiceCollection services) =>
services services
.AddTransient<FrozenTorrentJob>()
.AddTransient<FrozenTorrentHandler>(); .AddTransient<FrozenTorrentHandler>();
private static IServiceCollection AddQuartzServices(this IServiceCollection services, IConfiguration configuration) => private static IServiceCollection AddQuartzServices(this IServiceCollection services, IConfiguration configuration) =>
@@ -1,5 +1,6 @@
using Common.Configuration; using Common.Configuration;
using Domain.Sonarr.Queue; using Domain.Sonarr.Queue;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -43,14 +44,22 @@ public sealed class FrozenTorrentHandler
do do
{ {
UriBuilder uriBuilder = new UriBuilder(sonarrInstance.Url); Uri sonarrUri = new(sonarrInstance.Url, string.Format(SonarListUriTemplate, page));
uriBuilder.Path = 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); sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey);
HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest); HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
}
catch
{
_logger.LogWarning("queue list failed | {uri}", sonarrUri);
throw;
}
string responseBody = await response.Content.ReadAsStringAsync(); string responseBody = await response.Content.ReadAsStringAsync();
QueueListResponse? queueResponse = JsonConvert.DeserializeObject<QueueListResponse>(responseBody); QueueListResponse? queueResponse = JsonConvert.DeserializeObject<QueueListResponse>(responseBody);
@@ -67,20 +76,25 @@ public sealed class FrozenTorrentHandler
if (torrent is not { CompletionOn: not null, Downloaded: null or 0 }) if (torrent is not { CompletionOn: not null, Downloaded: null or 0 })
{ {
// TODO log skip _logger.LogInformation("skip | {torrent}", record.Title);
continue; continue;
} }
// delete and block from sonarr sonarrUri = new(sonarrInstance.Url, string.Format(SonarDeleteUriTemplate, record.Id));
uriBuilder = new(sonarrInstance.Url); sonarrRequest = new(HttpMethod.Delete, sonarrUri);
uriBuilder.Path = string.Format(SonarDeleteUriTemplate, record.Id);
sonarrRequest = new(HttpMethod.Delete, uriBuilder.Uri);
sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey); sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey);
response = await _httpClient.SendAsync(sonarrRequest); response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
} }
catch
{
_logger
}
}
if (queueResponse.Records.Count is 0) if (queueResponse.Records.Count is 0)
{ {