From 3a99ee0a7c5cdd4d16f9f7ee25841945863474c1 Mon Sep 17 00:00:00 2001 From: Jonathan Daniel Date: Fri, 1 Sep 2023 14:57:41 +0300 Subject: [PATCH] Remove `default_headers` parameter from `get_url_response` Not needed anymore. Update the tests and BOI usage. --- tests/test_headers.py | 25 ++----------------------- xil/_headers.py | 7 +------ xil/boi.py | 4 +--- 3 files changed, 4 insertions(+), 32 deletions(-) diff --git a/tests/test_headers.py b/tests/test_headers.py index 12161b4..49e9ae6 100644 --- a/tests/test_headers.py +++ b/tests/test_headers.py @@ -9,7 +9,7 @@ import pytest -from xil._headers import _DEFAULT_CONTEXT, UA_HEADER, get_url_response +from xil._headers import _DEFAULT_CONTEXT, get_url_response @pytest.fixture(name="url") @@ -35,38 +35,17 @@ def _compare_requests(request1: Request, request2: Request) -> bool: ) -@pytest.mark.parametrize( - ("default_headers", "expected_headers"), [(False, {}), (True, UA_HEADER)] -) -def test_get_url_response_headers( - url: str, - default_headers: bool, - expected_headers: dict[str, str], - mock_urlopen: Mock, -) -> None: - """Test the get_url_response request headers""" - get_url_response(url, default_headers) - mock_urlopen.assert_called_once() - actual_request = mock_urlopen.call_args.args[0] - expected_request = Request(url=url, headers=expected_headers) - assert _compare_requests( - actual_request, expected_request - ), "The actual request is different than expected" - - -@pytest.mark.parametrize("default_headers", [False, True]) @pytest.mark.parametrize( ("set_context", "expected_context"), [(False, None), (True, _DEFAULT_CONTEXT)] ) def test_get_url_response_context( url: str, - default_headers: bool, set_context: bool, expected_context: ssl.SSLContext | None, mock_urlopen: Mock, ) -> None: """Test that get_url_response sets an SSL context when it is asked to""" - get_url_response(url, default_headers=default_headers, set_context=set_context) + get_url_response(url, set_context=set_context) mock_urlopen.assert_called_once() assert mock_urlopen.call_args.kwargs == { "context": expected_context, diff --git a/xil/_headers.py b/xil/_headers.py index ca745f6..fb9f2b7 100644 --- a/xil/_headers.py +++ b/xil/_headers.py @@ -18,22 +18,17 @@ def get_url_response( url: str, - default_headers: bool = True, set_context: bool = False, ) -> http.client.HTTPResponse: """ Return the response from a URL with custom headers and SSL context when opening if set_context is True. """ - if default_headers: - headers = UA_HEADER - else: - headers = {} if set_context: context = _DEFAULT_CONTEXT else: context = None - request = urllib.request.Request(url, headers=headers) + request = urllib.request.Request(url) return urllib.request.urlopen( # type: ignore[no-any-return] request, context=context ) diff --git a/xil/boi.py b/xil/boi.py index bf40a2f..20d03f9 100644 --- a/xil/boi.py +++ b/xil/boi.py @@ -25,9 +25,7 @@ [[_CURRENCY_KEY], ["code", "official rate", "change (%)", "amount", "time"]] ) -get_boi_url_response = partial( - get_url_response, default_headers=False, set_context=True -) +get_boi_url_response = partial(get_url_response, set_context=True) def get_boi_df(url: str = _BOI_URL) -> pd.DataFrame: