TermoAutorizacaoResponse.fromHeaders constructor

TermoAutorizacaoResponse.fromHeaders(
  1. Map<String, String> headers
)

Cria resposta a partir de headers HTTP (para cache)

Implementation

factory TermoAutorizacaoResponse.fromHeaders(Map<String, String> headers) {
  final status = int.tryParse(headers['status'] ?? '304') ?? 304;
  final etag = headers['etag'] ?? '';
  final expires = headers['expires'];

  String? token;
  DateTime? dataExpiracao;

  // Extrair token do ETag
  if (etag.contains('autenticar_procurador_token:')) {
    final startIndex = etag.indexOf('autenticar_procurador_token:') + 28;
    final endIndex = etag.indexOf('"', startIndex);
    if (endIndex > startIndex) {
      token = etag.substring(startIndex, endIndex);
    }
  }

  // Parsear data de expiração
  if (expires != null) {
    try {
      dataExpiracao = DateTime.parse(expires);
    } catch (e) {
      // Ignorar erro de parsing
    }
  }

  return TermoAutorizacaoResponse(
    status: status,
    mensagens: [
      MensagemNegocio(codigo: 'CACHE', texto: 'Token em cache válido'),
    ],
    autenticarProcuradorToken: token,
    dataExpiracao: dataExpiracao,
    isCacheValido: true,
  );
}