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