Skip to content

Commit

Permalink
Merge pull request #4 from mgxd/enh/timeout
Browse files Browse the repository at this point in the history
enh: add timeout + request kwargs
  • Loading branch information
mgxd authored Sep 16, 2019
2 parents a46b922 + cb8c0fb commit bf70c9b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
16 changes: 13 additions & 3 deletions etelemetry/client.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
from requests import request, ConnectionError
from requests import request, ConnectionError, ReadTimeout

from .config import ET_PROJECTS


def _etrequest(endpoint, method="get", **kwargs):
if kwargs.get('timeout') is None:
kwargs['timeout'] = 5
try:
res = request(method, endpoint, **kwargs)
except ConnectionError:
raise RuntimeError("Connection to server could not be made")
except ReadTimeout:
raise RuntimeError(
"No response from server in {timeout} seconds".format(
timeout=kwargs.get('timeout')
)
)
res.raise_for_status()
return res


def get_project(repo):
def get_project(repo, **rargs):
"""
Fetch latest version from server.
Parameters
==========
repo : str
GitHub repository as <owner>/<project>
**rargs
Request keyword arguments
Returns
=======
Expand All @@ -28,5 +38,5 @@ def get_project(repo):
"""
if "/" not in repo:
raise ValueError("Invalid repository")
res = _etrequest(ET_PROJECTS + repo)
res = _etrequest(ET_PROJECTS.format(repo=repo), **rargs)
return res.json(encoding="utf-8")
13 changes: 7 additions & 6 deletions etelemetry/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
hostname = "rig.mit.edu/"
hostname = "rig.mit.edu"
https = True

if https is True:
ET_ROOT = "https://" + hostname
prefix = "https"
else:
ET_ROOT = "http://" + hostname
prefix = "http"

root_endpoint = "et/"
ET_ROOT += root_endpoint
ET_ROOT = "{prefix}://{hostname}/et/".format(
prefix=prefix, hostname=hostname
)

ET_PROJECTS = ET_ROOT + "projects/"
ET_PROJECTS = ET_ROOT + "projects/{repo}"
9 changes: 7 additions & 2 deletions etelemetry/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
from ..client import _etrequest, get_project


def test_request():
def test_etrequest():
endpoint = "http://fakeendpoint/"
with pytest.raises(Exception):
with pytest.raises(RuntimeError):
_etrequest(endpoint, method="get")
assert _etrequest(ET_ROOT)
# ensure timeout is working properly
endpoint = "https://google.com"
with pytest.raises(RuntimeError):
_etrequest(endpoint, timeout=0.01)
assert _etrequest(endpoint)


def test_get_project():
Expand Down

0 comments on commit bf70c9b

Please sign in to comment.