diff --git a/README.md b/README.md index 93c8b03..d89d283 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,17 @@ Here's a basic example of how to use the `CensusAPI` class to retrieve a table: ``` +> [!TIP] +> If you encounter SSL verification issues, you can bypass this step by +> setting the `verify` parameter when creating an instance of `CensusAPI`: +> ```python +> api_unverified = CensusAPI(verify=False) +> ``` +> In general, this is not recommended as SSL verification helps +> ensure the security of your machine and its connection to the internet. +> Please use this at your own discretion. + + ## Limitations The `CensusAPI` class includes a variety of methods to interact with the API in @@ -99,6 +110,7 @@ query tables containing age data despite being able to create them through the web interface. Again, this is a deliberate choice by the developers and may be subject to change. + ## Contributing This project is open-source, and contributions from the community are welcome. diff --git a/src/census21api/wrapper.py b/src/census21api/wrapper.py index 61bbe4a..446a215 100644 --- a/src/census21api/wrapper.py +++ b/src/census21api/wrapper.py @@ -15,7 +15,16 @@ class CensusAPI: - """A wrapper for the 2021 England and Wales Census API.""" + """A wrapper for the 2021 England and Wales Census API. + + Parameters + ---------- + verify : bool + Whether to use SSL verification. Defaults to True. + """ + + def __init__(self, verify: bool = True) -> None: + self.verify: bool = verify def _process_response(self, response: Response) -> JSONLike: """ @@ -74,7 +83,7 @@ def get(self, url: str) -> JSONLike: successful, and `None` otherwise. """ - response = requests.get(url, verify=True) + response = requests.get(url, verify=self.verify) return self._process_response(response) diff --git a/tests/test_examples.py b/tests/test_examples.py index 7ffda04..a807609 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -6,7 +6,38 @@ def test_issue_39_blocked_pairs(): - """https://github.com/datasciencecampus/census21api/issues/39""" + """ + https://github.com/datasciencecampus/census21api/issues/39 + + This test no longer works as is. See + `test_issue_39_blocked_pairs_gets_400_error` for an update to this + case. However, we keep this test for posterity, and test that it + **doesn't** work. + """ + + population_type = "UR_HH" + area_type = "nat" + dimensions = ("economic_activity_status_12a", "industry_current_88a") + + api = CensusAPI() + + with pytest.warns(UserWarning) as w: + assert "blocked pair" not in w + table = api.query_table(population_type, area_type, dimensions) + + assert table is None + + +def test_issue_39_blocked_pairs_gets_400_error(): + """ + Originally, this was about catching blocked pairs. + + See https://github.com/datasciencecampus/census21api/issues/39 for + details on the original issue. + + As of 2024-02-16, it seems that blocked pairs now give a 400 error. + This test now checks for that. + """ population_type = "UR_HH" area_type = "nat" @@ -14,7 +45,7 @@ def test_issue_39_blocked_pairs(): api = CensusAPI() - with pytest.warns(UserWarning, match="blocked pair"): + with pytest.warns(UserWarning, match="Status code: 400"): table = api.query_table(population_type, area_type, dimensions) assert table is None diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index 3d9d7c2..70c4fbc 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -27,13 +27,14 @@ MOCK_URL = "mock://test.com/" -def test_init(): +@given(st.booleans()) +def test_init(verify: bool): """Test that the `CensusAPI` class can be instantiated correctly.""" - api = CensusAPI() + api = CensusAPI(verify) assert isinstance(api, CensusAPI) - assert vars(api) == {} + assert vars(api) == {"verify": verify} @given(st.dictionaries(st.text(), st.text())) @@ -91,11 +92,11 @@ def test_process_response_invalid_json(): assert data is None -@given(st.dictionaries(st.text(), st.text())) -def test_get(json): +@given(st.dictionaries(st.text(), st.text()), st.booleans()) +def test_get(json, verify: bool): """Test that the API only gives data from successful responses.""" - api = CensusAPI() + api = CensusAPI(verify) with mock.patch("census21api.wrapper.requests.get") as get, mock.patch( "census21api.wrapper.CensusAPI._process_response" @@ -107,7 +108,7 @@ def test_get(json): assert data == json - get.assert_called_once_with(MOCK_URL, verify=True) + get.assert_called_once_with(MOCK_URL, verify=verify) process.assert_called_once_with(response)