From 7b710e48e3b4b67a26c0effc59a2ccf915eef203 Mon Sep 17 00:00:00 2001 From: Chris Carlon Date: Wed, 28 Aug 2024 15:13:27 +0100 Subject: [PATCH] Updates 2024-08-28 - Added more functionalty with search methods --- README.md | 27 ++++++++++++++++++++------- pytest.ini | 2 +- src/api_endpoints.py | 1 + src/herding_cats.py | 28 +++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 65fa071..a17a6be 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ >[!IMPORTANT] > THIS IS A WORK IN PROGRESS +> Using London's Datastore to test functionality # Purpose @@ -11,18 +12,30 @@ ## Examples of UK Open Data Catalogues -1. [Bristol Open Data](https://opendata.bristol.gov.uk) -2. [London Datastore](https://data.london.gov.uk) -3. [Data Mill North](https://datamillnorth.org) -4. [Gov Open Data](https://www.data.gov.uk) +| Catalogue Name | Website | Catalogue API Endpoint | +|----------------|---------|-------------------| +| Bristol Open Data | https://opendata.bristol.gov.uk | TBC | +| London Datastore | https://data.london.gov.uk | CKAN: https://data.london.gov.uk/api/3/ | +| Data Mill North | https://datamillnorth.org | TBC | +| Gov Open Data | https://www.data.gov.uk | TBC | ## Basic usage examples: ```python -# Example usage +# Example usage 1 if __name__ == "__main__": with CatSession("data.london.gov.uk") as session: explore = CatExplorer(session) - v = explore.package_search_json(search_query="census") - pprint(v) + census_package = explore.package_search_json(search_query="census") + pprint(census_package) +``` + +```python +# Example usage 2 +if __name__ == "__main__": + with CatSession("data.london.gov.uk") as session: + explore = CatExplorer(session) + packlage_list = explore.package_list_json() + boundary_info = explore.package_show_info_json('2011-boundary-files') + pprint(show_info) ``` diff --git a/pytest.ini b/pytest.ini index 3f539bb..2d26460 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,3 @@ [pytest] -pythonpath = . +pythonpath = src/ addopts = -v diff --git a/src/api_endpoints.py b/src/api_endpoints.py index e7f61a9..522cbbf 100644 --- a/src/api_endpoints.py +++ b/src/api_endpoints.py @@ -2,6 +2,7 @@ class CkanApiPaths: BASE_PATH = "/api/3/action/{}" PACKAGE_LIST = BASE_PATH.format("package_list") PACKAGE_SEARCH = BASE_PATH.format("package_search") + PACKAGE_INFO = BASE_PATH.format("package_show") # Add more paths as needed... diff --git a/src/herding_cats.py b/src/herding_cats.py index 3c5838a..0999ee0 100644 --- a/src/herding_cats.py +++ b/src/herding_cats.py @@ -8,8 +8,8 @@ from enum import Enum from urllib.parse import urlencode -from .api_endpoints import CkanApiPaths -from .cats_errors import CatExplorerError, CatSessionError +from api_endpoints import CkanApiPaths +from cats_errors import CatExplorerError, CatSessionError class CatSession: def __init__(self, domain: str) -> None: @@ -93,7 +93,24 @@ def package_search_json(self, search_query: Optional[str]=None): params["q"] = search_query url = f"{base_url}?{urlencode(params)}" if params else base_url - print(url) + + try: + response = self.cat_session.session.get(url) + response.raise_for_status() + data = response.json() + return data['result'] + except requests.RequestException as e: + logger.error(f"Failed to search datasets: {e}") + raise CatExplorerError(f"Failed to search datasets: {str(e)}") + + def package_show_info_json(self, package_name:str): + base_url = self.cat_session.base_url + CkanApiPaths.PACKAGE_INFO + + params = {} + if package_name: + params["id"] = package_name + + url = f"{base_url}?{urlencode(params)}" if params else base_url try: response = self.cat_session.session.get(url) @@ -119,5 +136,6 @@ def get_package_count(self) -> int: if __name__ == "__main__": with CatSession("data.london.gov.uk") as session: explore = CatExplorer(session) - v = explore.package_search_json(search_query="census") - pprint(v) + show_list = explore.package_list_json() + show_info = explore.package_show_info_json('2011-boundary-files') + pprint(show_info)