Add timeout configuration
This commit is contained in:
parent
ee6e6acfb3
commit
122d46f591
18
seur/api.py
18
seur/api.py
|
@ -3,6 +3,7 @@
|
|||
|
||||
from xml.dom.minidom import parseString
|
||||
import urllib2
|
||||
import socket
|
||||
import os
|
||||
import genshi
|
||||
import genshi.template
|
||||
|
@ -25,10 +26,12 @@ class API(object):
|
|||
'seurid',
|
||||
'ci',
|
||||
'ccc',
|
||||
'timeout',
|
||||
'context',
|
||||
)
|
||||
|
||||
def __init__(self, username, password, vat, franchise, seurid, ci, ccc, context={}):
|
||||
def __init__(self, username, password, vat, franchise, seurid, ci, ccc,
|
||||
timeout=None, context={}):
|
||||
"""
|
||||
This is the Base API class which other APIs have to subclass. By
|
||||
default the inherited classes also get the properties of this
|
||||
|
@ -48,6 +51,7 @@ class API(object):
|
|||
:param seurid: identification description
|
||||
:param ci: franchise code
|
||||
:param ccc: identification description
|
||||
:param timeout: int number of seconds to lost connection
|
||||
"""
|
||||
self.username = username
|
||||
self.password = password
|
||||
|
@ -56,6 +60,7 @@ class API(object):
|
|||
self.seurid = seurid
|
||||
self.ci = ci
|
||||
self.ccc = ccc
|
||||
self.timeout = timeout
|
||||
self.context = context
|
||||
|
||||
def __enter__(self):
|
||||
|
@ -75,8 +80,13 @@ class API(object):
|
|||
"""
|
||||
headers={}
|
||||
request = urllib2.Request(url, xml, headers)
|
||||
response = urllib2.urlopen(request)
|
||||
return response.read()
|
||||
try:
|
||||
response = urllib2.urlopen(request, timeout=self.timeout)
|
||||
return response.read()
|
||||
except socket.timeout as err:
|
||||
return
|
||||
except socket.error as err:
|
||||
return
|
||||
|
||||
def test_connection(self):
|
||||
"""
|
||||
|
@ -96,6 +106,8 @@ class API(object):
|
|||
url = 'http://cit.seur.com/CIT-war/services/ImprimirECBWebService'
|
||||
xml = tmpl.generate(**vals).render()
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return 'timed out'
|
||||
dom = parseString(result)
|
||||
|
||||
#Get message connection
|
||||
|
|
|
@ -83,8 +83,10 @@ class Picking(API):
|
|||
|
||||
url = 'http://cit.seur.com/CIT-war/services/ImprimirECBWebService'
|
||||
xml = tmpl.generate(**vals).render()
|
||||
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return reference, label, 'timed out'
|
||||
|
||||
dom = parseString(result)
|
||||
|
||||
#Get message error from XML
|
||||
|
@ -133,6 +135,9 @@ class Picking(API):
|
|||
url = 'https://ws.seur.com/webseur/services/WSConsultaExpediciones'
|
||||
xml = tmpl.generate(**vals).render()
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return
|
||||
|
||||
dom = parseString(result)
|
||||
|
||||
#Get info
|
||||
|
@ -165,6 +170,9 @@ class Picking(API):
|
|||
url = 'https://ws.seur.com/webseur/services/WSConsultaExpediciones'
|
||||
xml = tmpl.generate(**vals).render()
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return
|
||||
|
||||
dom = parseString(result)
|
||||
|
||||
#Get list
|
||||
|
@ -227,6 +235,9 @@ class Picking(API):
|
|||
xml = tmpl.generate(**vals).render()
|
||||
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return
|
||||
|
||||
dom = parseString(result)
|
||||
|
||||
if self.context.get('pdf'):
|
||||
|
@ -237,8 +248,7 @@ class Picking(API):
|
|||
traza = dom.getElementsByTagName('traza')
|
||||
if traza:
|
||||
return traza[0].firstChild.data
|
||||
|
||||
return None
|
||||
return
|
||||
|
||||
def manifiesto(self, data):
|
||||
"""
|
||||
|
@ -268,12 +278,13 @@ class Picking(API):
|
|||
xml = tmpl.generate(**vals).render()
|
||||
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return
|
||||
|
||||
dom = parseString(result)
|
||||
|
||||
pdf = dom.getElementsByTagName('ns1:out')
|
||||
if pdf:
|
||||
return pdf[0].firstChild.data
|
||||
return
|
||||
return pdf[0].firstChild.data if pdf else None
|
||||
|
||||
def city(self, city):
|
||||
"""
|
||||
|
@ -293,6 +304,8 @@ class Picking(API):
|
|||
url = 'https://ws.seur.com/WSEcatalogoPublicos/servlet/XFireServlet/WSServiciosWebPublicos'
|
||||
xml = tmpl.generate(**vals).render()
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return []
|
||||
|
||||
dom = parseString(result)
|
||||
info = dom.getElementsByTagName('out')
|
||||
|
@ -331,6 +344,8 @@ class Picking(API):
|
|||
url = 'https://ws.seur.com/WSEcatalogoPublicos/servlet/XFireServlet/WSServiciosWebPublicos'
|
||||
xml = tmpl.generate(**vals).render()
|
||||
result = self.connect(url, xml)
|
||||
if not result:
|
||||
return []
|
||||
|
||||
dom = parseString(result)
|
||||
info = dom.getElementsByTagName('ns1:out')
|
||||
|
|
22
test.py
22
test.py
|
@ -1,12 +1,12 @@
|
|||
username = ''
|
||||
password = ''
|
||||
username_expedicion = ''
|
||||
password_expedicion = ''
|
||||
vat = '' #VAT company
|
||||
franchise = '' # Franchise code
|
||||
seurid = '' #Description ID Seur
|
||||
ci = '' #Customer Code Seur
|
||||
ccc = '' #Account Code Seur
|
||||
username_expedicion = '' # list and info services
|
||||
password_expedicion = '' # list and info services
|
||||
|
||||
context = {}
|
||||
context['printer'] = 'ZEBRA'
|
||||
|
@ -21,11 +21,11 @@ print "Seur services"
|
|||
services = services()
|
||||
print services
|
||||
|
||||
with API(username, password, vat, franchise, seurid, ci, ccc, context) as seur_api:
|
||||
with API(username, password, vat, franchise, seurid, ci, ccc, context=context) as seur_api:
|
||||
print "Test connection"
|
||||
print seur_api.test_connection()
|
||||
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
print "Send a new shipment - Label ECB"
|
||||
|
||||
data = {}
|
||||
|
@ -40,7 +40,7 @@ with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as pi
|
|||
#~ data['clave_reembolso'] = '' # Add F to invoice
|
||||
#~ data['valor_reembolso'] = ''
|
||||
data['cliente_nombre'] = 'Zikzakmedia SL'
|
||||
data['cliente_direccion'] = 'Docror Fleming, 28. Baixos'
|
||||
data['cliente_direccion'] = 'Doctor Fleming, 28. Baixos'
|
||||
#~ data['cliente_tipovia'] = 'CL'
|
||||
#~ data['cliente_tnumvia'] = 'N'
|
||||
#~ data['cliente_numvia'] = '93'
|
||||
|
@ -66,7 +66,7 @@ with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as pi
|
|||
print "Generated label in /tmp/seur-label.txt"
|
||||
|
||||
context['pdf'] = True
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
print "Send a new shipment - Label PDF"
|
||||
reference, label, error = picking_api.create(data)
|
||||
|
||||
|
@ -78,7 +78,7 @@ with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as pi
|
|||
f.write(decodestring(label))
|
||||
print "Generated PDF label in /tmp/seur-label.pdf"
|
||||
|
||||
with Picking(username_expedicion, password_expedicion, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username_expedicion, password_expedicion, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
print "Get info picking"
|
||||
data = {}
|
||||
|
||||
|
@ -90,7 +90,7 @@ with Picking(username_expedicion, password_expedicion, vat, franchise, seurid, c
|
|||
info = picking_api.info(data)
|
||||
print info
|
||||
|
||||
with Picking(username_expedicion, password_expedicion, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username_expedicion, password_expedicion, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
|
||||
print "Get list picking"
|
||||
data = {}
|
||||
|
@ -102,7 +102,7 @@ with Picking(username_expedicion, password_expedicion, vat, franchise, seurid, c
|
|||
print info
|
||||
|
||||
context['pdf'] = True
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
print "Get Label PDF"
|
||||
|
||||
data = {}
|
||||
|
@ -136,7 +136,7 @@ with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as pi
|
|||
f.write(decodestring(label))
|
||||
print "Generated PDF label in /tmp/seur-label.pdf"
|
||||
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
print "Get Manifiesto"
|
||||
|
||||
data = {}
|
||||
|
@ -146,7 +146,7 @@ with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as pi
|
|||
f.write(decodestring(manifiesto))
|
||||
print "Generated PDF label in /tmp/seur-manifiesto.pdf"
|
||||
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context) as picking_api:
|
||||
with Picking(username, password, vat, franchise, seurid, ci, ccc, context=context) as picking_api:
|
||||
print "Get values from Seur about city or zip"
|
||||
|
||||
city = 'Granollers'
|
||||
|
|
Loading…
Reference in New Issue