diff options
Diffstat (limited to 'Gestor.Application/Servicos/CepService.cs')
| -rw-r--r-- | Gestor.Application/Servicos/CepService.cs | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/Gestor.Application/Servicos/CepService.cs b/Gestor.Application/Servicos/CepService.cs new file mode 100644 index 0000000..f33d711 --- /dev/null +++ b/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<string> 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<string>(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<EnderecoBase> SearchDirect(string cep)
+ {
+ return await this.SearchDirectApiAgger(cep);
+ }
+
+ public async Task<EnderecoBase> 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 |