<commit_before>﻿// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Configuration;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host.Config;
using WebJobs.Extensions.Splunk;
using WebJobs.Extensions.Splunk.Services;
using System.IO;
using System.Threading.Tasks;

namespace WebJobs.Extensions.Splunk
{
    /// <summary>
    /// Defines the configuration options for the Splunk binding.
    /// </summary>
    public class SplunkConfiguration : IExtensionConfigProvider
    {
        internal const string SplunkHecHostSettingName = "AzureWebJobsSplunkHttpEventCollectorHost";
        internal const string SplunkTokenSettingName = "AzureWebJobsSplunkToken";
        internal const string SplunkHostSettingName = "AzureWebJobsSplunkHost";
        internal const string SplunkSourceSettingName = "AzureWebJobsSplunkSource";
        internal const string SplunkSourceTypeSettingName = "AzureWebJobsSplunkSourceType";
        internal const string SplunkIndexSettingName = "AzureWebJobsSplunkIndex";

        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public SplunkConfiguration()
        {
            var host = GetSettingFromConfigOrEnvironment(SplunkHecHostSettingName);
            HecHost = host != null ? new Uri(host) : null;
            var token = GetSettingFromConfigOrEnvironment(SplunkTokenSettingName);
            Token = token != null ? new Guid(token) : Token; 
            Host = GetSettingFromConfigOrEnvironment(SplunkHostSettingName);
            Source = GetSettingFromConfigOrEnvironment(SplunkSourceSettingName);
            SourceType = GetSettingFromConfigOrEnvironment(SplunkSourceTypeSettingName);
            Index = GetSettingFromConfigOrEnvironment(SplunkIndexSettingName);
        }

        /// <summary>
        /// Uri of the Splunk Http Event Collector to send events to
        /// </summary>
        public Uri HecHost { get; set; }

        /// <summary>
        /// Token to use to authenticate with Splunk
        /// </summary>
        public Guid Token { get; set; }

        /// <summary>
        /// Host which will be assigned to each event
        /// </summary>
        public string Host { get; set; }

        /// <summary>
        /// Source which will be assigned to each event
        /// </summary>
        public string Source { get; set; }

        /// <summary>
        /// SourceType which will be assigned to each event
        /// </summary>
        public string SourceType { get; set; }

        /// <summary>
        /// Index where each event will be stored
        /// </summary>
        public string Index { get; set; }

        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            var cm = context.Config.GetService<IConverterManager>();
            cm.AddConverter<object, SplunkEvent>(ConvertObject2SplunkEvent);
            cm.AddConverter<string, SplunkEvent>(ConvertString2SplunkEvent);
            cm.AddConverter<Stream, SplunkEvent>(ConvertStream2SplunkEvent);
            var provider = new SplunkAttributeBindingProvider(cm, this);
            context.Config.RegisterBindingExtension(provider);
        }

        internal static string GetSettingFromConfigOrEnvironment(string key)
        {
            var value = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(value))
            {
                value = Environment.GetEnvironmentVariable(key);
            }

            return value;
        }

        private static SplunkEvent ConvertObject2SplunkEvent(object input)
        {
            var splunkEvent = new SplunkEvent
            {
                Event = input
            };
            return splunkEvent;
        }

        private static SplunkEvent ConvertString2SplunkEvent(string input)
        {
            var splunkEvent = new SplunkEvent
            {
                Event = input
            };
            return splunkEvent;
        }

        private static SplunkEvent ConvertStream2SplunkEvent(Stream input)
        {
            using (var reader = new StreamReader(input))
            {
                var splunkEvent = new SplunkEvent
                {
                    Event = reader.ReadToEnd()
                };<commit_msg>Adding support for converting JSON strings to objects<commit_after>﻿// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Configuration;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host.Config;
using WebJobs.Extensions.Splunk;
using WebJobs.Extensions.Splunk.Services;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace WebJobs.Extensions.Splunk
{
    /// <summary>
    /// Defines the configuration options for the Splunk binding.
    /// </summary>
    public class SplunkConfiguration : IExtensionConfigProvider
    {
        internal const string SplunkHecHostSettingName = "AzureWebJobsSplunkHttpEventCollectorHost";
        internal const string SplunkTokenSettingName = "AzureWebJobsSplunkToken";
        internal const string SplunkHostSettingName = "AzureWebJobsSplunkHost";
        internal const string SplunkSourceSettingName = "AzureWebJobsSplunkSource";
        internal const string SplunkSourceTypeSettingName = "AzureWebJobsSplunkSourceType";
        internal const string SplunkIndexSettingName = "AzureWebJobsSplunkIndex";

        private static JsonSerializer _serializer;

        static SplunkConfiguration()
        {
            _serializer = new JsonSerializer();
            _serializer.NullValueHandling = NullValueHandling.Ignore;
            _serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }

        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public SplunkConfiguration()
        {
            var host = GetSettingFromConfigOrEnvironment(SplunkHecHostSettingName);
            HecHost = host != null ? new Uri(host) : null;
            var token = GetSettingFromConfigOrEnvironment(SplunkTokenSettingName);
            Token = token != null ? new Guid(token) : Token; 
            Host = GetSettingFromConfigOrEnvironment(SplunkHostSettingName);
            Source = GetSettingFromConfigOrEnvironment(SplunkSourceSettingName);
            SourceType = GetSettingFromConfigOrEnvironment(SplunkSourceTypeSettingName);
            Index = GetSettingFromConfigOrEnvironment(SplunkIndexSettingName);
        }

        /// <summary>
        /// Uri of the Splunk Http Event Collector to send events to
        /// </summary>
        public Uri HecHost { get; set; }

        /// <summary>
        /// Token to use to authenticate with Splunk
        /// </summary>
        public Guid Token { get; set; }

        /// <summary>
        /// Host which will be assigned to each event
        /// </summary>
        public string Host { get; set; }

        /// <summary>
        /// Source which will be assigned to each event
        /// </summary>
        public string Source { get; set; }

        /// <summary>
        /// SourceType which will be assigned to each event
        /// </summary>
        public string SourceType { get; set; }

        /// <summary>
        /// Index where each event will be stored
        /// </summary>
        public string Index { get; set; }

        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            var cm = context.Config.GetService<IConverterManager>();
            cm.AddConverter<object, SplunkEvent>(ConvertObject2SplunkEvent);
            cm.AddConverter<string, SplunkEvent>(ConvertString2SplunkEvent);
            cm.AddConverter<Stream, SplunkEvent>(ConvertStream2SplunkEvent);
            var provider = new SplunkAttributeBindingProvider(cm, this);
            context.Config.RegisterBindingExtension(provider);
        }

        internal static string GetSettingFromConfigOrEnvironment(string key)
        {
            var value = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(value))
            {
                value = Environment.GetEnvironmentVariable(key);
            }

            return value;
        }

        private static SplunkEvent ConvertObject2SplunkEvent(object input)
        {
            var splunkEvent = new SplunkEvent
            {
                Event = input
            };
            return splunkEvent;
        }

        private static SplunkEvent ConvertString2SplunkEvent(string input)
        {
            using(var sr= new StringReader(input))
            {
                var jr = new JsonTextReader(sr);
                var splunkEvent = new SplunkEvent();

                try
                {
                    var obj = _serializer.Deserialize(jr);
                    splunkEvent.Event = obj;
                }
                catch
                {
                    splunkEvent.Event = input;
                }
                return splunkEvent;
            }
        }

        private static SplunkEvent ConvertStream2SplunkEvent(Stream input)
        {
            using (var reader = new StreamReader(input))
            {
                var splunkEvent = new SplunkEvent
                {
                    Event = reader.ReadToEnd()
                };
{'fraction_non_alphanumeric': 0.08001628830296244, 'fraction_numerical': 0.0012216227221826326, 'mean_word_length': 2.0218394340203014, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415144', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2320, 'n_words': 622}