using System.Linq; using xCommons.Extensions; using xEventService.Models; using xExceptions.Constants; using xEventService.Constants; using xEventService.Configuration; namespace xEventService.Extensions { /// /// Extensions for Event Service ... /// public static class XEventServiceExtensions { /// /// Validate Event Broker ... /// /// /// public static bool IsValid(this XEventBroker source) { // var result = !source.IsNull() && source != XEventBroker.None; // return result; } /// /// Validate Event Service Configuration ... /// /// /// public static bool IsValid(this XEventServiceConfiguration source) { // var result = !source.IsNullOrDefault() && source.Broker.IsValid() && !source.Url.IsNullOrEmpty() && source.Url.IsValidUrl(); // return result; } /// /// Validate RabbitMQ Exchange Type ... /// /// /// public static bool IsValid(this XRabbitMQExchangeType source) { // var result = source != XRabbitMQExchangeType.None; // return result; } /// /// Validate RabbitMQ Configuration ... /// /// /// public static bool IsValid(this XRabbitMQOptions source) { // var result = !source.IsNullOrDefault() && !source.Exchange.IsNullOrEmpty() && source.ExchangeType.IsValid(); // return result; } /// /// Validate an Event Message ... /// /// /// public static bool IsValid(this XEventMessage source) { // var result = !source.IsNullOrDefault() && !source.Action.IsNullOrEmpty() && !source.Sender.IsNullOrEmpty(); // return result; } /// /// Validate an Event Request ... /// /// /// public static bool IsValid(this XEventRequest source) { // var result = !source.IsNullOrDefault() && !source.Topic.IsNullOrEmpty() && source.Message.IsValid(); // return result; } /// /// Validate ZeroMQ Pattern ... /// /// /// public static bool IsValid(this XZeroMQPattern source) { // var result = source != XZeroMQPattern.None; // return result; } /// /// Validate ZeroMQ Options ... /// /// /// public static bool IsValid(this XZeroMQOptions source) { // var result = !source.IsNullOrDefault() && source.Pattern.IsValid(); // return result; } /// /// Validate an Array is Wildcard passed or not ... /// /// /// public static bool IsAllowedWildcard(this string[] source) { // var result = source is not null && source.Length == 1 && source.Any(s => s == XEventServiceConstants.XWildcard); // return result; } /// /// Validate a Collection is Valid for Kafka Consumer ... /// /// /// public static bool IsAllowedCollection( this string[] source ) { // var result = source.HasChild(); // return result; } /// /// Validate Sender ... /// /// /// /// public static bool IsValidSender( this XEventMessage source, string[] allowedSenders ) { // // Validate Source and Collection ... var result = source.IsValid() && allowedSenders .IsAllowedCollection(); if (!result) { return result; } // // Validate Sender ... result = allowedSenders.IsAllowedWildcard() || allowedSenders.Any(x => x.Equals( source.Sender, System.StringComparison.InvariantCultureIgnoreCase)); // return result; } /// /// Validate Action ... /// /// /// /// public static bool IsValidAction( this XEventMessage source, string[] allowedActions ) { // // Validate Source and Collection ... var result = source.IsValid() && allowedActions .IsAllowedCollection(); if (!result) { return result; } // // Validate Action ... result = allowedActions.IsAllowedWildcard() || allowedActions.Any(x => x.Equals( source.Action, System.StringComparison.InvariantCultureIgnoreCase)); // return result; } /// /// Completely Validate a Message, based on providing: /// - Allowed Actions /// - Allowed Senders /// /// /// /// /// public static bool IsValid( this XEventMessage source, string[] allowedSenders, string[] allowedActions ) { // var result = source.IsValid() && source.IsValidAction(allowedActions) && source.IsValidSender(allowedSenders); // return result; } /// /// Parse Url and Extract Host address ... /// /// /// public static string GetRabbitMQHost(this XEventServiceConfiguration source) { // var result = string.Empty; // if (!source.IsValid()) { XException.InvalidConfiguration.Throw(); } // var parts = source.Url.Split(':'); if (parts.Length != 2) { XException.InvalidConfiguration.Throw(); } // result = parts[0]; if (result.IsNullOrEmpty() || !result.IsValidUrl() ) { XException.InvalidConfiguration.Throw(); } // return result; } /// /// Parse Url and Extract Port Number ... /// /// /// public static int GetRabbitMQPort(this XEventServiceConfiguration source) { // int result = -1; // if (!source.IsValid()) { XException.InvalidConfiguration.Throw(); } // var parts = source.Url.Split(':'); if (parts.Length != 2) { XException.InvalidConfiguration.Throw(); } // // Try Parse Port as Integer ... int.TryParse(parts[1], out result); if (result <= 0) { XException.InvalidConfiguration.Throw(); } // return result; } } }