Example #1
0
        /*public async Task<PredictionResponse> Predict(PredictionRequest predictionRequest)
         * {
         *  if (predictionRequest?.Items == null || !predictionRequest.Items.Any())
         *  {
         *      throw new ArgumentException("The request is not correct.", nameof(predictionRequest));
         *  }
         *
         *  if (this.serviceConfiguration.PredictionProviderType == PredictionProviderType.ClientExecutable)
         *  {
         *      this.ValidatePredictionExecubablePath();
         *  }
         *
         *  var guid = Guid.NewGuid().ToString();
         *  this.logger.Info($"Generating prediction request guid '{guid}'");
         *
         *  this.logger.Info("Inserting requests");
         *  await this.InsertRequests(predictionRequest, guid);
         *
         *  if (this.serviceConfiguration.PredictionProviderType == PredictionProviderType.Server)
         *  {
         *      this.logger.Info("Calling prediction service");
         *      await this.CallPredictionService(guid);
         *  }
         *  else
         *  {
         *      this.logger.Info("Running prediction executable");
         *      await this.RunPredictionExecutable(guid);
         *  }
         *
         *  this.logger.Info($"Prediction service has finished.");
         *
         *  this.logger.Info("Generating response");
         *  return await this.GenerateResponse(predictionRequest, guid);
         * }*/
        #endregion

        #region My Methods
        private async Task CallPredictionService(string guid)
        {
            using (PredictionServiceClient client = new PredictionServiceClient(new Uri(this.serviceConfiguration.RServerUrl, UriKind.RelativeOrAbsolute)))
            {
                client.HttpClient.Timeout = this.serviceConfiguration.RServerClientTimeout;

                var loginParameters = new LoginRequest(this.serviceConfiguration.RServerUserName, this.serviceConfiguration.RServerPassword);

                var loginResult = await client.LoginWithHttpMessagesAsync(loginParameters);

                if (string.IsNullOrWhiteSpace(loginResult.Body.AccessToken))
                {
                    throw new SecurityException("Could not log in to prediction service.");
                }

                var headers     = client.HttpClient.DefaultRequestHeaders;
                var accessToken = loginResult.Body.AccessToken;
                headers.Remove("Authorization");
                headers.Add("Authorization", $"Bearer {accessToken}");

                var result = await client.PredictWithHttpMessagesAsync(new InputParameters(guid));

                if (!string.IsNullOrWhiteSpace(result.Body.ErrorMessage))
                {
                    throw new Exception(result.Body.ErrorMessage);
                }
            }
        }