diff --git a/src/DotNet.CEP.Search/DotNet.CEP.Search.App/CepSearch.cs b/src/DotNet.CEP.Search/DotNet.CEP.Search.App/CepSearch.cs index 214d329..e00fd21 100644 --- a/src/DotNet.CEP.Search/DotNet.CEP.Search.App/CepSearch.cs +++ b/src/DotNet.CEP.Search/DotNet.CEP.Search.App/CepSearch.cs @@ -11,7 +11,7 @@ namespace DotNet.CEP.Search.App /// Cep Search /// public class CepSearch : BaseCepSearch, ICepSearch - { + { /// /// Returns the address @@ -37,6 +37,30 @@ public async Task GetAddressByCepAsync(string cep, Cancellation } } + /// + /// Returns the raw data address + /// + /// CEP number without '-' + /// Token to cancel task + /// JSON with address + public async Task GetAddressByCepRawDataAsync(string cep, CancellationToken cancellationToken = default) + { + try + { + var address = await GetAddressFromCorreiosByCepRawData(cep, cancellationToken).ConfigureAwait(false); + + return address; + } + catch (HttpRequestException) + { + throw; + } + catch (JsonSerializationException) + { + throw; + } + } + /// /// Returns the address /// @@ -50,7 +74,7 @@ public ResponseAddress GetAddressByCep(string cep) var address = GetAddressFromCorreiosByCep(cep, new CancellationToken()).Result; return address; - + } catch (HttpRequestException) { @@ -122,7 +146,7 @@ private async Task GetAddressFromCorreiosByCep(string cep, Canc { Content = new FormUrlEncodedContent(dict), }; - + var httpResponse = await _client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); var cepResponse = await httpResponse.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); @@ -132,7 +156,7 @@ private async Task GetAddressFromCorreiosByCep(string cep, Canc var address = new ResponseAddress() { - Infos = new AddressInfo[size] + Infos = new AddressInfo[size] }; for (int i = 0; i < size; i++) @@ -150,6 +174,27 @@ private async Task GetAddressFromCorreiosByCep(string cep, Canc return address; } + private async Task GetAddressFromCorreiosByCepRawData(string cep, CancellationToken cancellationToken) + { + var dict = new Dictionary + { + {"pagina","/app/endereco/index.php"}, + {"endereco",cep }, + {"tipoCEP","ALL" } + }; + + var httpRequest = new HttpRequestMessage(HttpMethod.Post, UrlCorreio) + { + Content = new FormUrlEncodedContent(dict), + }; + + var httpResponse = await _client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); + var cepResponse = await httpResponse.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var respConvertido = JsonConvert.DeserializeObject(cepResponse); + + return respConvertido; + } + private async Task GetCepFromCorreiosByAddress(string address, CancellationToken cancellationToken) { var dict = new Dictionary @@ -178,7 +223,7 @@ private async Task GetCepFromCorreiosByAddress(string address, Canc }; for (int i = 0; i < respConvertido?.Dados.Length; i++) - { + { cep.Infos[i] = new CepInfo {