Skip to content
Snippets Groups Projects
Verified Commit eec4699f authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

swh.core.api.SWHRemoteAPI: Permit to set a timeout option

Related T1061
parent 89f329d1
No related branches found
Tags v0.0.40
1 merge request!10swh.core.api.SWHRemoteAPI: Permit to set a timeout option
...@@ -24,17 +24,20 @@ class SWHRemoteAPI: ...@@ -24,17 +24,20 @@ class SWHRemoteAPI:
""" """
def __init__(self, api_exception, url): def __init__(self, api_exception, url, timeout=None):
super().__init__() super().__init__()
self.api_exception = api_exception self.api_exception = api_exception
base_url = url if url.endswith('/') else url + '/' base_url = url if url.endswith('/') else url + '/'
self.url = base_url self.url = base_url
self.session = requests.Session() self.session = requests.Session()
self.timeout = timeout
def _url(self, endpoint): def _url(self, endpoint):
return '%s%s' % (self.url, endpoint) return '%s%s' % (self.url, endpoint)
def raw_post(self, endpoint, data, **opts): def raw_post(self, endpoint, data, **opts):
if self.timeout and 'timeout' not in opts:
opts['timeout'] = self.timeout
try: try:
return self.session.post( return self.session.post(
self._url(endpoint), self._url(endpoint),
...@@ -45,6 +48,8 @@ class SWHRemoteAPI: ...@@ -45,6 +48,8 @@ class SWHRemoteAPI:
raise self.api_exception(e) raise self.api_exception(e)
def raw_get(self, endpoint, params=None, **opts): def raw_get(self, endpoint, params=None, **opts):
if self.timeout and 'timeout' not in opts:
opts['timeout'] = self.timeout
try: try:
return self.session.get( return self.session.get(
self._url(endpoint), self._url(endpoint),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment