From e31e4a9906b36ab337fa73ea5a5acdf2d4b582be Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 9 Nov 2024 21:57:23 -0600 Subject: version events --- src/slskd/Events/Types/Events.cs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs index 678a5294..1f4c6060 100644 --- a/src/slskd/Events/Types/Events.cs +++ b/src/slskd/Events/Types/Events.cs @@ -22,10 +22,10 @@ using slskd.Transfers; public enum EventType { - None = 0, - Any = 1, - DownloadFileComplete = 2, - DownloadDirectoryComplete = 3, + None, + DownloadFileComplete, + DownloadDirectoryComplete, + Any, } public abstract record Event @@ -33,20 +33,23 @@ public abstract record Event public Guid Id { get; } = Guid.NewGuid(); public DateTime Timestamp { get; init; } = DateTime.UtcNow; public abstract EventType Type { get; } + public abstract int Version { get; } } public sealed record DownloadFileCompleteEvent : Event { - public override EventType Type => EventType.DownloadFileComplete; - public string LocalFilename { get; init; } - public string RemoteFilename { get; init; } - public Transfer Transfer { get; init; } + public override EventType Type { get; } = EventType.DownloadFileComplete; + public override int Version { get; } = 0; + public required string LocalFilename { get; init; } + public required string RemoteFilename { get; init; } + public required Transfer Transfer { get; init; } } public sealed record DownloadDirectoryCompleteEvent : Event { - public override EventType Type => EventType.DownloadDirectoryComplete; - public string LocalDirectoryName { get; init; } - public string RemoteDirectoryName { get; init; } - public string Username { get; init; } + public override EventType Type { get; } = EventType.DownloadDirectoryComplete; + public override int Version { get; } = 0; + public required string LocalDirectoryName { get; init; } + public required string RemoteDirectoryName { get; init; } + public required string Username { get; init; } } \ No newline at end of file -- cgit v1.2.3 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(-) 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 From ac1dae488ebc9474461064b979d8c2a03da17a82 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Nov 2024 18:18:46 -0600 Subject: add missing versions --- src/slskd/Events/Types/Events.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs index f6cb8bd2..fc9397ea 100644 --- a/src/slskd/Events/Types/Events.cs +++ b/src/slskd/Events/Types/Events.cs @@ -49,6 +49,7 @@ public sealed record DownloadFileCompleteEvent : Event public sealed record DownloadDirectoryCompleteEvent : Event { public override EventType Type => EventType.DownloadDirectoryComplete; + public override int Version { get; } = 0; public string LocalDirectoryName { get; init; } public string RemoteDirectoryName { get; init; } public string Username { get; init; } @@ -57,4 +58,5 @@ public sealed record DownloadDirectoryCompleteEvent : Event public sealed record NoopEvent : Event { public override EventType Type => EventType.Noop; + public override int Version { get; } = 0; } \ No newline at end of file -- cgit v1.2.3 From 32ce0640657dfe2f54ca81f47258d6301e006546 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 24 Nov 2024 18:19:06 -0600 Subject: include type in route template --- src/slskd/Events/API/EventsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index 92748450..ad7cd93e 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -105,7 +105,7 @@ public class EventsController : ControllerBase /// Authentication is valid but not sufficient to access this endpoint. /// An unexpected error is encountered. /// The request completed successfully. - [HttpPost("", Name = nameof(RaiseEvent))] + [HttpPost("{type}", Name = nameof(RaiseEvent))] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(401)] -- cgit v1.2.3 From 2aeb5be605f6e4af541ebcc325181d11487e90e0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 24 Nov 2024 18:19:53 -0600 Subject: ignore case when parsing type --- src/slskd/Events/API/EventsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index ad7cd93e..29845e8c 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -114,7 +114,7 @@ public class EventsController : ControllerBase [ProducesResponseType(typeof(Event), 201)] public IActionResult RaiseEvent([FromRoute] string type, [FromBody] string disambiguator) { - if (!Enum.TryParse(type, out var eventType)) + if (!Enum.TryParse(type, ignoreCase: true, out var eventType)) { var names = Enum.GetNames(typeof(EventType)) .Where(n => n != EventType.Any.ToString() && n != EventType.None.ToString()); -- cgit v1.2.3 From 12cebb629e7bad910e6e9ed718c7aaeb1d86c807 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 24 Nov 2024 18:21:20 -0600 Subject: handle Noop --- src/slskd/Events/API/EventsController.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index 29845e8c..60b554b0 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -98,7 +98,7 @@ public class EventsController : ControllerBase /// Raises a sample event of the specified . /// /// The type of event to raise. - /// A string used to disambiguate generated events. + /// An optional 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. @@ -133,6 +133,7 @@ public class EventsController : ControllerBase { 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" }, + EventType.Noop => new NoopEvent(), _ => throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."), }; -- cgit v1.2.3 From 23175133bb4c46e9d2c6b922822f7af9d9de59af Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 24 Nov 2024 18:38:27 -0600 Subject: tweak endpoint docs --- src/slskd/Events/API/EventsController.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index 60b554b0..a0b23777 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -95,16 +95,16 @@ public class EventsController : ControllerBase } /// - /// Raises a sample event of the specified . + /// Raises a sample event of the specified type. /// /// The type of event to raise. /// An optional 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. + /// The specified type is not a valid event type. /// Authentication credentials are omitted. /// Authentication is valid but not sufficient to access this endpoint. /// An unexpected error is encountered. - /// The request completed successfully. + /// The request completed successfully. [HttpPost("{type}", Name = nameof(RaiseEvent))] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(string), 400)] @@ -127,18 +127,18 @@ public class EventsController : ControllerBase 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" }, - EventType.Noop => new NoopEvent(), - _ => throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."), - }; - try { + 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" }, + EventType.Noop => new NoopEvent(), + _ => throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."), + }; + EventBus.Raise(@event); return StatusCode(201, @event); } -- cgit v1.2.3 From c346e5db9922187fbf55e202207e68789056dbfc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 27 Nov 2024 19:00:00 -0600 Subject: include default Transfer --- src/slskd/Events/API/EventsController.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index a0b23777..c0aa92f0 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -25,6 +25,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Serilog; +using slskd.Transfers; /// /// Events. @@ -133,7 +134,7 @@ public class EventsController : ControllerBase Event @event = eventType switch { - EventType.DownloadFileComplete => new DownloadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = null }, + EventType.DownloadFileComplete => new DownloadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = new Transfer() }, EventType.DownloadDirectoryComplete => new DownloadDirectoryCompleteEvent { LocalDirectoryName = $"{d}local.directory", RemoteDirectoryName = $"{d}remote.directory", Username = $"{d}username" }, EventType.Noop => new NoopEvent(), _ => throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."), -- cgit v1.2.3 From 6650447a76a042ce1710dc3f352856cd4ee5520c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 27 Nov 2024 19:00:16 -0600 Subject: docs for events --- docs/config.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/docs/config.md b/docs/config.md index 8ac29622..8dc43000 100644 --- a/docs/config.md +++ b/docs/config.md @@ -923,7 +923,58 @@ retention: # Integrations -## Webhooks +## User-Defined + +User-defined integrations allow users to configure external applications to receive data from slskd as things happen internally. slskd uses an internal [event bus](https://www.akamai.com/glossary/what-is-an-event-bus) over which event data is sent from application logic to whatever destination(s) users choose. + +The number and type of events will change as the application matures, but each event should have some minimal properties: + +|Name|Description| +|-|-| +|Id|The unique ID for the event. Events may be delivered to external consumers more than once; use this property to de-duplicate them.| +|Timestamp|The time (UTC) at which the event was raised. Events can arrive out of chronological order; use this property to determine if that has taken place.| +|Version|The event version, used to disambiguate event shapes if a breaking change is made to the data in the future.| +|Type|The event type. Use this to determine what other data will be included. See: [Events.cs](https://github.com/slskd/slskd/blob/master/src/slskd/Events/Types/Events.cs).| + +Here's an example of what a `DownloadFileComplete` event (at the time of this writing; review Events.cs!) looks like: + +```json +{ + "type": "DownloadFileComplete", + "version": 0, + "localFilename": "asdfasfdasfdlocal.file", + "remoteFilename": "asdfasfdasfdremote.file", + "transfer": { + "id": "00000000-0000-0000-0000-000000000000", + "direction": "Download", + "size": 0, + "startOffset": 0, + "state": "None", + "requestedAt": "0001-01-01T00:00:00", + "bytesTransferred": 0, + "averageSpeed": 0, + "bytesRemaining": 0, + "percentComplete": 0 + }, + "id": "973bed18-8516-4b7d-ab24-e13888385237", + "timestamp": "2024-11-28T00:54:13.8939864Z" +} +``` + +Users developing integrations can use the internal `RaiseEvent` HTTP endpoint to create sample events and put them on the bus. This will help test integrations end to end without having to take action within the application. + +Here's an example cURL command for the endpoint: + +```bash +curl --location 'localhost:5030/api/v0/events/downloadfilecomplete' \ +--header 'Content-Type: application/json' \ +--header 'X-API-Key: ' \ +--data '"asdfasfdasfd"' +``` + +The data passed is a 'disambiguator' that's prepended to sample data. This will help users differentiate between different event instances. + +### Webhooks Webhooks (outbound HTTP requests) can be configured to be called when application events are raised. Webhooks are given a name (useful for troubleshooting!), a list of triggering events, a `call` configuration that defines the HTTP request to be made, and timeout and retry configuration. @@ -958,7 +1009,7 @@ integration: attempts: 3 ``` -## User-Defined Scripts +### Scripts User-defined scripts can be configured to run when application events are raised. Scripts are given a name (useful for troubleshooting!), a list of triggering events, and a `run` command that's used to execute the script. @@ -981,7 +1032,11 @@ integration: run: cmd.exe echo $DATA >> event_log.txt # Windows ``` -## FTP +## System-Defined + +System-defined integrations are part of the application logic. Users can configure settings, but don't have any control over behavior. + +### FTP Files can be uploaded to a remote FTP server upon completion. Files are uploaded to the server and remote path specified using the directory and filename with which they were downloaded; the FTP will match the layout of the local disk. @@ -1018,7 +1073,7 @@ integration: retry_attempts: 3 ``` -## Pushbullet +### Pushbullet Pushbullet notifications can be sent when a private message is sent, or the current user's username is mentioned in a chat room. Notifications are prefixed with a user-definable string to differentiate these notifications from others. -- cgit v1.2.3 From 6ade4aedfce517a394b9e1275860db7308fa0428 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 30 Nov 2024 14:10:07 -0600 Subject: tweak event docs --- docs/config.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/config.md b/docs/config.md index 8dc43000..9d17974f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -961,18 +961,18 @@ Here's an example of what a `DownloadFileComplete` event (at the time of this wr } ``` -Users developing integrations can use the internal `RaiseEvent` HTTP endpoint to create sample events and put them on the bus. This will help test integrations end to end without having to take action within the application. +Users developing integrations can use the internal `RaiseEvent` HTTP endpoint to create sample events and put them on the bus. This will help test integrations end to end without having to take action within the application, but be aware; using this endpoint after testing is complete is not a good idea! Here's an example cURL command for the endpoint: ```bash curl --location 'localhost:5030/api/v0/events/downloadfilecomplete' \ ---header 'Content-Type: application/json' \ ---header 'X-API-Key: ' \ ---data '"asdfasfdasfd"' + --header 'Content-Type: application/json' \ + --header 'X-API-Key: ' \ + --data '"asdfasfdasfd"' ``` -The data passed is a 'disambiguator' that's prepended to sample data. This will help users differentiate between different event instances. +The data passed is a 'disambiguator' that's prepended to sample data. This helps differentiate between different event instances. ### Webhooks -- cgit v1.2.3