Esempio n. 1
0
        private void ProcessResult <T>(ExecutionResponse <T> result, KieCommandTypeEnum commandType)
        {
            if (typeof(T) != typeof(object))
            {
                try
                {
                    var command    = Commands.FirstOrDefault(c => c.Command.CommandType == commandType);
                    var identifier = command?.Command as ICommandOutIdentifier;
                    var outObject  = result.Result?.ExecutionResults.Results.SingleOrDefault(e => e.Key == identifier?.OutIdentifier);

                    if (outObject != null)
                    {
                        var item  = (JObject)outObject.Value.Value;
                        var first = item.First;

                        if (first is JProperty)
                        {
                            var prop          = first as JProperty;
                            var commandObject = command?.Command as ICommandObject;
                            var v             = prop.Name.Equals(commandObject?.CommandObject);
                            if (true)
                            {
                                result.SmartSingleResponse = JsonConvert.DeserializeObject <T>(prop.Value.ToString(), new UnixTimestampConverter());
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Esempio n. 2
0
        private async Task <ExecutionResponse <T> > ExecuteCall <T>(string containerName)
        {
            Commands = Commands.OrderByDescending(c => c.Command.CommandType).ToList();
            var json   = JsonConvert.SerializeObject(this);
            var result = new ExecutionResponse <T>();

            using (var client = new HttpClient {
                BaseAddress = new Uri(HostUrl)
            })
            {
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(ApplicationType));

                if (!string.IsNullOrEmpty(AuthUserName))
                {
                    var byteArray = Encoding.ASCII.GetBytes($"{AuthUserName}:{AuthPassword}");
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AutenticationType, Convert.ToBase64String(byteArray));
                }

                try
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Post, string.Concat(InstancesPath, DefaultInstancesPath, containerName)))
                    {
                        request.Content = new StringContent(json);
                        request.Content.Headers.ContentType = new MediaTypeHeaderValue(ApplicationType);

                        //using (var response = await client.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false))
                        using (var response = await client.SendAsync(request))
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                result = await response.Content.ReadAsAsync <ExecutionResponse <T> >();

                                result.ResponseBody = await response.Content.ReadAsStringAsync();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errorType    = ex.GetType().ToString();
                    string errorMessage = errorType + ": " + ex.Message;
                    throw new Exception(errorMessage, ex.InnerException);
                }
            }

            result.RequestBody = json;

            return(result);
        }
Esempio n. 3
0
        private async Task <ExecutionResponse <T> > ExecuteAsync <T>(string containerName, KieCommandTypeEnum commandType)
        {
            var startDate = DateTime.Now;

            ExecutionResponse <T> result = await ExecuteCall <T>(containerName);

            ProcessResult(result, commandType);

            var span = DateTime.Now - startDate;

            result.ElapsedTime = (int)span.TotalMilliseconds;

            return(result);
        }
        public async Task <ExecutionResponse <T> > ExecuteAsync <T>(string containerName)
        {
            var startDate = DateTime.Now;

            Commands = Commands.OrderByDescending(c => c.Command.CommandType).ToList();

            var json   = JsonConvert.SerializeObject(this);
            var result = new ExecutionResponse <T>();

            using (var client = new HttpClient {
                BaseAddress = new Uri(HostUrl)
            })
            {
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                if (!string.IsNullOrEmpty(AuthUserName))
                {
                    var byteArray = Encoding.ASCII.GetBytes($"{AuthUserName}:{AuthPassword}");
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                }

                using (var request = new HttpRequestMessage(HttpMethod.Post, string.Concat(InstancesPath, "/services/rest/server/containers/instances/", containerName)))
                {
                    request.Content = new StringContent(json);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            result = await response.Content.ReadAsAsync <ExecutionResponse <T> >();

                            result.ResponseBody = await response.Content.ReadAsStringAsync();
                        }
                    }
                }
            }

            result.RequestBody = json;

            if (typeof(T) != typeof(object))
            {
                var command = Commands.FirstOrDefault(c => c.Command.CommandType == KieCommandTypeEnum.Insert);
                var entity  = command?.Command as CommandInsert;

                try
                {
                    var outObject =
                        result.Result?.ExecutionResults.Results.SingleOrDefault(e => e.Key == entity?.OutIdentifier);

                    if (outObject != null)
                    {
                        var item  = (JObject)outObject.Value.Value;
                        var first = item.First;

                        if (first is JProperty)
                        {
                            var prop = first as JProperty;

                            if (prop.Name.Equals(entity?.CommandObject.ObjectNameSpace))
                            {
                                result.SmartSingleResponse = JsonConvert.DeserializeObject <T>(prop.Value.ToString(), new UnixTimestampConverter());
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            var span = DateTime.Now - startDate;

            result.ElapsedTime = (int)span.TotalMilliseconds;

            return(result);
        }