-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9fec26
commit 43a88cc
Showing
12 changed files
with
364 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using OpenAdm.Application.Dtos.Response; | ||
using OpenAdm.Application.Interfaces; | ||
using OpenAdm.Application.Models.Fretes; | ||
using OpenAdm.Infra.Model; | ||
|
||
namespace OpenAdm.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("cnpj")] | ||
public class CnpjConsultaController : ControllerBase | ||
{ | ||
private readonly ICnpjConsultaService _cnpjConsultaService; | ||
|
||
public CnpjConsultaController(ICnpjConsultaService cnpjConsultaService) | ||
{ | ||
_cnpjConsultaService = cnpjConsultaService; | ||
} | ||
|
||
[HttpGet("consulta")] | ||
[ProducesResponseType<ConsultaCnpjResponse>(200)] | ||
[ProducesResponseType<ErrorResponse>(400)] | ||
public async Task<IActionResult> Consulta([FromQuery] string cnpj) | ||
{ | ||
var response = await _cnpjConsultaService.ConsultaCnpjAsync(cnpj); | ||
return Ok(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using OpenAdm.Infra.Model; | ||
|
||
namespace OpenAdm.Application.Interfaces; | ||
|
||
public interface ICnpjConsultaService | ||
{ | ||
Task<ConsultaCnpjResponse> ConsultaCnpjAsync(string cnpj); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using OpenAdm.Application.Interfaces; | ||
using OpenAdm.Domain.Exceptions; | ||
using OpenAdm.Infra.HttpService.Interfaces; | ||
using OpenAdm.Infra.Model; | ||
|
||
namespace OpenAdm.Application.Services; | ||
|
||
public class CnpjConsultaService : ICnpjConsultaService | ||
{ | ||
private readonly ICnpjHttpService _cnpjHttpService; | ||
|
||
public CnpjConsultaService(ICnpjHttpService cnpjHttpService) | ||
{ | ||
_cnpjHttpService = cnpjHttpService; | ||
} | ||
|
||
public async Task<ConsultaCnpjResponse> ConsultaCnpjAsync(string cnpj) | ||
{ | ||
if (string.IsNullOrWhiteSpace(cnpj)) | ||
{ | ||
throw new ExceptionApi("CNPJ inválido"); | ||
} | ||
|
||
return await _cnpjHttpService.ConsultarCnpjAsync(cnpj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace OpenAdm.Infra.Enums; | ||
|
||
public enum HttpServiceEnum | ||
{ | ||
ConsultaCnpj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using OpenAdm.Infra.Model; | ||
|
||
namespace OpenAdm.Infra.HttpService.Interfaces; | ||
|
||
public interface ICnpjHttpService | ||
{ | ||
Task<ConsultaCnpjResponse> ConsultarCnpjAsync(string cnpj); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Text.Json; | ||
using OpenAdm.Domain.Exceptions; | ||
using OpenAdm.Infra.Enums; | ||
using OpenAdm.Infra.HttpService.Interfaces; | ||
using OpenAdm.Infra.Model; | ||
|
||
namespace OpenAdm.Infra.HttpService.Services; | ||
|
||
public class CnpjHttpService : ICnpjHttpService | ||
{ | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public CnpjHttpService(IHttpClientFactory httpClientFactory) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
public async Task<ConsultaCnpjResponse> ConsultarCnpjAsync(string cnpj) | ||
{ | ||
var client = _httpClientFactory.CreateClient(HttpServiceEnum.ConsultaCnpj.ToString()); | ||
|
||
var response = await client.GetAsync(cnpj); | ||
var body = await response.Content.ReadAsStreamAsync(); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
if (body == null || body.Length == 0) | ||
{ | ||
throw new ExceptionApi("Não foi possível obter a resposta da consulta do seu CNPJ"); | ||
} | ||
|
||
var erro = JsonSerializer.Deserialize<ConsultaCnpjErroResponse>(body, JsonSerializerOptionsApi.Options()); | ||
|
||
if (!string.IsNullOrWhiteSpace(erro?.Message)) | ||
{ | ||
throw new ExceptionApi(erro.Message); | ||
} | ||
|
||
throw new ExceptionApi("Não foi possível efetuar a consulta do seu CNPJ"); | ||
} | ||
|
||
return JsonSerializer.Deserialize<ConsultaCnpjResponse>(body, JsonSerializerOptionsApi.Options()) | ||
?? throw new ExceptionApi("Não foi possível efetuar a consulta do seu CNPJ!"); ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace OpenAdm.Infra.Model; | ||
|
||
public class ConsultaCnpjErroResponse | ||
{ | ||
public string Message { get; set; } = string.Empty; | ||
} |
Oops, something went wrong.