/// <summary>
        /// Formats the field value in the appropriate line protocol format based on the type of the value object.
        /// The value type must be a string, boolean, or integral or floating-point type.
        /// </summary>
        /// <param name="value">The field value to format.</param>
        /// <returns>The field value formatted as a string used in the line protocol format.</returns>
        public static String FormatValue(Object value)
        {
            Type type = value?.GetType();

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (!InfluxUtils.IsValidValueType(type))
            {
                throw new ArgumentException(nameof(value), $"Value is not one of the supported types: {type} - Valid types: {String.Join(", ", InfluxUtils.ValidValueTypes.Select(t => t.Name))}");
            }

            if (InfluxUtils.IsIntegralType(type))
            {
                return(FormatValue(Convert.ToInt64(value)));
            }
            if (InfluxUtils.IsFloatingPointType(type))
            {
                return(FormatValue(Convert.ToDouble(value)));
            }
            if (value is Boolean)
            {
                return(FormatValue((Boolean)value));
            }
            if (value is String)
            {
                return(FormatValue((String)value));
            }
            if (value is Char)
            {
                return(FormatValue(value.ToString()));
            }
            return(FormatValue(value.ToString()));
        }
Example #2
0
        /// <summary>
        /// Creates a new InfluxDB configuration object with values initialized from the URI.
        /// </summary>
        /// <param name="influxDbUri">The URI of the InfluxDB server, including any query string parameters.</param>
        public InfluxConfig(Uri influxDbUri)
        {
            if (influxDbUri == null)
            {
                throw new ArgumentNullException(nameof(influxDbUri));
            }
            Hostname = influxDbUri.Host;
            Port     = (UInt16)influxDbUri.Port;
            var queryKvps = influxDbUri.ParseQueryString();

            foreach (var kvp in queryKvps.Where(k => !String.IsNullOrEmpty(k.Value)))
            {
                if (kvp.Key.ToLower() == "db")
                {
                    Database = kvp.Value;
                }
                if (kvp.Key.ToLower() == "rp")
                {
                    RetentionPolicy = kvp.Value;
                }
                if (kvp.Key.ToLower() == "u")
                {
                    Username = kvp.Value;
                }
                if (kvp.Key.ToLower() == "p")
                {
                    Password = kvp.Value;
                }
                if (kvp.Key.ToLower() == "precision")
                {
                    Precision = InfluxUtils.FromShortName(kvp.Value);
                }
            }
        }