Skip to content

Commit

Permalink
[IMP] json() en modelo
Browse files Browse the repository at this point in the history
  • Loading branch information
ovnicraft committed Nov 22, 2017
1 parent cc0c425 commit f30104e
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
27 changes: 27 additions & 0 deletions runa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,30 @@
__author__ = """Cristian Salamea"""
__email__ = '[email protected]'
__version__ = '0.1.0'

# _____ _ _ _ _
# | __ \| | | | \ | | /\
# | |__) | | | | \| | / \
# | _ /| | | | . ` | / /\ \
# | | \ \| |__| | |\ |/ ____ \
# |_| \_\\____/|_| \_/_/ \_\
#


"""
Runa Library
~~~~~~~~~~~~
Runa es una libreria para consumir los servicios web
del bus gubernamental de Ecuador de una manera *Pythonica*
>>> import runa
>>> r = runa.read_by_nui('0102030405')
>>> r.Nombre
PUJON JUAN
>>> print(r)
<Runa [0102030405 PUJON JUAN]>
"""

from .api import read_by_nui
73 changes: 73 additions & 0 deletions runa/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
"""
runa.api
~~~~~~~~
RUNA API
"""
import logging
from urllib.error import URLError

# from suds.client import Client
from zeep import Client
from suds.wsse import Security, UsernameToken, Timestamp

from .utils import WS_CIUDADANO, WS_ACCESS, AUTHORIZED_NUI
from .models import PreparedRuna

logger = logging.getLogger('suds')


def login(WSDL):
try:
client = Client(WSDL)
except URLError:
logger.error("Error en WS")
return False, False

cl_auth = Client(WS_ACCESS)
logger.info("***********\n Leyendo types en servicios...")
req_auth = cl_auth.factory.create("validarPermisoPeticion")
logger.info("***********\n Autentificacion...")
req_auth.Cedula = AUTHORIZED_NUI
req_auth.Urlsw = WS_CIUDADANO
logger.info("***********\nResultado de auth...")

response = cl_auth.service.ValidarPermiso(req_auth)

if response.TienePermiso == 'N':
logger.error("No tiene permisos, respuesta de WS: %s" % response.TienePermiso)
return False
return response, client


def read_by_nui(nui, mode='prod', authorized_nui=None):
response, client = login(WS_CIUDADANO)
if not response:
return False
logger.info("Digest: %s" % response.Digest)
logger.info("Fecha: %s" % response.Fecha)
logger.info("FechaF: %s " % response.FechaF)
logger.info("Nonce: %s " % response.Nonce)
logger.info("TienePermiso: %s " % response.TienePermiso)
security = Security()
token = UsernameToken(authorized_nui)
token.setcreated(response.Fecha)
token.nonce_has_encoding = True
token.setnonce(response.Nonce)
token.setpassworddigest(response.Digest)
security.tokens.append(token)
token_ts = Timestamp()
token_ts.created = response.Fecha
token_ts.expires = response.FechaF
security.tokens.append(token_ts)
client.set_options(wsse=security)
consulta_response = False
try:
consulta_response = client.service.BusquedadPorNui(NUI=nui, Usuario="testroot", Contrasenia="Sti1DigS21")
except WebFault:
logger.info("error en la consulta")
runa = PreparedRuna()
runa.prepare(consulta_response)
return runa
54 changes: 54 additions & 0 deletions runa/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""
runa.models
~~~~~~~~~~~
RUNA Models
"""
import json


class PreparedRuna(object):

def __init__(self):
self.Calle = None
self.CodigoTipoCedulado = None
self.CondicionCedulado = None
self.Conyuge = None
self.Domicilio = None
self.EstadoCivil = None
self.FechaCedulacion = None
self.FechaMatrimonio = None
self.FechaNacimiento = None
self.IndividualDactilar = None
self.Instruccion = None
self.LugarMatrimonio = None
self.LugarNacimiento = None
self.NUI = None
self.Nacionalidad = None
self.Nombre = None
self.NombreMadre = None
self.NombrePadre = None
self.NumeroCasa = None
self.Profesion = None
self.Sexo = None
self.Genero = None
self.FechaInscripcionGenero = None
self.ValidNUI = True

def __repr__(self):
return '<Runa [%s - %s]>' % (self.NUI, self.Nombre)

def json(self):
return json.dumps(self.__dict__)

def nui_is_valid(self):
""" Validate NUI """
pass

def prepare(self, response):
""" Prepare given response data in object"""
self.Calle = response.Calle
self.NUI = response.NUI
self.Nombre = response.Nombre
20 changes: 20 additions & 0 deletions runa/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-

import os
import json

CONFIG_FILE = '/var/tmp/config.json'

# Read config
with open(CONFIG_FILE) as env_file:
VARS_ENV = json.load(env_file)

AUTHORIZED_NUI = os.environ.get('AUTHORIZED_NUI', None)
WS_CIUDADANO_PRODUCTION = VARS_ENV.get('WS_CIUDADANO_PRODUCTION', None)
WS_CIUDADANO_TESTING = VARS_ENV.get('WS_CIUDADANO_TESTING', None)
WS_ACCESS = VARS_ENV.get('WS_ACCESS', None)
WS_CIUDADANO = None


def set_ws(mode='prod'):
WS_CIUDADANO = mode == 'prod' and WS_CIUDADANO_PRODUCTION or WS_CIUDADANO_TESTING # noqa

0 comments on commit f30104e

Please sign in to comment.