Reworked Models to use tmdb to enable integration with Radarr api. Also reworked test connection to use api to validate as I ran into CORS errors. I also set up an endpoint to call radarr to delete movies. Currently only got to retrieving movie info. Should be able to use the id retrieved to then delete the movie.
This commit is contained in:
60
Jellyfin.Plugin.MediaCleaner/Controllers/SonarrController.cs
Normal file
60
Jellyfin.Plugin.MediaCleaner/Controllers/SonarrController.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Jellyfin.Plugin.MediaCleaner.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System;
|
||||
|
||||
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
|
||||
|
||||
[Route("sonarr")]
|
||||
public class SonarrController : Controller
|
||||
{
|
||||
private static Configuration Configuration =>
|
||||
Plugin.Instance!.Configuration;
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public SonarrController(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
|
||||
// Set the default request headers
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", Configuration.SonarrAPIKey);
|
||||
}
|
||||
|
||||
[HttpPost("testConnection")]
|
||||
public async Task<IActionResult> TestConnection([FromBody] ConnectionTestRequest request)
|
||||
{
|
||||
if (request == null || string.IsNullOrWhiteSpace(request.Address) || string.IsNullOrWhiteSpace(request.ApiKey))
|
||||
{
|
||||
return BadRequest("Address and ApiKey are required.");
|
||||
}
|
||||
|
||||
var address = request.Address.Trim();
|
||||
if (!address.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
|
||||
!address.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
address = "http://" + address;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var httpRequest = new HttpRequestMessage(HttpMethod.Get, address);
|
||||
httpRequest.Headers.Add("X-Api-Key", request.ApiKey);
|
||||
|
||||
var response = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
|
||||
return Ok(new { success = response.IsSuccessStatusCode });
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status502BadGateway, e.Message);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user