Refactor of http client and addition of sonarr anime table
This commit is contained in:
@@ -4,11 +4,10 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Jellyfin.Plugin.MediaCleaner.Enums;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Linq;
|
||||
|
||||
@@ -24,25 +23,10 @@ public record RadarrMovie(
|
||||
[Route("radarr")]
|
||||
public class RadarrController : Controller
|
||||
{
|
||||
private static Configuration Configuration =>
|
||||
Plugin.Instance!.Configuration;
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public RadarrController(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
|
||||
// Set the default request headers
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", Configuration.RadarrAPIKey);
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> GetRadarrMovieInfo(MovieInfo movieInfo)
|
||||
{
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.RadarrAddress,
|
||||
HttpHelper httpHelper = new(ServerType.Radarr);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/movie?tmdbId={Uri.EscapeDataString(movieInfo.TmdbId ?? string.Empty)}&excludeLocalCovers=false"
|
||||
).ConfigureAwait(false);
|
||||
@@ -76,9 +60,8 @@ public class RadarrController : Controller
|
||||
|
||||
RadarrMovie movie = (RadarrMovie)radarrMovieInfoResult.Value;
|
||||
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.RadarrAddress,
|
||||
HttpHelper httpHelper = new(ServerType.Radarr);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Delete,
|
||||
$"/api/v3/movie/{movie.Id}?deleteFiles=true&addImportExclusion=true"
|
||||
).ConfigureAwait(false);
|
||||
@@ -109,10 +92,11 @@ public class RadarrController : Controller
|
||||
|
||||
try
|
||||
{
|
||||
using var testHttpClient = new HttpClient();
|
||||
using var httpRequest = new HttpRequestMessage(HttpMethod.Get, address);
|
||||
httpRequest.Headers.Add("X-Api-Key", request.ApiKey);
|
||||
|
||||
var response = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
|
||||
var response = await testHttpClient.SendAsync(httpRequest).ConfigureAwait(false);
|
||||
return Ok(new { success = response.IsSuccessStatusCode });
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
|
||||
@@ -11,7 +11,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Jellyfin.Plugin.MediaCleaner.Enums;
|
||||
using Jellyfin.Plugin.MediaCleaner.Helpers;
|
||||
|
||||
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
|
||||
@@ -37,9 +37,6 @@ public record Season(
|
||||
[Route("sonarr")]
|
||||
public class SonarrController : Controller
|
||||
{
|
||||
private static Configuration Configuration =>
|
||||
Plugin.Instance!.Configuration;
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public SonarrController(HttpClient httpClient)
|
||||
@@ -48,13 +45,11 @@ public class SonarrController : Controller
|
||||
|
||||
// Set the default request headers
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", Configuration.SonarrAPIKey);
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> GetSonarrSeriesInfo(SeriesInfo seriesInfo){
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.SonarrAddress,
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/series?tvdbId={Uri.EscapeDataString(seriesInfo.TvdbId ?? string.Empty)}"
|
||||
).ConfigureAwait(false);
|
||||
@@ -71,9 +66,8 @@ public class SonarrController : Controller
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> GetSonarrEpisodeInfo(SonarrSeries sonarrSeries){
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.SonarrAddress,
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/episode?seriesId={sonarrSeries.Id?.ToString(CultureInfo.InvariantCulture)}"
|
||||
).ConfigureAwait(false);
|
||||
@@ -109,7 +103,7 @@ public class SonarrController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("deleteSeriesFromSonarr")]
|
||||
public async Task<IActionResult> DeleteSeriesFromRadarr([FromBody] SeriesInfo seriesInfo){
|
||||
public async Task<IActionResult> DeleteSeriesFromSonarr([FromBody] SeriesInfo seriesInfo){
|
||||
|
||||
if (seriesInfo == null || string.IsNullOrEmpty(seriesInfo.TvdbId))
|
||||
{
|
||||
@@ -157,9 +151,8 @@ public class SonarrController : Controller
|
||||
return BadRequest("No stale series provided.");
|
||||
}
|
||||
|
||||
var series = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.SonarrAddress,
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
var series = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/series/{staleSeries.Id}"
|
||||
).ConfigureAwait(false);
|
||||
@@ -195,9 +188,7 @@ public class SonarrController : Controller
|
||||
|
||||
seriesDict["seasons"] = updatedSeasons;
|
||||
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.SonarrAddress,
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Put,
|
||||
$"/api/v3/series/{staleSeries.Id}",
|
||||
seriesDict
|
||||
@@ -213,9 +204,8 @@ public class SonarrController : Controller
|
||||
return BadRequest("No episode file IDs provided.");
|
||||
}
|
||||
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.SonarrAddress,
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Delete,
|
||||
"/api/v3/episodefile/bulk",
|
||||
new { episodeFileIds }
|
||||
@@ -231,9 +221,9 @@ public class SonarrController : Controller
|
||||
return BadRequest("No episode IDs provided.");
|
||||
}
|
||||
|
||||
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
||||
_httpClient,
|
||||
Configuration.SonarrAddress,
|
||||
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Put,
|
||||
"/api/v3/episode/monitor",
|
||||
new { episodeIds, monitored = false }
|
||||
|
||||
@@ -25,7 +25,11 @@ public class StateController(MediaCleanerState state) : Controller
|
||||
public IActionResult GetMoviesTitle() =>
|
||||
Ok($"Stale Movies (Unwatched for and created over {Configuration.StaleMediaCutoff} Days ago.)");
|
||||
|
||||
[HttpGet("getSeriesTitle")]
|
||||
[HttpGet("getSeriesTitle")]
|
||||
public IActionResult GetSeriesTitle() =>
|
||||
Ok($"Stale Series (Unwatched for and created over {Configuration.StaleMediaCutoff} Days ago.)");
|
||||
Ok($"Stale TV Series (Unwatched for and created over {Configuration.StaleMediaCutoff} Days ago.)");
|
||||
|
||||
[HttpGet("getAnimeSeriesTitle")]
|
||||
public IActionResult GetAnimeSeriesTitle() =>
|
||||
Ok($"Stale Anime Series (Unwatched for and created over {Configuration.StaleMediaCutoff} Days ago.)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user