using HermesSocketLibrary.Socket.Data; using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { public class CreateConnection : IRequest { public string Name => "create_connection"; public string[] RequiredKeys => ["name", "type", "clientId", "accessToken", "grantType", "scope", "expiration"]; private ILogger _logger; public CreateConnection(ILogger logger) { _logger = logger; } public Task Grant(Channel channel, IDictionary data) { string name = data["name"].ToString()!; string type = data["type"].ToString()!; string clientId = data["clientId"].ToString()!; string accessToken = data["accessToken"].ToString()!; string grantType = data["grantType"].ToString()!; string scope = data["scope"].ToString()!; if (!DateTime.TryParse(data["expiration"].ToString()!, out var expiresAt)) return Task.FromResult(RequestResult.Failed("Expiration needs to be a date time string.")); var connection = new Connection() { UserId = channel.Id, Name = name, Type = type, ClientId = clientId, AccessToken = accessToken, GrantType = grantType, Scope = scope, ExpiresAt = expiresAt, }; bool result = channel.Connections.Set(name, connection); if (result) { _logger.Information($"Added connection to channel [name: {name}][type: {type}][scope: {scope}][expiration: {expiresAt}][channel: {channel.Id}]"); return Task.FromResult(RequestResult.Successful(connection)); } return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache.")); } } }