.history/generated/Module_20241126190136.cs

// Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/powershell@4.0.708)
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

namespace Commvault.Powershell
{
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Security.Cryptography.X509Certificates;
    using static Commvault.Powershell.Runtime.Extensions;
    using SignalDelegate = global::System.Func<string, global::System.Threading.CancellationToken, global::System.Func<global::System.EventArgs>, global::System.Threading.Tasks.Task>;
    using EventListenerDelegate = global::System.Func<string, global::System.Threading.CancellationToken, global::System.Func<global::System.EventArgs>, global::System.Func<string, global::System.Threading.CancellationToken, global::System.Func<global::System.EventArgs>, global::System.Threading.Tasks.Task>, global::System.Management.Automation.InvocationInfo, string, global::System.Exception, global::System.Threading.Tasks.Task>;



    public class InsecureHttpClientFactory : Commvault.Powershell.Runtime.HttpClientFactory //Creates HttpClient without validation
    {
        public InsecureHttpClientFactory() : base(new HttpClient()) { }

        public HttpClient CreateHttpClient()
        {
            var handler = new HttpClientHandler();
            //This is INSECURE and only acceptable for controlled testing environments!!
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            return new HttpClient(handler);
        }
    }
    
    /// <summary>A class that contains the module-common code and data.</summary>
    public partial class Module
    {
        public global::System.Net.Http.HttpClientHandler _handler = new global::System.Net.Http.HttpClientHandler{
            ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
        };

        private static bool _init = false;

        private static readonly global::System.Object _initLock = new global::System.Object();

        private static Commvault.Powershell.Module _instance;

        /// <summary>the ISendAsync pipeline instance</summary>
        private Commvault.Powershell.Runtime.HttpPipeline _pipeline;

        /// <summary>the ISendAsync pipeline instance (when proxy is enabled)</summary>
        private Commvault.Powershell.Runtime.HttpPipeline _pipelineWithProxy;

        private static readonly global::System.Object _singletonLock = new global::System.Object();

        public bool _useProxy = false;

        public global::System.Net.WebProxy _webProxy = new global::System.Net.WebProxy();

        /// <summary>The instance of the Client API</summary>
        public Commvault.Powershell.CommvaultPowerShell ClientAPI { get; set; }

        /// <summary>A delegate that gets called for each signalled event</summary>
        public EventListenerDelegate EventListener { get; set; }

        /// <summary>the singleton of this module class</summary>
        public static Commvault.Powershell.Module Instance { get { if (_instance == null) { lock (_singletonLock) { if (_instance == null) { _instance = new Module(); }}} return _instance; } }

        /// <summary>The Name of this module</summary>
        public string Name => @"CommvaultPowerShell";

        /// <param name="invocationInfo">The <see cref="System.Management.Automation.InvocationInfo" /> from the cmdlet</param>
        /// <param name="pipeline">The HttpPipeline for the request</param>

        partial void AfterCreatePipeline(global::System.Management.Automation.InvocationInfo invocationInfo, ref Commvault.Powershell.Runtime.HttpPipeline pipeline);

        /// <param name="invocationInfo">The <see cref="System.Management.Automation.InvocationInfo" /> from the cmdlet</param>
        /// <param name="pipeline">The HttpPipeline for the request</param>

        partial void BeforeCreatePipeline(global::System.Management.Automation.InvocationInfo invocationInfo, ref Commvault.Powershell.Runtime.HttpPipeline pipeline);

        partial void CustomInit();

        /// <summary>Creates an instance of the HttpPipeline for each call.</summary>
        /// <param name="invocationInfo">The <see cref="System.Management.Automation.InvocationInfo" /> from the cmdlet</param>
        /// <param name="parameterSetName">the cmdlet's parameterset name.</param>
        /// <param name="extensibleParameters">a dict for extensible parameters</param>
        /// <returns>An instance of Commvault.Powershell.Runtime.HttpPipeline for the remote call.</returns>
        public Commvault.Powershell.Runtime.HttpPipeline CreatePipeline(global::System.Management.Automation.InvocationInfo invocationInfo, string parameterSetName = null, global::System.Collections.Generic.IDictionary<string,object> extensibleParameters = null)
        {
            Commvault.Powershell.Runtime.HttpPipeline pipeline = null;
            BeforeCreatePipeline(invocationInfo, ref pipeline);
            pipeline = (pipeline ?? (_useProxy ? _pipelineWithProxy : _pipeline)).Clone();
            AfterCreatePipeline(invocationInfo, ref pipeline);
            return pipeline;
        }

        /// <summary>Initialization steps performed after the module is loaded.</summary>
        public void Init()
        {
            // called at module init time...
            if (_init == false)
            {
                lock (_initLock) {
                    if (_init == false) {
                        CustomInit();
                        _init = true;
                    }
                }
            }
        }

        /// <summary>Creates the module instance.</summary>
        private Module()
        {
            // Constructor
            ClientAPI = new Commvault.Powershell.CommvaultPowerShell();

            // Initialize the HttpClientHandler with proper configuration
            _handler = new global::System.Net.Http.HttpClientHandler
            {
                // Configure proxy if required
                Proxy = _webProxy,
                
                // Optional: Disable SSL certificate validation (use ONLY in development/test environments)
                ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true
            };

            // Initialize pipelines
            _pipeline = new Commvault.Powershell.Runtime.HttpPipeline(
                new Commvault.Powershell.Runtime.HttpClientFactory(new global::System.Net.Http.HttpClient(_handler))
            );
            _pipelineWithProxy = new Commvault.Powershell.Runtime.HttpPipeline(
                new Commvault.Powershell.Runtime.HttpClientFactory(new global::System.Net.Http.HttpClient(_handler))
            );
        }


        /// <param name="proxy">The HTTP Proxy to use.</param>
        /// <param name="proxyCredential">The HTTP Proxy Credentials</param>
        /// <param name="proxyUseDefaultCredentials">True if the proxy should use default credentials</param>
        public void SetProxyConfiguration(global::System.Uri proxy, global::System.Management.Automation.PSCredential proxyCredential, bool proxyUseDefaultCredentials)
        {
            _useProxy = proxy != null;
            if (proxy == null)
            {
                return;
            }
            // set the proxy configuration
            _webProxy.Address = proxy;
            _webProxy.BypassProxyOnLocal = false;
            if (proxyUseDefaultCredentials)
            {
                _webProxy.Credentials = null;
                _webProxy.UseDefaultCredentials = true;
            }
            else
            {
                _webProxy.UseDefaultCredentials = false;
                _webProxy.Credentials = proxyCredential ?.GetNetworkCredential();
            }
        }

        /// <summary>Called to dispatch events to the common module listener</summary>
        /// <param name="id">The ID of the event </param>
        /// <param name="token">The cancellation token for the event </param>
        /// <param name="getEventData">A delegate to get the detailed event data</param>
        /// <param name="signal">The callback for the event dispatcher </param>
        /// <param name="invocationInfo">The <see cref="System.Management.Automation.InvocationInfo" /> from the cmdlet</param>
        /// <param name="parameterSetName">the cmdlet's parameterset name.</param>
        /// <param name="exception">the exception that is being thrown (if available)</param>
        /// <returns>
        /// A <see cref="global::System.Threading.Tasks.Task" /> that will be complete when handling of the event is completed.
        /// </returns>
        public async global::System.Threading.Tasks.Task Signal(string id, global::System.Threading.CancellationToken token, global::System.Func<global::System.EventArgs> getEventData, SignalDelegate signal, global::System.Management.Automation.InvocationInfo invocationInfo, string parameterSetName, global::System.Exception exception)
        {
            using( NoSynchronizationContext )
            {
                if (EventListener != null)
                {
                    await EventListener.Invoke(id,token,getEventData, signal, invocationInfo, parameterSetName,exception);
                }
            }
        }
    }
}