Skip to content

Commit

Permalink
Remove default_headers parameter from get_url_response
Browse files Browse the repository at this point in the history
Not needed anymore. Update the tests and BOI usage.
  • Loading branch information
jond01 committed Sep 1, 2023
1 parent e98e8aa commit 3a99ee0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 32 deletions.
25 changes: 2 additions & 23 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions xil/_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 1 addition & 3 deletions xil/boi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3a99ee0

Please sign in to comment.