/// <summary>
        ///
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="kvEndpoint"></param>
        /// <param name="key"></param>
        /// <param name="city"></param>
        /// <param name="company"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public static async Task <TResult> GetOrDefaultAsync <TResult>(this IKVEndpoint kvEndpoint, string key, string city = null, string company = null, CancellationToken ct = default(CancellationToken))
        {
            QueryResult <KVPair> queryResult = await kvEndpoint.Get(RequestKey(key, city, company), ct);

            if (queryResult.StatusCode != HttpStatusCode.OK)
            {
                queryResult = await kvEndpoint.Get(RequestKey(key, !string.IsNullOrWhiteSpace(city) ? "default" : null, !string.IsNullOrWhiteSpace(company) ? "default" : null), ct);
            }
            return(queryResult.StatusCode == HttpStatusCode.OK ? JsonConvert.DeserializeObject <TResult>(Encoding.UTF8.GetString(queryResult.Response.Value)) : default(TResult));
        }
示例#2
0
        /// <summary>
        /// Reads a key as a string, returning <c>null</c> if the key doesn't exist.
        /// </summary>
        /// <param name="kv">The key/value endpoint.</param>
        /// <param name="key">The key.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <returns>The string value or <c>null</c>.</returns>
        /// <remarks>
        /// <note>
        /// Any exceptions thrown will be wrapped within an <see cref="AggregateException"/>.
        /// </note>
        /// </remarks>
        public static async Task <string> GetStringOrDefault(this IKVEndpoint kv, string key, CancellationToken cancellationToken = default)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(key));

            var response = (await kv.Get(key, cancellationToken)).Response;

            if (response == null)
            {
                return(null);
            }

            return(Encoding.UTF8.GetString(response.Value));
        }
示例#3
0
        /// <summary>
        /// Reads a key as a byte array, throwing an exception if the key doesn't exist.
        /// </summary>
        /// <param name="kv">The key/value endpoint.</param>
        /// <param name="key">The key.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <returns>The byte array value.</returns>
        /// <exception cref="KeyNotFoundException">Thrown if <paramref name="key"/> could not be found.</exception>
        /// <remarks>
        /// <note>
        /// Any exceptions thrown will be wrapped within an <see cref="AggregateException"/>.
        /// </note>
        /// </remarks>
        public static async Task <byte[]> GetBytes(this IKVEndpoint kv, string key, CancellationToken cancellationToken = default)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(key));

            var response = (await kv.Get(key, cancellationToken)).Response;

            if (response == null)
            {
                throw new KeyNotFoundException(key);
            }

            return(response.Value);
        }
示例#4
0
        //---------------------------------------------------------------------
        // IKVEndpoint extensions

        /// <summary>
        /// Determines whether a Consul key exists.
        /// </summary>
        /// <param name="kv">The key/value endpoint.</param>
        /// <param name="key">The key.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <returns><c>true</c> if the key exists.</returns>
        public static async Task <bool> Exists(this IKVEndpoint kv, string key, CancellationToken cancellationToken = default)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(key));

            try
            {
                return((await kv.Get(key, cancellationToken)).Response != null);
            }
            catch (Exception e)
            {
                if (e.Contains <KeyNotFoundException>())
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }
        //try to get value from consul configuration source
        public string GetValue(string key)
        {
            IKVEndpoint kv = this.client?.KV;

            try
            {
                key = key.Replace('.', '/');

                var value = kv.Get(nametag + key)?.Result?.Response?.Value;
                if (value == null)
                {
                    return(null);
                }

                return(Encoding.UTF8.GetString(value, 0, value.Length));
            }
            catch
            {
                _logger.LogInformation($"Unable to get value of key '{nametag + key}'");
                return(null);
            }
        }