/// <summary> Initializes a new instance of PersonalizerClient. </summary>
        /// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
        /// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
        /// <param name="options"> The options for configuring the client. </param>
        public PersonalizerClient(Uri endpoint, TokenCredential credential, PersonalizerClientOptions options = null)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            options ??= new PersonalizerClientOptions();
            clientDiagnostics              = new ClientDiagnostics(options);
            pipeline                       = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes));
            stringEndpoint                 = endpoint.AbsoluteUri;
            RankRestClient                 = new RankRestClient(clientDiagnostics, pipeline, stringEndpoint);
            EventsRestClient               = new EventsRestClient(clientDiagnostics, pipeline, stringEndpoint);
            MultiSlotRestClient            = new MultiSlotRestClient(clientDiagnostics, pipeline, stringEndpoint);
            MultiSlotEventsRestClient      = new MultiSlotEventsRestClient(clientDiagnostics, pipeline, stringEndpoint);
            ServiceConfigurationRestClient = new ServiceConfigurationRestClient(clientDiagnostics, pipeline, stringEndpoint);
            PolicyRestClient               = new PolicyRestClient(clientDiagnostics, pipeline, stringEndpoint);
            tokenCredential                = credential;

            this.useLocalInference = options.UseLocalInference;
            if (useLocalInference)
            {
                validateAndAssignSampleRate(options.SubsampleRate);
                //lazy load Rankprocessor
                rlNetProcessor = new Lazy <RlNetProcessor>(() => GetConfigurationForRankProcessor());
            }
        }
        /// <summary> Initializes a new instance of LogClient. </summary>
        /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
        /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
        /// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
        internal PersonalizerAdministrationClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint)
        {
            string stringEndpoint = endpoint.AbsoluteUri;

            LogRestClient = new LogRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            ModelRestClient       = new ModelRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            EvaluationsRestClient = new EvaluationsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            PolicyRestClient      = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            _clientDiagnostics    = clientDiagnostics;
            _pipeline             = pipeline;
        }
 /// <summary> Apply Learning Settings and model from a pre-existing Offline Evaluation, making them the current online Learning Settings and model and replacing the previous ones. </summary>
 /// <param name="body"> The PolicyReferenceContract to use. </param>
 /// <param name="cancellationToken"> The cancellation token to use. </param>
 public virtual Response ApplyPersonalizerEvaluation(PersonalizerPolicyReferenceOptions body, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("PersonalizerAdministrationClient.ApplyPersonalizerEvaluation");
     scope.Start();
     try
     {
         return(ServiceConfigurationRestClient.ApplyFromEvaluation(body, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
 /// <summary> Get the Personalizer service configuration. </summary>
 /// <param name="cancellationToken"> The cancellation token to use. </param>
 public virtual Response <PersonalizerServiceProperties> GetPersonalizerProperties(CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("PersonalizerAdministrationClient.GetPersonalizerProperties");
     scope.Start();
     try
     {
         return(ServiceConfigurationRestClient.Get(cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
 /// <summary> Update the Personalizer service configuration. </summary>
 /// <param name="config"> The personalizer service configuration. </param>
 /// <param name="cancellationToken"> The cancellation token to use. </param>
 public virtual async Task <Response <PersonalizerServiceProperties> > UpdatePersonalizerPropertiesAsync(PersonalizerServiceProperties config, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("PersonalizerAdministrationClient.UpdatePersonalizerProperties");
     scope.Start();
     try
     {
         return(await ServiceConfigurationRestClient.UpdateAsync(config, cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
        public ServiceConfigurationClient(string endpoint, AzureKeyCredential credential, PersonalizerClientOptions options = null)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            options ??= new PersonalizerClientOptions();
            _clientDiagnostics = new ClientDiagnostics(options);
            _pipeline          = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "Ocp-Apim-Subscription-Key"));
            RestClient         = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, endpoint);
        }
        public ServiceConfigurationClient(string endpoint, TokenCredential credential, PersonalizerClientOptions options = null)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            options ??= new PersonalizerClientOptions();
            _clientDiagnostics = new ClientDiagnostics(options);
            string[] scopes = { "https://cognitiveservices.azure.com/.default" };
            _pipeline  = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes));
            RestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, endpoint);
        }
        /// <summary> Gets the rank processor initiated with live model to use </summary>
        internal virtual RlNetProcessor GetConfigurationForRankProcessor(CancellationToken cancellationToken = default)
        {
            Configuration config = new Configuration();

            // set up the model
            if (azureKeyCredential != null)
            {
                config["http.api.key"] = azureKeyCredential.Key;
            }
            else if (tokenCredential != null)
            {
                var         tokenRequestContext = new TokenRequestContext(scopes);
                AccessToken token = tokenCredential.GetToken(tokenRequestContext, cancellationToken);
                config["http.api.key"]             = "Bearer " + token.Token;
                config["http.api.header.key.name"] = "Authorization";
                tokenExpiry = token.ExpiresOn;
            }
            else
            {
                throw new ApplicationException("PersonalizerClient is neither initalized with Token Credential nor with AzureKey Credential");
            }
            personalizerServiceProperties = ServiceConfigurationRestClient.Get(cancellationToken);
            personalizerPolicy            = PolicyRestClient.Get(cancellationToken);
            //interactions & observations
            config["interaction.http.api.host"]         = stringEndpoint + "personalizer/v1.1-preview.3/logs/interactions";
            config["observation.http.api.host"]         = stringEndpoint + "personalizer/v1.1-preview.3/logs/observations";
            config["interaction.sender.implementation"] = "INTERACTION_HTTP_API_SENDER";
            config["observation.sender.implementation"] = "OBSERVATION_HTTP_API_SENDER";
            config["interaction.subsample.rate"]        = Convert.ToString(this.subsampleRate, CultureInfo.InvariantCulture);
            config["observation.subsample.rate"]        = Convert.ToString(this.subsampleRate, CultureInfo.InvariantCulture);
            //model
            config["model.blob.uri"] = stringEndpoint + "personalizer/v1.1-preview.3/model";
            config["model.source"]   = "HTTP_MODEL_DATA";

            config["model.vw.initial_command_line"] = personalizerPolicy.Arguments;
            config["protocol.version"]            = "2";
            config["initial_exploration.epsilon"] = Convert.ToString(personalizerServiceProperties.ExplorationPercentage, CultureInfo.InvariantCulture);
            config["rank.learning.mode"]          = Convert.ToString(personalizerServiceProperties.LearningMode, CultureInfo.InvariantCulture);
            LiveModel liveModel = new LiveModel(config);

            liveModel.Init();
            LiveModelBase liveModelAdapter = new LiveModelAdapter(liveModel);

            liveModelLastRefresh = DateTimeOffset.UtcNow;
            return(new RlNetProcessor(liveModelAdapter));
        }
        /// <summary> Initializes a new instance of PersonalizerClient. </summary>
        /// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
        /// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
        /// <param name="options"> The options for configuring the client. </param>
        public PersonalizerAdministrationClient(Uri endpoint, AzureKeyCredential credential, PersonalizerClientOptions options = null)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            options ??= new PersonalizerClientOptions();
            _clientDiagnostics = new ClientDiagnostics(options);
            _pipeline          = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "Ocp-Apim-Subscription-Key"));
            string stringEndpoint = endpoint.AbsoluteUri;

            LogRestClient = new LogRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            ModelRestClient       = new ModelRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            EvaluationsRestClient = new EvaluationsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            PolicyRestClient      = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
        }
        /// <summary> Initializes a new instance of PersonalizerClient. </summary>
        /// <param name="endpoint"> Supported Cognitive Services endpoint. </param>
        /// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
        /// <param name="options"> The options for configuring the client. </param>
        public PersonalizerAdministrationClient(Uri endpoint, TokenCredential credential, PersonalizerClientOptions options = null)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            options ??= new PersonalizerClientOptions();
            _clientDiagnostics = new ClientDiagnostics(options);
            string[] scopes = { "https://cognitiveservices.azure.com/.default" };
            _pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes));
            string stringEndpoint = endpoint.AbsoluteUri;

            LogRestClient = new LogRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            ServiceConfigurationRestClient = new ServiceConfigurationRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            ModelRestClient       = new ModelRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            EvaluationsRestClient = new EvaluationsRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
            PolicyRestClient      = new PolicyRestClient(_clientDiagnostics, _pipeline, stringEndpoint);
        }
 internal ServiceConfigurationClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint)
 {
     RestClient         = new ServiceConfigurationRestClient(clientDiagnostics, pipeline, endpoint);
     _clientDiagnostics = clientDiagnostics;
     _pipeline          = pipeline;
 }