Exemple #1
0
        async public Task create()
        {
            if (custom)
            {
                bool exists = await this.tableExists();

                IBatchProducer batch = _serviceLayerConnector.CreateBatch();

                if (!exists)
                {
                    string table = tablePayload();

                    ServiceLayerResponse response = await _serviceLayerConnector.Post("UserTablesMD", table, true);

                    if (!response.success)
                    {
                        // TODO: REGISTRAR LOG DE TABELA NÃO CRIADA
                        System.Console.WriteLine($"Erro ao criar a tabela '{this.name}': {response.errorCode}-{response.errorMessage}");
                    }
                }
            }

            foreach (var c in columns)
            {
                string newColumn = columnPayload(c);
                ServiceLayerResponse response = await _serviceLayerConnector.Post("UserFieldsMD", newColumn, true);

                if (!response.success && response.errorCode != FIELD_ALREADY_EXISTS)
                {
                    // TODO: REGISTRAR LOG DE CAMPOS NÃO CRIADOS
                    System.Console.WriteLine($"Erro ao criar o campo '{c.name}': {response.errorCode}-{response.errorMessage}");
                }
            }

            if (custom)
            {
                foreach (var i in indexes)
                {
                    string newIndex = indexPayload(i);

                    ServiceLayerResponse response = await _serviceLayerConnector.Post("UserKeysMD", newIndex, true);

                    if (!response.success)
                    {
                        // TODO: REGISTRAR LOG DE INDICES NÃO CRIADOS
                        System.Console.WriteLine($"Erro ao criar o índice '{i.name}': {response.errorCode}-{response.errorMessage}");
                    }
                }
            }

            System.Console.WriteLine("OK");
        }
Exemple #2
0
        async public Task Insert(Product entity)
        {
            string record = toJson(entity);

            ServiceLayerResponse response = await _serviceLayerConnector.Post("U_VSIS_PRODUCT", record);

            if (!response.success)
            {
                string message = $"Erro ao inserir dados em '{entity.EntityName}': {response.errorCode}-{response.errorMessage}";

                Console.WriteLine(message);
                throw new ApplicationException(message);
            }
        }
Exemple #3
0
        async public Task Update(Product entity)
        {
            string record = toJson(entity);
            string query  = Uri.EscapeUriString($"U_VSIS_PRODUCT('{entity.id}')");

            ServiceLayerResponse response = await _serviceLayerConnector.Patch(query, record);

            if (!response.success)
            {
                string message = $"Erro ao atualizar o registro '{entity.id}' em '{entity.EntityName}': {response.errorCode}-{response.errorMessage}";

                Console.WriteLine(message);
                throw new ApplicationException(message);
            }
        }
Exemple #4
0
        async public Task Insert(List <Product> entities)
        {
            List <string> records = new List <string>();

            foreach (var e in entities)
            {
                records.Add(toJson(e));
            }

            ServiceLayerResponse response = await _serviceLayerConnector.PostMany("U_VSIS_PRODUCT", records);

            if (!response.success)
            {
                string message = $"Erro ao inserir dados em '{entities[0].EntityName}': {response.errorCode}-{response.errorMessage}";
                Console.WriteLine(message);
                throw new ApplicationException(message);
            }
        }
Exemple #5
0
        async public Task Insert(Model.LegacyFiscalOperations entity)
        {
            IBatchProducer batch = _serviceLayerConnector.CreateBatch();

            batch = _serviceLayerConnector.CreateBatch();
            string record = toJson(entity);

            batch.Post(HttpMethod.Post, "/U_VSCATLGCYCFOP", record);

            ServiceLayerResponse response = await _serviceLayerConnector.Post(batch);


            if (!response.success)
            {
                string message = $"Erro ao enviar transação de '{entity.EntityName}': {response.errorCode}-{response.errorMessage}";
                Console.WriteLine(message);
                throw new ApplicationException(message);
            }
        }
Exemple #6
0
        async public Task <List <ServiceLayerResponse> > parseMultiPartResponse(HttpResponseMessage response)
        {
            List <ServiceLayerResponse> responseList = new List <ServiceLayerResponse>();

            var qp = from p in response.Content.Headers.ContentType.Parameters
                     where p.Name == "boundary"
                     select p;

            var parameter = qp.FirstOrDefault();

            string boundary = parameter == null ? "" : parameter.Value;

            using (Stream responseMessage = await response.Content.ReadAsStreamAsync())
                using (StreamReader reader = new StreamReader(responseMessage))
                {
                    int           statusCode    = 0;
                    bool          beginData     = false;
                    string        statusMessage = string.Empty;
                    StringBuilder submessage    = new StringBuilder(512);

                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();

                        if (line.StartsWith($"--{boundary}"))
                        {
                            if (submessage.Length > 0)
                            {
                                ServiceLayerResponse slResponse = new ServiceLayerResponse();

                                if (statusCode == (int)HttpStatusCode.BadRequest)
                                {
                                    ServiceLayerError error = parseError(submessage.ToString());
                                    slResponse.success      = false;
                                    slResponse.errorCode    = error.code;
                                    slResponse.errorMessage = error.message.value;
                                }
                                else if (statusCode >= 300)
                                {
                                    slResponse.success      = false;
                                    slResponse.errorCode    = statusCode.ToString();
                                    slResponse.errorMessage = statusMessage;
                                }
                                else
                                {
                                    slResponse.success      = true;
                                    slResponse.errorCode    = "";
                                    slResponse.errorMessage = "";
                                    slResponse.data         = submessage.ToString();
                                }

                                responseList.Add(slResponse);

                                submessage.Clear();
                                statusCode    = 0;
                                statusMessage = string.Empty;
                                beginData     = false;
                            }
                        }
                        else if (line.StartsWith("HTTP"))
                        {
                            string[] httpResponse = line.Split(' ');
                            statusCode    = int.Parse(httpResponse[1]);
                            statusMessage = string.Join(' ', httpResponse, 2, httpResponse.Length - 2);
                        }
                        else if (statusCode != 0 && line == string.Empty)
                        {
                            beginData = true;
                        }
                        else if (beginData)
                        {
                            submessage.Append(line);
                        }
                    }
                }

            return(responseList);
        }
Exemple #7
0
        public async Task <ServiceLayerResponse> Send(HttpMethod method, string query, HttpContent payload, bool noRoute = false, bool returnContent = false)
        {
            ServiceLayerResponse slResponse = new ServiceLayerResponse();

            addSecurityCookies(noRoute);

            HttpRequestMessage message = new HttpRequestMessage(method, query);

            message.Content = payload;
            if (!returnContent)
            {
                message.Headers.Add("Prefer", "return-no-content");
            }
            HttpResponseMessage response = new HttpResponseMessage();

            DateTime inicio = DateTime.Now;

            try
            {
                response = await httpClient.SendAsync(message);
            }
            catch (Exception e)
            {
            }

            var fim = DateTime.Now - inicio;

            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                string responseContent = await response.Content.ReadAsStringAsync();

                ServiceLayerError error = parseError(responseContent);

                slResponse.success      = false;
                slResponse.errorCode    = error.code;
                slResponse.errorMessage = error.message.value;

                _logger.LogError($"{method.Method.ToUpper()} {query}: {error.code} {error.message.value}");
            }
            else if (!response.IsSuccessStatusCode)
            {
                slResponse.success      = false;
                slResponse.errorCode    = response.StatusCode.ToString();
                slResponse.errorMessage = response.ReasonPhrase;

                _logger.LogError($"{method.Method.ToUpper()} {query}: {slResponse.errorCode} {slResponse.errorMessage}");
            }
            else
            {
                slResponse.success      = true;
                slResponse.errorCode    = "";
                slResponse.errorMessage = "";

                _logger.LogInformation($"{method.Method.ToUpper()} {query}: Success");

                if (payload is MultipartContent)
                {
                    slResponse.internalResponses = await parseMultiPartResponse(response);

                    if (slResponse.internalResponses != null)
                    {
                        foreach (var r in slResponse.internalResponses)
                        {
                            if (!r.success)
                            {
                                _logger.LogError($"{method.Method.ToUpper()} {query}: {r.errorCode} {r.errorMessage}");
                            }
                        }
                    }
                }
                else
                {
                    slResponse.data = await response.Content.ReadAsStringAsync();
                }
            }

            return(slResponse);
        }