From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- Codemerx/Gestor.Application/Servicos/CepService.cs | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 Codemerx/Gestor.Application/Servicos/CepService.cs (limited to 'Codemerx/Gestor.Application/Servicos/CepService.cs') diff --git a/Codemerx/Gestor.Application/Servicos/CepService.cs b/Codemerx/Gestor.Application/Servicos/CepService.cs new file mode 100644 index 0000000..f33d711 --- /dev/null +++ b/Codemerx/Gestor.Application/Servicos/CepService.cs @@ -0,0 +1,161 @@ +using Agger.Registro; +using Gestor.Application.Helpers; +using Gestor.Common.Validation; +using Gestor.Model.Domain.Generic; +using Gestor.Model.Helper; +using Newtonsoft.Json.Linq; +using System; +using System.Diagnostics; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Gestor.Application.Servicos +{ + public class CepService + { + private const string GuidGestor = "bbbf4f03-01fc-4300-b430-33e007753578"; + + public CepService() + { + } + + private static async Task Authorization() + { + string str; + string empty; + using (HttpClient httpClient = new HttpClient()) + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + string str1 = DateTime.Now.ToString("yyyy-MM-dd"); + string base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}|{1}|{2}", ApplicationHelper.IdFornecedor, "bbbf4f03-01fc-4300-b430-33e007753578", str1))); + Uri apiCep = Address.get_ApiCep(); + string[] strArrays = new string[] { "/auth" }; + Uri uri = apiCep.Append(strArrays); + HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(uri, base64String.ToHttpContent(null, "application/json")); + if (httpResponseMessage.get_IsSuccessStatusCode()) + { + JToken item = httpResponseMessage.get_Content().ToJObject().get_Item("token"); + if (item != null) + { + empty = item.ToString(); + } + else + { + empty = null; + } + } + else + { + empty = string.Empty; + } + str = empty; + } + return str; + } + + public async Task SearchDirect(string cep) + { + return await this.SearchDirectApiAgger(cep); + } + + public async Task SearchDirectApiAgger(string cep) + { + EnderecoBase enderecoBase; + string upper; + string str; + string upper1; + string str1; + string upper2; + try + { + string str2 = Gestor.Model.Helper.ValidationHelper.OnlyNumber(cep); + if (!Gestor.Common.Validation.ValidationHelper.IsNullOrEmpty(str2)) + { + using (HttpClient httpClient = new HttpClient()) + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + string str3 = await CepService.Authorization(); + httpClient.get_DefaultRequestHeaders().TryAddWithoutValidation("Authorization", string.Concat("Bearer ", str3)); + Uri apiCep = Address.get_ApiCep(); + string[] strArrays = new string[] { str2 }; + Uri uri = apiCep.Append(strArrays); + string str4 = await await httpClient.GetAsync(uri).get_Content().ReadAsStringAsync(); + JObject jObject = JObject.Parse(str4); + if (jObject == null || str4.Equals("Endereço não encontrado")) + { + enderecoBase = null; + } + else + { + EnderecoBase enderecoBase1 = new EnderecoBase(); + JToken item = jObject.get_Item("logradouro"); + if (item != null) + { + upper = item.ToString().ToUpper(); + } + else + { + upper = null; + } + enderecoBase1.set_Endereco(upper); + JToken jToken = jObject.get_Item("bairro"); + if (jToken != null) + { + str = jToken.ToString().ToUpper(); + } + else + { + str = null; + } + enderecoBase1.set_Bairro(str); + JToken item1 = jObject.get_Item("cidade"); + if (item1 != null) + { + upper1 = item1.ToString().ToUpper(); + } + else + { + upper1 = null; + } + enderecoBase1.set_Cidade(upper1); + JToken jToken1 = jObject.get_Item("estado"); + if (jToken1 != null) + { + str1 = jToken1.ToString().ToUpper(); + } + else + { + str1 = null; + } + enderecoBase1.set_Estado(str1); + JToken item2 = jObject.get_Item("cep"); + if (item2 != null) + { + upper2 = item2.ToString().ToUpper(); + } + else + { + upper2 = null; + } + enderecoBase1.set_Cep(upper2); + enderecoBase = enderecoBase1; + } + } + } + else + { + enderecoBase = null; + } + } + catch (Exception exception) + { + enderecoBase = null; + } + return enderecoBase; + } + } +} \ No newline at end of file -- cgit v1.2.3