Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Raw data from Correios #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/DotNet.CEP.Search/DotNet.CEP.Search.App/CepSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace DotNet.CEP.Search.App
/// Cep Search
/// </summary>
public class CepSearch : BaseCepSearch, ICepSearch
{
{

/// <summary>
/// Returns the address
Expand All @@ -37,6 +37,30 @@ public async Task<ResponseAddress> GetAddressByCepAsync(string cep, Cancellation
}
}

/// <summary>
/// Returns the raw data address
/// </summary>
/// <param name="cep">CEP number without '-'</param>
/// <param name="cancellationToken">Token to cancel task</param>
/// <returns>JSON with address</returns>
public async Task<ResponseCorreios> GetAddressByCepRawDataAsync(string cep, CancellationToken cancellationToken = default)
{
try
{
var address = await GetAddressFromCorreiosByCepRawData(cep, cancellationToken).ConfigureAwait(false);

return address;
}
catch (HttpRequestException)
{
throw;
}
catch (JsonSerializationException)
{
throw;
}
}

/// <summary>
/// Returns the address
/// </summary>
Expand All @@ -50,7 +74,7 @@ public ResponseAddress GetAddressByCep(string cep)
var address = GetAddressFromCorreiosByCep(cep, new CancellationToken()).Result;

return address;

}
catch (HttpRequestException)
{
Expand Down Expand Up @@ -122,7 +146,7 @@ private async Task<ResponseAddress> 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);

Expand All @@ -132,7 +156,7 @@ private async Task<ResponseAddress> GetAddressFromCorreiosByCep(string cep, Canc

var address = new ResponseAddress()
{
Infos = new AddressInfo[size]
Infos = new AddressInfo[size]
};

for (int i = 0; i < size; i++)
Expand All @@ -150,6 +174,27 @@ private async Task<ResponseAddress> GetAddressFromCorreiosByCep(string cep, Canc
return address;
}

private async Task<ResponseCorreios> GetAddressFromCorreiosByCepRawData(string cep, CancellationToken cancellationToken)
{
var dict = new Dictionary<string, string>
{
{"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<ResponseCorreios>(cepResponse);

return respConvertido;
}

private async Task<ResponseCep> GetCepFromCorreiosByAddress(string address, CancellationToken cancellationToken)
{
var dict = new Dictionary<string, string>
Expand Down Expand Up @@ -178,7 +223,7 @@ private async Task<ResponseCep> GetCepFromCorreiosByAddress(string address, Canc
};

for (int i = 0; i < respConvertido?.Dados.Length; i++)
{
{

cep.Infos[i] = new CepInfo
{
Expand Down