blob: f33d711bc2e3092548eba0886a099ab9736b6a7c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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;
}
}
}
|