コード例 #1
0
        /// <summary>
        /// Writes a byte array value to a Consul key.
        /// </summary>
        /// <param name="kv">The key/value endpoint.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <returns><c>true</c> on success.</returns>
        public static async Task <bool> PutBytes(this IKVEndpoint kv, string key, byte[] value, CancellationToken cancellationToken = default)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(key));

            var p = new KVPair(key);

            p.Value = value ?? new byte[0];

            return((await kv.Put(p, cancellationToken)).Response);
        }
コード例 #2
0
        public async Task <bool> SetKeyValueAsync(string key, string value)
        {
            key = HttpUtils.StripFrontAndBackSlashes(key);
            var kv = new KVPair(key)
            {
                Value = Encoding.UTF8.GetBytes(value)
            };
            var response = await _kvEndpoint.Put(kv);

            return(response.StatusCode == System.Net.HttpStatusCode.OK);
        }
コード例 #3
0
        public static Task <WriteResult <bool> > PutAsync <T>(this IKVEndpoint endpoint, string key, T value,
                                                              JsonSerializerSettings settings = default, CancellationToken cancellation = default)
        {
            var json = settings == null
                ? JsonConvert.SerializeObject(value)
                : JsonConvert.SerializeObject(value, settings);

            var utf8Bytes = Encoding.UTF8.GetBytes(json);

            return(endpoint.Put(new KVPair(key)
            {
                Value = utf8Bytes
            }, cancellation));
        }
コード例 #4
0
        /// <summary>
        /// Writes a string value to a Consul key.
        /// </summary>
        /// <param name="kv">The key/value endpoint.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <returns><c>true</c> on success.</returns>
        /// <remarks>
        /// This method writes an empty string for <c>null</c> values and writes
        /// the <see cref="object.ToString()"/> results otherwise.
        /// </remarks>
        public static async Task <bool> PutString(this IKVEndpoint kv, string key, object value, CancellationToken cancellationToken = default)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(key));

            var p = new KVPair(key);

            if (value == null)
            {
                value = string.Empty;
            }

            p.Value = Encoding.UTF8.GetBytes(value.ToString());

            return((await kv.Put(p, cancellationToken)).Response);
        }