Added timeout to api_client.py

pull/94/head
Piero Toffanin 2017-01-29 16:17:05 -05:00
rodzic 8939ba9b00
commit fa843be5fc
1 zmienionych plików z 11 dodań i 9 usunięć

Wyświetl plik

@ -8,6 +8,7 @@ import json
import os
from urllib.parse import urlunparse
TIMEOUT = 10
class ApiClient:
def __init__(self, host, port):
@ -21,28 +22,28 @@ class ApiClient:
return urlunparse(('http', netloc, url, '', '', ''))
def info(self):
return requests.get(self.url('/info')).json()
return requests.get(self.url('/info'), timeout=TIMEOUT).json()
def options(self):
return requests.get(self.url('/options')).json()
return requests.get(self.url('/options'), timeout=TIMEOUT).json()
def task_info(self, uuid):
return requests.get(self.url('/task/{}/info').format(uuid)).json()
return requests.get(self.url('/task/{}/info').format(uuid), timeout=TIMEOUT).json()
def task_output(self, uuid, line = 0):
return requests.get(self.url('/task/{}/output?line={}').format(uuid, line)).json()
return requests.get(self.url('/task/{}/output?line={}').format(uuid, line), timeout=TIMEOUT).json()
def task_cancel(self, uuid):
return requests.post(self.url('/task/cancel'), data={'uuid': uuid}).json()
return requests.post(self.url('/task/cancel'), data={'uuid': uuid}, timeout=TIMEOUT).json()
def task_remove(self, uuid):
return requests.post(self.url('/task/remove'), data={'uuid': uuid}).json()
return requests.post(self.url('/task/remove'), data={'uuid': uuid}, timeout=TIMEOUT).json()
def task_restart(self, uuid):
return requests.post(self.url('/task/restart'), data={'uuid': uuid}).json()
return requests.post(self.url('/task/restart'), data={'uuid': uuid}, timeout=TIMEOUT).json()
def task_download(self, uuid, asset):
res = requests.get(self.url('/task/{}/download/{}').format(uuid, asset), stream=True)
res = requests.get(self.url('/task/{}/download/{}').format(uuid, asset), stream=True, timeout=TIMEOUT)
if "Content-Type" in res.headers and "application/json" in res.headers['Content-Type']:
return res.json()
else:
@ -61,4 +62,5 @@ class ApiClient:
) for image in images]
return requests.post(self.url("/task/new"),
files=files,
data={'name': name, 'options': json.dumps(options)}).json()
data={'name': name, 'options': json.dumps(options)},
timeout=TIMEOUT).json()