diff --git a/source/conf.py.example b/source/conf.py.example index 0d6c94a..7c4f2d8 100644 --- a/source/conf.py.example +++ b/source/conf.py.example @@ -6,3 +6,6 @@ RUTA_FIEL = '' # ~ Nombre predeterminado de los archivos FIEL NOMBRE_FIEL = 'fiel' + + +TIMEOUT = 10 diff --git a/source/main.py b/source/main.py index 72f88a3..c337c3f 100644 --- a/source/main.py +++ b/source/main.py @@ -14,11 +14,11 @@ class JSONTranslator(): class AppApi(object): - def on_get(self, req, resp, rfc, cfdi): - resp.context['result'] = get_uuid(rfc, cfdi) + def on_get(self, req, resp, rfc, tipo, cfdi): + resp.context['result'] = get_uuid(rfc, tipo, cfdi) resp.status = falcon.HTTP_200 app = falcon.App(middleware=[JSONTranslator()]) api = AppApi() -app.add_route('/api/{rfc}/{cfdi}', api) +app.add_route('/api/{rfc}/{tipo}/{cfdi}', api) diff --git a/source/sat/portal_sat.py b/source/sat/portal_sat.py index cc2d34d..7a3af28 100644 --- a/source/sat/portal_sat.py +++ b/source/sat/portal_sat.py @@ -22,6 +22,8 @@ import requests from requests import Session, exceptions, adapters requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL' +from conf import TIMEOUT + LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s' LOG_DATE = '%d/%m/%Y %H:%M:%S' @@ -32,7 +34,6 @@ logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=LOG_DATE) log = logging.getLogger(__name__) -TIMEOUT = 10 VERIFY_CERT = True @@ -451,6 +452,7 @@ class PortalSAT(object): if not result: msg = 'Error al identificarse en el SAT' + self.error = msg log.error(msg) return False data = self._read_form(result) @@ -529,20 +531,16 @@ class PortalSAT(object): return xml - def get_uuid(self, cfdi_uuid): + def get_uuid(self, tipo, cfdi_uuid): data = {'error': '', 'xml': ''} msg = f'Buscando UUID: {cfdi_uuid}' log.debug(msg) - # ~ Recibidos - filters = self._get_filters(cfdi_uuid, False) - data['xml'] = self._search_by_uuid(filters) - - if data['xml']: - return data - - # ~ Emitidos - filters = self._get_filters(cfdi_uuid, True) - data['xml'] = self._search_by_uuid(filters) + if tipo == 'r': + filters = self._get_filters(cfdi_uuid, False) + data['xml'] = self._search_by_uuid(filters) + elif tipo == 'e': + filters = self._get_filters(cfdi_uuid, True) + data['xml'] = self._search_by_uuid(filters) return data diff --git a/source/sat/util.py b/source/sat/util.py index 6cb7e78..74589a0 100644 --- a/source/sat/util.py +++ b/source/sat/util.py @@ -49,13 +49,17 @@ def validate_fiel(rfc): return cert, error -def get_uuid(rfc, cfdi_uuid): +def get_uuid(rfc, tipo, cfdi_uuid): data = {'error': '', 'xml': ''} if not validate_uuid(cfdi_uuid): data['error'] = 'UUID inválido' return data + if not tipo.lower() in ('e', 'r'): + data['error'] = 'Tipo inválido, debe ser e o r' + return data + cert, error = validate_fiel(rfc) if not cert: data['error'] = error @@ -68,10 +72,10 @@ def get_uuid(rfc, cfdi_uuid): if not sat.is_connect: sat.logout() data['error'] = sat.error - log.eror(sat.error) + log.error(sat.error) return data - data = sat.get_uuid(cfdi_uuid) + data = sat.get_uuid(tipo, cfdi_uuid) sat.logout() except Exception as e: log.error(e)