Skip to content

Commit

Permalink
v0.9.2: add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix committed May 10, 2020
1 parent e32c2eb commit 815cff2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion wandbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = 'srz_zumix'
__version__ = '0.9.1'
__version__ = '0.9.2'

__copyright__ = '2014-2020 %s ' % __author__
__license__ = """
Expand Down
20 changes: 15 additions & 5 deletions wandbox/wandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from time import sleep
from requests.exceptions import HTTPError as RHTTPError
from requests.exceptions import ConnectionError as RConnectionError
from requests.exceptions import ConnectTimeout as RConnectTimeout

#
#
Expand All @@ -21,6 +22,7 @@ class Wandbox:

#api_url = 'http://melpon.org/wandbox/api'
api_url = 'https://wandbox.org/api'
timeout_ = (3.0, 60.0 * 5)

def __init__(self):
self.reset()
Expand All @@ -37,7 +39,7 @@ def GetCompilerList():
"""
get compiler list
"""
response = requests.get(Wandbox.api_url + '/list.json')
response = requests.get(Wandbox.api_url + '/list.json', timeout=3.0)
response.raise_for_status()
return response.json()

Expand All @@ -53,10 +55,18 @@ def GetPermlink(link):
"""
get wandbox permanet link
"""
response = requests.get(Wandbox.api_url + '/permlink/' + link)
response = requests.get(Wandbox.api_url + '/permlink/' + link, timeout=3.0)
response.raise_for_status()
return response.json()

@property
def timeout(self):
return self.timeout_

@timeout.setter
def timeout(self, v):
self.timeout_ = v

def get_permlink(self, link):
"""
get wandbox permanet link
Expand All @@ -70,7 +80,7 @@ def run(self):
"""
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
payload = json.dumps(self.parameter)
response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers)
response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers, timeout=self.timeout_)
response.raise_for_status()
try:
return response.json()
Expand Down Expand Up @@ -156,14 +166,14 @@ def reset(self):
def Call(action, retries, retry_wait):
try:
return action()
except (RHTTPError, RConnectionError) as e:
except (RHTTPError, RConnectionError, RConnectTimeout) as e:

def is_retry(e):
if e is None:
return False
if e.response is None:
return False
return e.response.status_code in [504]
return e.response.status_code in [500, 502, 503, 504]

retries -= 1
if is_retry(e) and retries > 0:
Expand Down

0 comments on commit 815cff2

Please sign in to comment.