diff --git a/cep/exc.py b/cep/exc.py new file mode 100644 index 0000000..5e0ec9c --- /dev/null +++ b/cep/exc.py @@ -0,0 +1,12 @@ +class CepError(Exception): + """ + Error interno del sitio web + https://www.banxico.org.mx/cep/ + """ + + +class MaxRequestError(CepError): + """ + Máximo número de peticiones alcanzadas para + obtener el CEP de una transferencia + """ diff --git a/cep/transferencia.py b/cep/transferencia.py index cd3f469..c3861e4 100644 --- a/cep/transferencia.py +++ b/cep/transferencia.py @@ -4,9 +4,16 @@ import clabe from lxml import etree +from requests import HTTPError from .client import Client from .cuenta import Cuenta +from .exc import CepError, MaxRequestError + +MAX_REQUEST_ERROR_MESSAGE = ( + b'Lo sentimos, pero ha excedido el número máximo ' + b'de consultas en este portal' +) @dataclass @@ -36,7 +43,15 @@ def validar( ) if not client: return None - xml = cls._descargar(client, 'XML') + + try: + xml = cls._descargar(client, 'XML') + except HTTPError as exc: + raise CepError from exc + + if MAX_REQUEST_ERROR_MESSAGE in xml: + raise MaxRequestError + resp = etree.fromstring(xml) ordenante = Cuenta.from_etree(resp.find('Ordenante')) diff --git a/cep/version.py b/cep/version.py index 4c9f88d..7fd229a 100644 --- a/cep/version.py +++ b/cep/version.py @@ -1 +1 @@ -__version__ = '0.2.0.dev0' +__version__ = '0.2.0' diff --git a/tests/cassettes/test_lanza_cep_error_para_errores_500.yaml b/tests/cassettes/test_lanza_cep_error_para_errores_500.yaml new file mode 100644 index 0000000..1bdb0fd --- /dev/null +++ b/tests/cassettes/test_lanza_cep_error_para_errores_500.yaml @@ -0,0 +1,957 @@ +interactions: +- request: + body: tipoCriterio=T&receptorParticipante=0&captcha=c&tipoConsulta=1&fecha=19-04-2022&criterio=CUENCA927820173168&emisor=90646&receptor=40012&cuenta=012180000&monto=0.01 + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '172' + Content-Type: + - application/x-www-form-urlencoded + User-Agent: + - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, + like Gecko) Chrome/75.0.3770.142 Safari/537.36 + method: POST + uri: http://www.banxico.org.mx/cep/valida.do + response: + body: + string: '' + headers: + Connection: + - Keep-Alive + Content-Length: + - '0' + Date: + - Tue, 19 Apr 2022 21:17:33 GMT + Location: + - https://www.banxico.org.mx/cep/valida.do + Server: + - mxbanxico + status: + code: 307 + message: Temporary Redirect +- request: + body: tipoCriterio=T&receptorParticipante=0&captcha=c&tipoConsulta=1&fecha=19-04-2022&criterio=CUENCA927820173168&emisor=90646&receptor=40012&cuenta=012180000&monto=0.01 + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '172' + Content-Type: + - application/x-www-form-urlencoded + User-Agent: + - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, + like Gecko) Chrome/75.0.3770.142 Safari/537.36 + method: POST + uri: https://www.banxico.org.mx/cep/valida.do + response: + body: + string: "\n
\r\n La imagen de seguridad no fue\ + \ ingresada correctamente. Favor de validar.\r\n
\r\n+ + Error 500--Internal Server + Error+ + |
From RFC 2068 Hypertext + Transfer Protocol -- HTTP/1.1:+ +10.5.1 500 Internal Server Error+ +The server encountered an unexpected condition + which prevented it from fulfilling the request. + + |