Skip to content

Commit

Permalink
Fix: add requests timeout (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman authored Sep 18, 2023
2 parents 00044ae + 6bb9dc2 commit e6135df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ci:

repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.1.1
rev: v2.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
Expand All @@ -21,27 +21,27 @@ repos:
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 23.1.0
rev: 23.9.1
hooks:
- id: black
types:
- python

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8
types:
- python

- repo: https://github.com/pycqa/bandit
rev: 1.7.4
rev: 1.7.5
hooks:
- id: bandit
args: ["-ll"]
files: .py$

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.33.0
rev: v0.36.0
hooks:
- id: markdownlint
4 changes: 3 additions & 1 deletion eligibility_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(
jwe_cek_enc,
server_public_key,
headers={},
timeout=5,
):
self.verify_url = verify_url

Expand All @@ -44,6 +45,7 @@ def __init__(
raise ValueError('"Authorization" should not be set as an additional header.')

self.headers = headers
self.timeout = timeout

def _tokenize_request(self, sub, name, types):
"""Create a request token."""
Expand Down Expand Up @@ -91,7 +93,7 @@ def _request(self, sub, name, types):

try:
logger.debug(f"GET request to {self.verify_url}")
r = requests.get(self.verify_url, headers=self._auth_headers(token))
r = requests.get(self.verify_url, headers=self._auth_headers(token), timeout=self.timeout)
except requests.ConnectionError:
raise ApiError("Connection to verification server failed")
except requests.Timeout:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ def test_create_valid_client():
pytest.fail("Failed to create valid Client")


def test_create_valid_client_timeout():
client = Client(**valid_configuration())

assert client.timeout == 5

client = Client(**valid_configuration(), timeout=1000)

assert client.timeout == 1000


@pytest.mark.parametrize("header_name", ["Authorization", "authorization", "AuThOrIzAtIoN"])
def test_create_invalid_client_bad_headers(header_name):
headers = {header_name: "value"}
Expand Down

0 comments on commit e6135df

Please sign in to comment.