|
1 | 1 | from contextlib import suppress
|
2 | 2 |
|
3 | 3 | from lxml import etree
|
| 4 | +import gzip |
| 5 | +import base64 |
4 | 6 |
|
5 | 7 | from erpbrasil.edoc.edoc import DocumentoEletronico
|
| 8 | +from erpbrasil.transmissao import TransmissaoSOAP |
6 | 9 |
|
7 | 10 | from .resposta import analisar_retorno_raw
|
8 | 11 |
|
@@ -223,13 +226,15 @@ def consulta_documento(self, chave):
|
223 | 226 |
|
224 | 227 | def envia_documento(self, edoc):
|
225 | 228 | xml_assinado = self.assina_raiz(edoc, edoc.infCte.Id)
|
226 |
| - raiz = Tcte(infCte=edoc, signature=None) |
227 |
| - xml_envio_string = raiz.to_xml() |
228 |
| - xml_envio_bytes = xml_envio_string.encode('utf-8') |
229 |
| - xml_envio_etree = etree.fromstring(xml_envio_bytes) |
230 |
| - xml_envio_etree.append(etree.fromstring(xml_assinado)) |
| 229 | + |
| 230 | + # Compactar o XML assinado com GZip |
| 231 | + gzipped_xml = gzip.compress(xml_assinado.encode('utf-8')) |
| 232 | + |
| 233 | + # Codificar o XML compactado em Base64 |
| 234 | + base64_gzipped_xml = base64.b64encode(gzipped_xml).decode('utf-8') |
| 235 | + |
231 | 236 | return self._post(
|
232 |
| - raiz=xml_envio_etree, |
| 237 | + raiz=base64_gzipped_xml, |
233 | 238 | url=self._search_url("CTeRecepcaoSincV4"),
|
234 | 239 | operacao="cteRecepcao",
|
235 | 240 | classe=RetCte,
|
@@ -286,12 +291,19 @@ def _search_url(self, service):
|
286 | 291 | def consulta_recibo(self):
|
287 | 292 | pass
|
288 | 293 |
|
289 |
| - def _post(self, raiz, url, operacao, classe): |
290 |
| - xml_string = raiz.to_xml() |
291 |
| - xml_etree = xml_string.from_xml() |
292 |
| - with self._transmissao.cliente(url): |
293 |
| - retorno = self._transmissao.enviar(operacao, xml_etree) |
294 |
| - return analisar_retorno_raw(operacao, raiz, xml_string, retorno, classe) |
295 |
| - |
296 | 294 | def get_documento_id(self, edoc):
|
297 | 295 | return edoc.infCte.Id[:3], edoc.infCte.Id[3:]
|
| 296 | + |
| 297 | + |
| 298 | +class TransmissaoCTE(TransmissaoSOAP): |
| 299 | + |
| 300 | + def interpretar_mensagem(self, mensagem, **kwargs): |
| 301 | + if isinstance(mensagem, str): |
| 302 | + try: |
| 303 | + return etree.fromstring(mensagem, parser=etree.XMLParser( |
| 304 | + remove_blank_text=True |
| 305 | + )) |
| 306 | + except (etree.XMLSyntaxError, ValueError): |
| 307 | + # Retorna a string original se houver um erro na conversão |
| 308 | + return mensagem |
| 309 | + return mensagem |
0 commit comments