refactor: Decouple release profiles from SonarrApi class

pull/124/head
Robert Dailey 2 years ago
parent 28aae58298
commit f87971fec1

@ -0,0 +1,11 @@
using TrashLib.Services.Sonarr.Api.Objects;
namespace TrashLib.Services.Sonarr.Api;
public interface IReleaseProfileApiService
{
Task UpdateReleaseProfile(SonarrReleaseProfile profile);
Task<SonarrReleaseProfile> CreateReleaseProfile(SonarrReleaseProfile profile);
Task<IList<SonarrReleaseProfile>> GetReleaseProfiles();
Task DeleteReleaseProfile(int releaseProfileId);
}

@ -6,10 +6,6 @@ public interface ISonarrApi
{
Task<IList<SonarrTag>> GetTags();
Task<SonarrTag> CreateTag(string tag);
Task<IList<SonarrReleaseProfile>> GetReleaseProfiles();
Task UpdateReleaseProfile(SonarrReleaseProfile profileToUpdate);
Task<SonarrReleaseProfile> CreateReleaseProfile(SonarrReleaseProfile newProfile);
Task DeleteReleaseProfile(int releaseProfileId);
Task<IReadOnlyCollection<SonarrQualityDefinitionItem>> GetQualityDefinition();
Task<IList<SonarrQualityDefinitionItem>> UpdateQualityDefinition(

@ -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<SonarrReleaseProfile> CreateReleaseProfile(SonarrReleaseProfile profile)
{
var profileToSend = await _profileHandler.CompatibleReleaseProfileForSendingAsync(profile);
var response = await _serverInfo.BuildRequest()
.AppendPathSegment("releaseprofile")
.PostJsonAsync(profileToSend)
.ReceiveJson<JObject>();
return _profileHandler.CompatibleReleaseProfileForReceiving(response);
}
public async Task<IList<SonarrReleaseProfile>> GetReleaseProfiles()
{
var response = await _serverInfo.BuildRequest()
.AppendPathSegment("releaseprofile")
.GetJsonAsync<List<JObject>>();
return response
.Select(_profileHandler.CompatibleReleaseProfileForReceiving)
.ToList();
}
public async Task DeleteReleaseProfile(int releaseProfileId)
{
await _serverInfo.BuildRequest()
.AppendPathSegment($"releaseprofile/{releaseProfileId}")
.DeleteAsync();
}
}

@ -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<IList<SonarrTag>> GetTags()
@ -34,43 +29,6 @@ public class SonarrApi : ISonarrApi
.ReceiveJson<SonarrTag>();
}
public async Task<IList<SonarrReleaseProfile>> GetReleaseProfiles()
{
var response = await BaseUrl()
.AppendPathSegment("releaseprofile")
.GetJsonAsync<List<JObject>>();
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<SonarrReleaseProfile> CreateReleaseProfile(SonarrReleaseProfile newProfile)
{
var profileToSend = await _profileHandler.CompatibleReleaseProfileForSendingAsync(newProfile);
var response = await BaseUrl()
.AppendPathSegment("releaseprofile")
.PostJsonAsync(profileToSend)
.ReceiveJson<JObject>();
return _profileHandler.CompatibleReleaseProfileForReceiving(response);
}
public async Task DeleteReleaseProfile(int releaseProfileId)
{
await BaseUrl()
.AppendPathSegment($"releaseprofile/{releaseProfileId}")
.DeleteAsync();
}
public async Task<IReadOnlyCollection<SonarrQualityDefinitionItem>> GetQualityDefinition()
{
return await BaseUrl()

@ -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<int> tagIds)
{
var newProfile = new SonarrReleaseProfile {Name = title, Enabled = true};
SetupProfileRequestObject(newProfile, profile, tagIds);
await _api.CreateReleaseProfile(newProfile);
await _releaseProfileApi.CreateReleaseProfile(newProfile);
}
}

@ -14,6 +14,7 @@ public class SonarrAutofacModule : Module
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<SonarrApi>().As<ISonarrApi>();
builder.RegisterType<ReleaseProfileApiService>().As<IReleaseProfileApiService>();
builder.RegisterType<SonarrValidationMessages>().As<ISonarrValidationMessages>();
builder.RegisterType<SonarrCompatibility>().As<ISonarrCompatibility>().SingleInstance();
builder.RegisterType<SonarrGuideDataLister>().As<ISonarrGuideDataLister>();

Loading…
Cancel
Save