From 2262c2d4c04c30ddfed7f8c9e7b2db64cdad5e12 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 9 Nov 2024 22:28:32 -0600 Subject: add endpoint to raise event --- src/slskd/Events/API/EventsController.cs | 71 ++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 4 deletions(-) (limited to 'src/slskd/Events/API/EventsController.cs') diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index 46afa5c7..92748450 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -19,6 +19,7 @@ namespace slskd.Events.API; using System; using System.Collections.Generic; +using System.Linq; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; @@ -29,34 +30,42 @@ using Serilog; /// Events. /// [Route("api/v{version:apiVersion}/[controller]")] -[ApiVersion("0")] [ApiController] +[ApiVersion("0")] [Produces("application/json")] [Consumes("application/json")] public class EventsController : ControllerBase { public EventsController( - EventService eventService) + EventService eventService, + EventBus eventBus) { Events = eventService; + EventBus = eventBus; } private EventService Events { get; } + private EventBus EventBus { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); /// - /// Retrieves a paginated list of past records. + /// Retrieves a paginated list of past event records. /// /// The offset (number of records) at which to start the requested page. /// The page size. /// The list of records. + /// The offset is less than zero, or if the limit is less than or equal to zero. + /// Authentication credentials are omitted. + /// Authentication is valid but not sufficient to access this endpoint. + /// An unexpected error is encountered. + /// The request completed successfully. [HttpGet("", Name = nameof(GetEvents))] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(401)] [ProducesResponseType(403)] - [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(typeof(string), 500)] + [ProducesResponseType(typeof(IEnumerable), 200)] public IActionResult GetEvents([FromQuery] int offset = 0, [FromQuery] int limit = 100) { if (offset < 0) @@ -84,4 +93,58 @@ public class EventsController : ControllerBase throw; } } + + /// + /// Raises a sample event of the specified . + /// + /// The type of event to raise. + /// A string used to disambiguate generated events. + /// The randomly generated event that was raised. + /// If the offset is less than zero, or if the limit is less than or equal to zero. + /// Authentication credentials are omitted. + /// Authentication is valid but not sufficient to access this endpoint. + /// An unexpected error is encountered. + /// The request completed successfully. + [HttpPost("", Name = nameof(RaiseEvent))] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(401)] + [ProducesResponseType(403)] + [ProducesResponseType(typeof(string), 500)] + [ProducesResponseType(typeof(Event), 201)] + public IActionResult RaiseEvent([FromRoute] string type, [FromBody] string disambiguator) + { + if (!Enum.TryParse(type, out var eventType)) + { + var names = Enum.GetNames(typeof(EventType)) + .Where(n => n != EventType.Any.ToString() && n != EventType.None.ToString()); + + return BadRequest($"Unknown event type '{type}'; must be one of {string.Join(", ", names)}"); + } + + if (eventType is EventType.None || eventType is EventType.Any) + { + return BadRequest($"Event type '{type}' can not be raised"); + } + + var d = disambiguator; + + Event @event = eventType switch + { + EventType.DownloadFileComplete => new DownloadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = null }, + EventType.DownloadDirectoryComplete => new DownloadDirectoryCompleteEvent { LocalDirectoryName = $"{d}local.directory", RemoteDirectoryName = $"{d}remote.directory", Username = $"{d}username" }, + _ => throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."), + }; + + try + { + EventBus.Raise(@event); + return StatusCode(201, @event); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to raise event {Type}: {Message}", eventType, ex.Message); + throw; + } + } } \ No newline at end of file -- cgit v1.2.3