public virtual bool CanHandle(string connectionString)
        {
            var tokens = new DbConnectionStringBuilder();

            try
            { tokens.ConnectionString = connectionString; }
            catch
            { return(false); }
            var api = tokens.Get(Api, string.Empty);

            return(StringComparer.InvariantCultureIgnoreCase.Compare(api, "elasticsearch") == 0);
        }
예제 #2
0
 public static bool TryGet(this DbConnectionStringBuilder builder, string[] candidateKeys, out string key, out string value)
 {
     foreach (var candidateKey in candidateKeys)
     {
         value = builder.Get(candidateKey);
         if (!string.IsNullOrEmpty(value))
         {
             key = candidateKey;
             return(true);
         }
     }
     key   = null;
     value = null;
     return(false);
 }
        public virtual ElasticsearchClientOption Execute(string connectionString)
        {
            var tokens = new DbConnectionStringBuilder()
            {
                ConnectionString = connectionString
            };
            var option = new ElasticsearchClientOption(connectionString)
            {
                Hostname = tokens.TryGet(Hostname, out string host) ? host : throw new ArgumentException("Hostname is mandatory for an Elasticsearch connection string"),
                                 Port = tokens.Get(Port, 9200)
            };

            if (tokens.TryGet(Username, out string username) ^ tokens.TryGet(Password, out string password))
            {
                throw new ArgumentException("username and password must be both filled or none of them must be filled");
            }

            option.Username = username;
            option.Password = password;

            return(option);
        }
    }