Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
certs/*
.idea/*
16 changes: 11 additions & 5 deletions face/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,30 @@ def __init__(self, **kwargs):

# Handle environment, df "prod"
self.environment = "prod"
self.destination = kwargs.get('destination', 'FACE')
if self.destination == 'FACE':
destination = FACE_ENVS
else:
raise Exception("Environment isn't correct. It must be FACE")
if 'environment' in kwargs:
assert type(kwargs['environment']) == str, "environment argument must be an string"
assert kwargs['environment'] in FACE_ENVS.keys(), "Provided environment '{}' not recognized in defined FACE_ENVS {}".format(kwargs['environment'], str(FACE_ENVS.keys()))
assert kwargs['environment'] in destination.keys(), "Provided environment '{}' not recognized in defined destination {}".format(kwargs['environment'], str(destination.keys()))
self.environment = kwargs['environment']

self.result_obj = kwargs.get('result_obj', False)

# Inject updated CA root certs for the transport layer
updated_session = Session()
updated_session.verify = certifi.where()
transport = zeep.transports.Transport(session=updated_session)
# initialize a ZEEP client with the desired FACe envs
self.client = zeep.Client(
FACE_ENVS[self.environment],
destination[self.environment],
plugins=[FACe_signer(self.certificate, debug=self.debug)],
transport=transport
)

# Initialitze specific services handlers
self.invoices = Invoice(service=self.client.service, email=self.email)
self.nifs = NIF(service=self.client.service)
self.administrations = Administration(service=self.client.service)
self.invoices = Invoice(service=self.client.service, email=self.email, result_obj=self.result_obj)
self.nifs = NIF(service=self.client.service, result_obj=self.result_obj)
self.administrations = Administration(service=self.client.service, result_obj=self.result_obj)
4 changes: 4 additions & 0 deletions face/services/administration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Administration(SOAP_Service):
Integrate all NIF-related methods
"""

def __init__(self, service, result_obj=False):
super(Administration, self).__init__(service)
self.result_obj = result_obj

def list(self):
"""
List administrations
Expand Down
36 changes: 27 additions & 9 deletions face/services/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class Invoice(SOAP_Service):
"""
Integrate all invoice-related methods
"""
def __init__(self, service, email):
def __init__(self, service, email, result_obj=False):
super(Invoice, self).__init__(service)
self.email = email
self.result_obj = result_obj

def fetch(self, invoice):
"""
Expand All @@ -21,20 +22,28 @@ def fetch(self, invoice):
assert type(invoice) in [int, str], "Invoice must be the registry number of the sended invoice."
call_result = self.serialize(self.service.consultarFactura(numeroRegistro=str(invoice)))
schema = InvoiceSchema()
return schema.load(call_result)
if self.result_obj:
return schema.load(call_result)
else:
return call_result

def send(self, invoice, attachments=None):
def send_by_filename(self, invoice, attachments=None):
assert type(invoice) == str, "Invoice must be the filename of the invoice to deliver"
invoice_content = base64.b64encode(open(invoice).read())
invoice_filename = os.path.basename(invoice)
return self.send(invoice_filename, invoice_content, attachments=attachments)

def send(self, invoice_filename, invoice_content, attachments=None):
"""
Send an invoice with optional attachments and return the delivery result

It prepares the payload wanted for the `enviarFactura` webservice with a base64 invoice and their filename
"""
assert type(invoice) == str, "Invoice must be the filename of the invoice to deliver"
the_invoice = {
"correo": self.email,
"factura": {
"factura": base64.b64encode(open(invoice).read()),
"nombre": os.path.basename(invoice),
"factura": invoice_content,
"nombre": invoice_filename,
"mime": "application/xml",
}
}
Expand All @@ -43,7 +52,10 @@ def send(self, invoice, attachments=None):

call_result = self.serialize(self.service.enviarFactura(the_invoice))
schema = InvoiceSchema()
return schema.load(call_result)
if self.result_obj:
return schema.load(call_result)
else:
return call_result

def cancel(self, invoice, reason):
"""
Expand All @@ -61,7 +73,10 @@ def cancel(self, invoice, reason):

call_result = self.serialize(self.service.anularFactura(**the_invoice))
schema = InvoiceSchema()
return schema.load(call_result)
if self.result_obj:
return schema.load(call_result)
else:
return call_result

def list_states(self):
"""
Expand All @@ -74,4 +89,7 @@ def list_states(self):

call_result = self.serialize(self.service.consultarEstados())
schema = StatusesSchema()
return schema.load(call_result)
if self.result_obj:
return schema.load(call_result)
else:
return call_result
9 changes: 8 additions & 1 deletion face/services/nif.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class NIF(SOAP_Service):
Integrate all NIF-related methods
"""

def __init__(self, service, result_obj=False):
super(NIF, self).__init__(service)
self.result_obj = result_obj

def list(self):
"""
List NIFs method.
Expand All @@ -16,4 +20,7 @@ def list(self):
call_result = self.serialize(self.service.consultarNIFs())

schema = ResponseSchema()
return schema.load(call_result)
if self.result_obj:
return schema.load(call_result)
else:
return call_result