using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Flurl.Http; using Newtonsoft.Json.Linq; using Serilog; using TrashLib.Config.Services; using TrashLib.Extensions; using TrashLib.Sonarr.Api.Objects; namespace TrashLib.Sonarr.Api; public class SonarrApi : ISonarrApi { private readonly ILogger _log; private readonly ISonarrReleaseProfileCompatibilityHandler _profileHandler; private readonly IServerInfo _serverInfo; public SonarrApi( IServerInfo serverInfo, ISonarrReleaseProfileCompatibilityHandler profileHandler, ILogger log) { _serverInfo = serverInfo; _profileHandler = profileHandler; _log = log; } public async Task> GetTags() { return await BaseUrl() .AppendPathSegment("tag") .GetJsonAsync>(); } public async Task CreateTag(string tag) { return await BaseUrl() .AppendPathSegment("tag") .PostJsonAsync(new {label = tag}) .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> GetQualityDefinition() { return await BaseUrl() .AppendPathSegment("qualitydefinition") .GetJsonAsync>(); } public async Task> UpdateQualityDefinition( IReadOnlyCollection newQuality) { return await BaseUrl() .AppendPathSegment("qualityDefinition/update") .PutJsonAsync(newQuality) .ReceiveJson>(); } private IFlurlRequest BaseUrl() => _serverInfo.BuildRequest().SanitizedLogging(_log); }