using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Flurl; using Flurl.Http; using Newtonsoft.Json.Linq; using TrashLib.Config; using TrashLib.Sonarr.Api.Objects; namespace TrashLib.Sonarr.Api { public class SonarrApi : ISonarrApi { private readonly ISonarrReleaseProfileCompatibilityHandler _profileHandler; private readonly IServerInfo _serverInfo; public SonarrApi(IServerInfo serverInfo, ISonarrReleaseProfileCompatibilityHandler profileHandler) { _serverInfo = serverInfo; _profileHandler = profileHandler; } 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) { await BaseUrl() .AppendPathSegment($"releaseprofile/{profileToUpdate.Id}") .PutJsonAsync(_profileHandler.CompatibleReleaseProfileForSending(profileToUpdate)); } public async Task CreateReleaseProfile(SonarrReleaseProfile newProfile) { var response = await BaseUrl() .AppendPathSegment("releaseprofile") .PostJsonAsync(_profileHandler.CompatibleReleaseProfileForSending(newProfile)) .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 string BaseUrl() => _serverInfo.BuildUrl(); } }