From f87971fec11f14796c09dfec59b2497cb58c49de Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Wed, 31 Aug 2022 08:35:53 -0500 Subject: [PATCH] refactor: Decouple release profiles from SonarrApi class --- .../Sonarr/Api/IReleaseProfileApiService.cs | 11 ++++ .../Services/Sonarr/Api/ISonarrApi.cs | 4 -- .../Sonarr/Api/ReleaseProfileApiService.cs | 58 +++++++++++++++++++ src/TrashLib/Services/Sonarr/Api/SonarrApi.cs | 44 +------------- .../ReleaseProfile/ReleaseProfileUpdater.cs | 13 +++-- .../Services/Sonarr/SonarrAutofacModule.cs | 1 + 6 files changed, 79 insertions(+), 52 deletions(-) create mode 100644 src/TrashLib/Services/Sonarr/Api/IReleaseProfileApiService.cs create mode 100644 src/TrashLib/Services/Sonarr/Api/ReleaseProfileApiService.cs diff --git a/src/TrashLib/Services/Sonarr/Api/IReleaseProfileApiService.cs b/src/TrashLib/Services/Sonarr/Api/IReleaseProfileApiService.cs new file mode 100644 index 00000000..1704c5fc --- /dev/null +++ b/src/TrashLib/Services/Sonarr/Api/IReleaseProfileApiService.cs @@ -0,0 +1,11 @@ +using TrashLib.Services.Sonarr.Api.Objects; + +namespace TrashLib.Services.Sonarr.Api; + +public interface IReleaseProfileApiService +{ + Task UpdateReleaseProfile(SonarrReleaseProfile profile); + Task CreateReleaseProfile(SonarrReleaseProfile profile); + Task> GetReleaseProfiles(); + Task DeleteReleaseProfile(int releaseProfileId); +} diff --git a/src/TrashLib/Services/Sonarr/Api/ISonarrApi.cs b/src/TrashLib/Services/Sonarr/Api/ISonarrApi.cs index 6612ca6d..02708039 100644 --- a/src/TrashLib/Services/Sonarr/Api/ISonarrApi.cs +++ b/src/TrashLib/Services/Sonarr/Api/ISonarrApi.cs @@ -6,10 +6,6 @@ public interface ISonarrApi { Task> GetTags(); Task CreateTag(string tag); - Task> GetReleaseProfiles(); - Task UpdateReleaseProfile(SonarrReleaseProfile profileToUpdate); - Task CreateReleaseProfile(SonarrReleaseProfile newProfile); - Task DeleteReleaseProfile(int releaseProfileId); Task> GetQualityDefinition(); Task> UpdateQualityDefinition( diff --git a/src/TrashLib/Services/Sonarr/Api/ReleaseProfileApiService.cs b/src/TrashLib/Services/Sonarr/Api/ReleaseProfileApiService.cs new file mode 100644 index 00000000..d402cbf6 --- /dev/null +++ b/src/TrashLib/Services/Sonarr/Api/ReleaseProfileApiService.cs @@ -0,0 +1,58 @@ +using Flurl.Http; +using Newtonsoft.Json.Linq; +using TrashLib.Config.Services; +using TrashLib.Services.Sonarr.Api.Objects; + +namespace TrashLib.Services.Sonarr.Api; + +public class ReleaseProfileApiService : IReleaseProfileApiService +{ + private readonly ISonarrReleaseProfileCompatibilityHandler _profileHandler; + private readonly IServerInfo _serverInfo; + + public ReleaseProfileApiService( + ISonarrReleaseProfileCompatibilityHandler profileHandler, + IServerInfo serverInfo) + { + _profileHandler = profileHandler; + _serverInfo = serverInfo; + } + + public async Task UpdateReleaseProfile(SonarrReleaseProfile profile) + { + var profileToSend = await _profileHandler.CompatibleReleaseProfileForSendingAsync(profile); + await _serverInfo.BuildRequest() + .AppendPathSegment($"releaseprofile/{profile.Id}") + .PutJsonAsync(profileToSend); + } + + public async Task CreateReleaseProfile(SonarrReleaseProfile profile) + { + var profileToSend = await _profileHandler.CompatibleReleaseProfileForSendingAsync(profile); + + var response = await _serverInfo.BuildRequest() + .AppendPathSegment("releaseprofile") + .PostJsonAsync(profileToSend) + .ReceiveJson(); + + return _profileHandler.CompatibleReleaseProfileForReceiving(response); + } + + public async Task> GetReleaseProfiles() + { + var response = await _serverInfo.BuildRequest() + .AppendPathSegment("releaseprofile") + .GetJsonAsync>(); + + return response + .Select(_profileHandler.CompatibleReleaseProfileForReceiving) + .ToList(); + } + + public async Task DeleteReleaseProfile(int releaseProfileId) + { + await _serverInfo.BuildRequest() + .AppendPathSegment($"releaseprofile/{releaseProfileId}") + .DeleteAsync(); + } +} diff --git a/src/TrashLib/Services/Sonarr/Api/SonarrApi.cs b/src/TrashLib/Services/Sonarr/Api/SonarrApi.cs index 60282263..df1e270a 100644 --- a/src/TrashLib/Services/Sonarr/Api/SonarrApi.cs +++ b/src/TrashLib/Services/Sonarr/Api/SonarrApi.cs @@ -1,6 +1,5 @@ using Flurl; using Flurl.Http; -using Newtonsoft.Json.Linq; using TrashLib.Config.Services; using TrashLib.Services.Sonarr.Api.Objects; @@ -8,15 +7,11 @@ namespace TrashLib.Services.Sonarr.Api; public class SonarrApi : ISonarrApi { - private readonly ISonarrReleaseProfileCompatibilityHandler _profileHandler; private readonly IServerInfo _serverInfo; - public SonarrApi( - IServerInfo serverInfo, - ISonarrReleaseProfileCompatibilityHandler profileHandler) + public SonarrApi(IServerInfo serverInfo) { _serverInfo = serverInfo; - _profileHandler = profileHandler; } public async Task> GetTags() @@ -34,43 +29,6 @@ public class SonarrApi : ISonarrApi .ReceiveJson(); } - public async Task> GetReleaseProfiles() - { - var response = await BaseUrl() - .AppendPathSegment("releaseprofile") - .GetJsonAsync>(); - - return response - .Select(_profileHandler.CompatibleReleaseProfileForReceiving) - .ToList(); - } - - public async Task UpdateReleaseProfile(SonarrReleaseProfile profileToUpdate) - { - var profileToSend = await _profileHandler.CompatibleReleaseProfileForSendingAsync(profileToUpdate); - await BaseUrl() - .AppendPathSegment($"releaseprofile/{profileToUpdate.Id}") - .PutJsonAsync(profileToSend); - } - - public async Task CreateReleaseProfile(SonarrReleaseProfile newProfile) - { - var profileToSend = await _profileHandler.CompatibleReleaseProfileForSendingAsync(newProfile); - var response = await BaseUrl() - .AppendPathSegment("releaseprofile") - .PostJsonAsync(profileToSend) - .ReceiveJson(); - - return _profileHandler.CompatibleReleaseProfileForReceiving(response); - } - - public async Task DeleteReleaseProfile(int releaseProfileId) - { - await BaseUrl() - .AppendPathSegment($"releaseprofile/{releaseProfileId}") - .DeleteAsync(); - } - public async Task> GetQualityDefinition() { return await BaseUrl() diff --git a/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs b/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs index dbd3039b..0811ef82 100644 --- a/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs +++ b/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs @@ -13,17 +13,19 @@ namespace TrashLib.Services.Sonarr.ReleaseProfile; public class ReleaseProfileUpdater : IReleaseProfileUpdater { - private readonly ISonarrApi _api; + private readonly IReleaseProfileApiService _releaseProfileApi; private readonly ISonarrCompatibility _compatibility; private readonly IReleaseProfileFilterPipeline _pipeline; private readonly IConsole _console; private readonly ISonarrGuideService _guide; + private readonly ISonarrApi _api; private readonly ILogger _log; public ReleaseProfileUpdater( ILogger logger, ISonarrGuideService guide, ISonarrApi api, + IReleaseProfileApiService releaseProfileApi, ISonarrCompatibility compatibility, IReleaseProfileFilterPipeline pipeline, IConsole console) @@ -31,6 +33,7 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater _log = logger; _guide = guide; _api = api; + _releaseProfileApi = releaseProfileApi; _compatibility = compatibility; _pipeline = pipeline; _console = console; @@ -131,7 +134,7 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater // Obtain all of the existing release profiles first. If any were previously created by our program // here, we favor replacing those instead of creating new ones, which would just be mostly duplicates // (but with some differences, since there have likely been updates since the last run). - var existingProfiles = await _api.GetReleaseProfiles(); + var existingProfiles = await _releaseProfileApi.GetReleaseProfiles(); foreach (var (profile, tags) in profilesAndTags) { @@ -174,7 +177,7 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater foreach (var profile in sonarrProfilesToDelete) { _log.Information("Deleting old Trash release profile: {ProfileName}", profile.Name); - await _api.DeleteReleaseProfile(profile.Id); + await _releaseProfileApi.DeleteReleaseProfile(profile.Id); } } @@ -246,13 +249,13 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater { _log.Debug("Update existing profile with id {ProfileId}", profileToUpdate.Id); SetupProfileRequestObject(profileToUpdate, profile, tagIds); - await _api.UpdateReleaseProfile(profileToUpdate); + await _releaseProfileApi.UpdateReleaseProfile(profileToUpdate); } private async Task CreateNewProfile(string title, ReleaseProfileData profile, IReadOnlyCollection tagIds) { var newProfile = new SonarrReleaseProfile {Name = title, Enabled = true}; SetupProfileRequestObject(newProfile, profile, tagIds); - await _api.CreateReleaseProfile(newProfile); + await _releaseProfileApi.CreateReleaseProfile(newProfile); } } diff --git a/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs b/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs index 9fc18aab..5a0ddd64 100644 --- a/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs +++ b/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs @@ -14,6 +14,7 @@ public class SonarrAutofacModule : Module protected override void Load(ContainerBuilder builder) { builder.RegisterType().As(); + builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As();