Skip to content

Commit

Permalink
kernelci.api: use HTTP request timeout config value
Browse files Browse the repository at this point in the history
Use the new timeout config value in all requests to avoid getting
stuck if the server never replies.

Signed-off-by: Guillaume Tucker <[email protected]>
  • Loading branch information
gctucker committed Jul 13, 2023
1 parent 695ab6f commit d8a53f6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions kernelci/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, config: kernelci.config.api.API, token: str):
self._headers = {'Content-Type': 'application/json'}
if self._token:
self._headers['Authorization'] = f'Bearer {self._token}'
self._timeout = float(config.timeout)

@property
def config(self) -> kernelci.config.api.API:
Expand Down Expand Up @@ -135,21 +136,29 @@ def _make_url(self, path):

def _get(self, path, params=None):
url = self._make_url(path)
resp = requests.get(url, params, headers=self._headers)
resp = requests.get(
url, params, headers=self._headers, timeout=self._timeout
)
resp.raise_for_status()
return resp

def _post(self, path, data=None, params=None):
url = self._make_url(path)
jdata = json.dumps(data)
resp = requests.post(url, jdata, headers=self._headers, params=params)
resp = requests.post(
url, jdata, headers=self._headers,
params=params, timeout=self._timeout
)
resp.raise_for_status()
return resp

def _put(self, path, data=None, params=None):
url = self._make_url(path)
jdata = json.dumps(data)
resp = requests.put(url, jdata, headers=self._headers, params=params)
resp = requests.put(
url, jdata, headers=self._headers,
params=params, timeout=self._timeout
)
resp.raise_for_status()
return resp

Expand Down
2 changes: 1 addition & 1 deletion kernelci/api/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def create_token(self, username: str, password: str,
if scopes:
data['scope'] = ' '.join(scopes)
url = self._make_url('/token')
resp = requests.post(url, data)
resp = requests.post(url, data, timeout=self._timeout)
resp.raise_for_status()
return resp.json()

Expand Down

0 comments on commit d8a53f6

Please sign in to comment.