2727__copyright__ = "Copyright 2022, mundialis GmbH & Co. KG"
2828__maintainer__ = "Anika Weinmann"
2929
30- import json
3130import re
32- import requests
3331
3432from actinia .location import Location
3533from actinia .resources .templating import tplEnv
3634from actinia .resources .logger import log
35+ from actinia .utils import request_and_check
3736
3837
3938class Actinia :
@@ -43,13 +42,16 @@ def __init__(
4342 api_version = "latest" ,
4443 user = None ,
4544 pw = None ,
45+ connect_timeout = None ,
46+ read_timeout = None ,
4647 ):
4748 self .api_prefix = api_version
4849 self .base_url = url
4950 self .headers = {"content-type" : "application/json; charset=utf-8" }
5051 self .user = None
5152 self .__password = None
5253 self .__auth = None
54+ self .timeout = (connect_timeout , read_timeout )
5355 if user and pw :
5456 self .set_authentication (user , pw )
5557 self .locations = dict ()
@@ -67,11 +69,9 @@ def __set_url(self):
6769
6870 def __check_version (self ):
6971 version_url = f"{ self .url } /version"
70- resp = requests .get (version_url )
71- if resp .status_code != 200 :
72- raise Exception ("Connection to actinia server failed!" )
73-
74- data = json .loads (resp .text )
72+ data = request_and_check (
73+ "GET" , version_url , ** {"timeout" : self .timeout }
74+ )
7575
7676 if len (data ) > 2 :
7777 log .debug (f"{ self .url } is working and will be used." )
@@ -102,23 +102,16 @@ def get_version(self):
102102 """
103103
104104 version_url = f"{ self .url } /version"
105- resp = requests .get (version_url )
106- if resp .status_code != 200 :
107- raise Exception ("Connection to actinia server failed!" )
108- data = json .loads (resp .text )
109- return data
105+ return request_and_check (
106+ "GET" , version_url , ** {"timeout" : self .timeout }
107+ )
110108
111109 def __check_auth (self ):
112110 url = f"{ self .url } /locations"
113- resp = requests .get (url , auth = (self .__auth ))
114- if resp .status_code == 401 :
115- raise Exception (
116- "Wrong user or password. Please check your inputs."
117- )
118- elif resp .status_code != 200 :
119- raise Exception (f"Error { resp .status_code } : { resp .text } " )
120- else :
121- log .debug (f"{ self .user } is logged in." )
111+ request_and_check (
112+ "GET" , url , ** {"timeout" : self .timeout , "auth" : (self .__auth )}
113+ )
114+ log .debug (f"{ self .user } is logged in." )
122115
123116 def set_authentication (self , user , pw ):
124117 """
@@ -155,11 +148,9 @@ def __request_locations(self):
155148 raise Exception ("Authentication is not set." )
156149
157150 url = f"{ self .url } /locations"
158- resp = requests .get (url , auth = (self .__auth ))
159- if resp .status_code != 200 :
160- raise Exception (f"Error { resp .status_code } : { resp .text } " )
161-
162- loc_names = json .loads (resp .text )["locations" ]
151+ loc_names = request_and_check (
152+ "GET" , url , ** {"timeout" : self .timeout , "auth" : (self .__auth )}
153+ )["locations" ]
163154 loc = {
164155 lname : Location (lname , self , self .__auth ) for lname in loc_names
165156 }
@@ -178,18 +169,16 @@ def create_location(self, name, epsgcode):
178169 postbody = tpl .render (epsgcode = epsgcode )
179170
180171 url = f"{ self .url } /locations/{ name } "
181- resp = requests .post (
172+ request_and_check (
173+ "POST" ,
182174 url ,
183- auth = (self .__auth ),
184- headers = self .headers ,
185- data = postbody ,
175+ ** {
176+ "timeout" : self .timeout ,
177+ "auth" : (self .__auth ),
178+ "headers" : self .headers ,
179+ "data" : postbody ,
180+ },
186181 )
187- if resp .status_code != 200 :
188- try :
189- msg = json .loads (resp .text )["message" ]
190- except Exception :
191- msg = resp .text
192- raise Exception (f"Error { resp .status_code } : { msg } " )
193182
194183 location = Location (name , self , self .__auth )
195184 if len (self .locations ) == 0 :
0 commit comments