fetchViaCepService function

Future<Cep> fetchViaCepService(
  1. String cepWithLeftPad
)

Implementation

Future<Cep> fetchViaCepService(String cepWithLeftPad) async {
  final url = Uri.parse('https://viacep.com.br/ws/$cepWithLeftPad/json/');
  late Cep resposta;
  try {
    final httpresponse = await http.get(
      url,
      headers: {
        'content-type': 'application/json;charset=utf-8',
      },
    );
    final analyze = analyzeAndParseResponse(httpresponse);
    resposta = extractCepValuesFromResponse(analyze);
  } catch (e) {
    final String? message = e is SimpleError
        ? e.message
        : 'Erro ao se conectar com o serviço ViaCEP.';

    throw ServiceError(
      service: Service.ViaCEP,
      message: message,
    );
  }

  return resposta;
}