Skip to content
Snippets Groups Projects
Commit dca675b8 authored by Alessandro Frigeri's avatar Alessandro Frigeri
Browse files

added timeout exception

parent 51e980d5
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
import requests import requests
import json import json
import urllib.parse import urllib.parse
import urllib3,socket
import logging
from collections import namedtuple from collections import namedtuple
class Mission: class Mission:
...@@ -102,14 +105,25 @@ def _check_resp(resp): ...@@ -102,14 +105,25 @@ def _check_resp(resp):
pass pass
def _get_resp(path): def _get_resp(path):
resp = requests.get(_url( path )) try:
resp = requests.get(_url( path ), timeout=1)
except requests.exceptions.ReadTimeout: # urllib3.exceptions.ReadTimeoutError:
print("no response in timeout time")
logging.warning('no response in timeout time')
sys.exit(0)
except requests.exceptions.ConnectTimeout:
print("no response in timeout time")
logging.warning('no response in timeout time')
sys.exit(0)
_check_resp(resp) _check_resp(resp)
r = resp.json() r = resp.json()
# To be checked with Peng # To be checked with Peng
count = r['count']
if 'results' in r: if 'results' in r:
return r['results'] return count,r['results']
if 'result' in r: if 'result' in r:
return r['result'] return count,r['result']
def get_specimen(sc=None,mn=None,ln=None,sty=None,ste=None): def get_specimen(sc=None,mn=None,ln=None,sty=None,ste=None):
''' '''
...@@ -123,26 +137,26 @@ def get_specimen(sc=None,mn=None,ln=None,sty=None,ste=None): ...@@ -123,26 +137,26 @@ def get_specimen(sc=None,mn=None,ln=None,sty=None,ste=None):
sp_list = [] sp_list = []
if sc: if sc:
for s in sc: for s in sc:
spec = _get_resp('/specimen/'+s) count,spec = _get_resp('/specimen/'+s)
sp_list.extend(spec) sp_list.extend(spec)
if mn: if mn:
for n in mn: for n in mn:
spec = _get_resp('/specimenlist/mission/'+n) count,spec = _get_resp('/specimenlist/mission/'+n)
sp_list.extend(spec) sp_list.extend(spec)
if ln: if ln:
for n in ln: for n in ln:
spec = _get_resp('/specimenlist/mission/'+n) count,spec = _get_resp('/specimenlist/mission/'+n)
sp_list.extend(spec) sp_list.extend(spec)
if sty: if sty:
for st in sty: for st in sty:
spec = _get_resp('/specimenlist/mission/'+st) count,spec = _get_resp('/specimenlist/mission/'+st)
sp_list.extend(spec) sp_list.extend(spec)
if ste: if ste:
for st in ste: for st in ste:
spec = _get_resp('/specimenlist/mission/'+st) count,spec = _get_resp('/specimenlist/mission/'+st)
sp_list.extend(spec) sp_list.extend(spec)
from collections import namedtuple from collections import namedtuple
...@@ -180,7 +194,7 @@ def get_missions(): ...@@ -180,7 +194,7 @@ def get_missions():
def get_specimentypes(): def get_specimentypes():
st_list = [] st_list = []
st = _get_resp('/cv/specimentypes') count,st = _get_resp('/cv/specimentypes')
for s in st: for s in st:
stobj = SpecimenType(s['name']) stobj = SpecimenType(s['name'])
st_list.append(stobj) st_list.append(stobj)
...@@ -188,7 +202,7 @@ def get_specimentypes(): ...@@ -188,7 +202,7 @@ def get_specimentypes():
def get_samplingtechniques(): def get_samplingtechniques():
st_list = [] st_list = []
st = _get_resp('/cv/samplingtechniques') count,st = _get_resp('/cv/samplingtechniques')
for s in st: for s in st:
stobj = SamplingTechnique(s['name']) stobj = SamplingTechnique(s['name'])
st_list.append(stobj) st_list.append(stobj)
...@@ -196,7 +210,7 @@ def get_samplingtechniques(): ...@@ -196,7 +210,7 @@ def get_samplingtechniques():
def get_analyzedmaterials(): def get_analyzedmaterials():
st_list = [] st_list = []
st = _get_resp('/cv/analyzedmaterials') count,st = _get_resp('/cv/analyzedmaterials')
for s in st: for s in st:
stobj = AnalyzedMaterial(s['name']) stobj = AnalyzedMaterial(s['name'])
st_list.append(stobj) st_list.append(stobj)
...@@ -204,14 +218,14 @@ def get_analyzedmaterials(): ...@@ -204,14 +218,14 @@ def get_analyzedmaterials():
def get_analytes(): def get_analytes():
analytes = [] analytes = []
an = _get_resp('/cv/analyzedmaterials') count,an = _get_resp('/cv/analyzedmaterials')
for a in an: for a in an:
analytes.append( Analyte(m_item['name'] )) analytes.append( Analyte(m_item['name'] ))
return analytes return analytes
def get_analysismethods(): def get_analysismethods():
am_list = [] am_list = []
am = _get_resp('/cv/analysismethods') count,am = _get_resp('/cv/analysismethods')
for a in am: for a in am:
aobj = AnalysisMethod(s['name']) aobj = AnalysisMethod(s['name'])
am_list.append(aobj) am_list.append(aobj)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment