Skip to content

Commit

Permalink
10.9 support (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBR-0001 authored Apr 3, 2024
1 parent 57f2e23 commit 1ae4798
Show file tree
Hide file tree
Showing 28 changed files with 1,130 additions and 1,120 deletions.
85 changes: 42 additions & 43 deletions Jellyfin.Plugin.OpenSubtitles/API/OpenSubtitlesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,60 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jellyfin.Plugin.OpenSubtitles.API
namespace Jellyfin.Plugin.OpenSubtitles.API;

/// <summary>
/// The open subtitles plugin controller.
/// </summary>
[ApiController]
[Produces(MediaTypeNames.Application.Json)]
[Authorize]
public class OpenSubtitlesController : ControllerBase
{
/// <summary>
/// The open subtitles plugin controller.
/// Validates login info.
/// </summary>
[ApiController]
[Produces(MediaTypeNames.Application.Json)]
[Authorize]
public class OpenSubtitlesController : ControllerBase
/// <remarks>
/// Accepts plugin configuration as JSON body.
/// </remarks>
/// <response code="200">Login info valid.</response>
/// <response code="400">Login info is missing data.</response>
/// <response code="401">Login info not valid.</response>
/// <param name="body">The request body.</param>
/// <returns>
/// An <see cref="NoContentResult"/> if the login info is valid, a <see cref="BadRequestResult"/> if the request body missing is data
/// or <see cref="UnauthorizedResult"/> if the login info is not valid.
/// </returns>
[HttpPost("Jellyfin.Plugin.OpenSubtitles/ValidateLoginInfo")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> ValidateLoginInfo([FromBody] LoginInfoInput body)
{
/// <summary>
/// Validates login info.
/// </summary>
/// <remarks>
/// Accepts plugin configuration as JSON body.
/// </remarks>
/// <response code="200">Login info valid.</response>
/// <response code="400">Login info is missing data.</response>
/// <response code="401">Login info not valid.</response>
/// <param name="body">The request body.</param>
/// <returns>
/// An <see cref="NoContentResult"/> if the login info is valid, a <see cref="BadRequestResult"/> if the request body missing is data
/// or <see cref="UnauthorizedResult"/> if the login info is not valid.
/// </returns>
[HttpPost("Jellyfin.Plugin.OpenSubtitles/ValidateLoginInfo")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> ValidateLoginInfo([FromBody] LoginInfoInput body)
var key = !string.IsNullOrWhiteSpace(body.CustomApiKey) ? body.CustomApiKey : OpenSubtitlesPlugin.ApiKey;
var response = await OpenSubtitlesHandler.OpenSubtitles.LogInAsync(body.Username, body.Password, key, CancellationToken.None).ConfigureAwait(false);

if (!response.Ok)
{
var key = !string.IsNullOrWhiteSpace(body.CustomApiKey) ? body.CustomApiKey : OpenSubtitlesPlugin.ApiKey;
var response = await OpenSubtitlesHandler.OpenSubtitles.LogInAsync(body.Username, body.Password, key, CancellationToken.None).ConfigureAwait(false);
var msg = $"{response.Code}{(response.Body.Length < 150 ? $" - {response.Body}" : string.Empty)}";

if (!response.Ok)
if (response.Body.Contains("message\":", StringComparison.Ordinal))
{
var msg = $"{response.Code}{(response.Body.Length < 150 ? $" - {response.Body}" : string.Empty)}";

if (response.Body.Contains("message\":", StringComparison.Ordinal))
var err = JsonSerializer.Deserialize<ErrorResponse>(response.Body);
if (err is not null)
{
var err = JsonSerializer.Deserialize<ErrorResponse>(response.Body);
if (err != null)
{
msg = string.Equals(err.Message, "You cannot consume this service", StringComparison.Ordinal) ? "Invalid API key provided" : err.Message;
}
msg = string.Equals(err.Message, "You cannot consume this service", StringComparison.Ordinal) ? "Invalid API key provided" : err.Message;
}

return Unauthorized(new { Message = msg });
}

if (response.Data != null)
{
await OpenSubtitlesHandler.OpenSubtitles.LogOutAsync(response.Data, key, CancellationToken.None).ConfigureAwait(false);
}
return Unauthorized(new { Message = msg });
}

return Ok(new { Downloads = response.Data?.User?.AllowedDownloads ?? 0 });
if (response.Data is not null)
{
await OpenSubtitlesHandler.OpenSubtitles.LogOutAsync(response.Data, key, CancellationToken.None).ConfigureAwait(false);
}

return Ok(new { Downloads = response.Data?.User?.AllowedDownloads ?? 0 });
}
}
41 changes: 20 additions & 21 deletions Jellyfin.Plugin.OpenSubtitles/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
using MediaBrowser.Model.Plugins;

namespace Jellyfin.Plugin.OpenSubtitles.Configuration
namespace Jellyfin.Plugin.OpenSubtitles.Configuration;

/// <summary>
/// The plugin configuration.
/// </summary>
public class PluginConfiguration : BasePluginConfiguration
{
/// <summary>
/// The plugin configuration.
/// Gets or sets the username.
/// </summary>
public class PluginConfiguration : BasePluginConfiguration
{
/// <summary>
/// Gets or sets the username.
/// </summary>
public string Username { get; set; } = string.Empty;
public string Username { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the password.
/// </summary>
public string Password { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the password.
/// </summary>
public string Password { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the custom API Key.
/// </summary>
public string CustomApiKey { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the custom API Key.
/// </summary>
public string CustomApiKey { get; set; } = string.Empty;

/// <summary>
/// Gets or sets a value indicating whether the credentials are invalid.
/// </summary>
public bool CredentialsInvalid { get; set; } = false;
}
/// <summary>
/// Gets or sets a value indicating whether the credentials are invalid.
/// </summary>
public bool CredentialsInvalid { get; set; } = false;
}
Loading

0 comments on commit 1ae4798

Please sign in to comment.