Commit dca675b8 authored by Alessandro Frigeri's avatar Alessandro Frigeri
Browse files

added timeout exception

parent 51e980d5
Loading
Loading
Loading
Loading
+27 −13
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@
import requests
import json 
import urllib.parse
import urllib3,socket
import logging

from collections import namedtuple

class Mission:
@@ -102,14 +105,25 @@ def _check_resp(resp):
      pass

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)
   r = resp.json()
   # To be checked with Peng
   count = r['count']
   if 'results' in r:
      return r['results']
      return count,r['results']
   if 'result' in r:
      return r['result']
      return count,r['result']
   

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 = []
   if sc:
      for s in sc: 
         spec = _get_resp('/specimen/'+s) 
         count,spec = _get_resp('/specimen/'+s) 
         sp_list.extend(spec)
   if mn:
      for n in mn:
         spec = _get_resp('/specimenlist/mission/'+n) 
         count,spec = _get_resp('/specimenlist/mission/'+n) 
         sp_list.extend(spec)

   if ln:
      for n in ln:
         spec = _get_resp('/specimenlist/mission/'+n) 
         count,spec = _get_resp('/specimenlist/mission/'+n) 
         sp_list.extend(spec)

   if sty:
      for st in sty:
         spec = _get_resp('/specimenlist/mission/'+st) 
         count,spec = _get_resp('/specimenlist/mission/'+st) 
         sp_list.extend(spec)

   if ste:
      for st in ste:
         spec = _get_resp('/specimenlist/mission/'+st) 
         count,spec = _get_resp('/specimenlist/mission/'+st) 
         sp_list.extend(spec)

   from collections import namedtuple
@@ -180,7 +194,7 @@ def get_missions():

def get_specimentypes():
   st_list = []
   st = _get_resp('/cv/specimentypes') 
   count,st = _get_resp('/cv/specimentypes') 
   for s in st:
      stobj = SpecimenType(s['name'])
      st_list.append(stobj)
@@ -188,7 +202,7 @@ def get_specimentypes():

def get_samplingtechniques():
   st_list = []
   st = _get_resp('/cv/samplingtechniques') 
   count,st = _get_resp('/cv/samplingtechniques') 
   for s in st:
      stobj = SamplingTechnique(s['name'])
      st_list.append(stobj)
@@ -196,7 +210,7 @@ def get_samplingtechniques():

def get_analyzedmaterials():
   st_list = []
   st = _get_resp('/cv/analyzedmaterials') 
   count,st = _get_resp('/cv/analyzedmaterials') 
   for s in st:
      stobj = AnalyzedMaterial(s['name'])
      st_list.append(stobj)
@@ -204,14 +218,14 @@ def get_analyzedmaterials():

def get_analytes():
   analytes = []
   an = _get_resp('/cv/analyzedmaterials') 
   count,an = _get_resp('/cv/analyzedmaterials') 
   for a in an:
      analytes.append( Analyte(m_item['name'] ))
   return analytes

def get_analysismethods():
   am_list = []
   am = _get_resp('/cv/analysismethods') 
   count,am = _get_resp('/cv/analysismethods') 
   for a in am:
      aobj = AnalysisMethod(s['name'])
      am_list.append(aobj)