fetchCorreiosService function

Future<Cep> fetchCorreiosService(
  1. String cepWithLeftPad
)

Implementation

Future<Cep> fetchCorreiosService(String cepWithLeftPad) async {
  final url = Uri.parse(
      'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente');

  late Cep resposta;

  final httpresponse = await http.post(
    url,
    headers: {
      'Content-Type': 'text/xml;charset=UTF-8',
      'cache-control': 'no-cache',
    },
    body:
        '<?xml version="1.0"?>\n<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://cliente.bean.master.sigep.bsb.correios.com.br/">\n  <soapenv:Header />\n  <soapenv:Body>\n    <cli:consultaCEP>\n      <cep>$cepWithLeftPad</cep>\n    </cli:consultaCEP>\n  </soapenv:Body>\n</soapenv:Envelope>',
  );

  try {
    final analyze = analyzeAndParseResponse(httpresponse);

    resposta = extractValuesFromSuccessResponse(analyze);
  } catch (e) {
    final String? message = e is SimpleError
        ? e.message
        : 'Erro ao se conectar com o serviço Correios.';

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

  return resposta;
}