diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..4fdb129d 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: patch + changes: + changed: + - //metadata is now implemented via a blueprint/service instead of endpoint. diff --git a/policyengine_api/api.py b/policyengine_api/api.py index 3f3bd725..0f32ae56 100644 --- a/policyengine_api/api.py +++ b/policyengine_api/api.py @@ -18,9 +18,10 @@ simulation_analysis_bp, ) from policyengine_api.routes.tracer_analysis_routes import tracer_analysis_bp +from policyengine_api.routes.metadata_routes import metadata_bp + from .endpoints import ( get_home, - get_metadata, get_household, post_household, update_household, @@ -57,7 +58,7 @@ app.route("/", methods=["GET"])(get_home) -app.route("//metadata", methods=["GET"])(get_metadata) +app.register_blueprint(metadata_bp) app.route("//household/", methods=["GET"])( get_household diff --git a/policyengine_api/endpoints/__init__.py b/policyengine_api/endpoints/__init__.py index cc42c81b..8dc30d42 100644 --- a/policyengine_api/endpoints/__init__.py +++ b/policyengine_api/endpoints/__init__.py @@ -1,5 +1,4 @@ from .home import get_home -from .metadata import get_metadata from .household import ( get_household, post_household, diff --git a/policyengine_api/endpoints/metadata.py b/policyengine_api/endpoints/metadata.py deleted file mode 100644 index 007a484b..00000000 --- a/policyengine_api/endpoints/metadata.py +++ /dev/null @@ -1,12 +0,0 @@ -from policyengine_api.utils.payload_validators import validate_country -from policyengine_api.country import COUNTRIES - - -@validate_country -def get_metadata(country_id: str) -> dict: - """Get metadata for a country. - - Args: - country_id (str): The country ID. - """ - return COUNTRIES.get(country_id).metadata diff --git a/policyengine_api/routes/metadata_routes.py b/policyengine_api/routes/metadata_routes.py new file mode 100644 index 00000000..583f8e5b --- /dev/null +++ b/policyengine_api/routes/metadata_routes.py @@ -0,0 +1,18 @@ +from flask import Blueprint + +from policyengine_api.utils.payload_validators import validate_country +from policyengine_api.services.metadata_service import MetadataService + +metadata_bp = Blueprint("metadata", __name__) +metadata_service = MetadataService() + + +@metadata_bp.route("//metadata", methods=["GET"]) +@validate_country +def get_metadata(country_id: str) -> dict: + """Get metadata for a country. + + Args: + country_id (str): The country ID. + """ + return metadata_service.get_metadata(country_id) diff --git a/policyengine_api/services/metadata_service.py b/policyengine_api/services/metadata_service.py new file mode 100644 index 00000000..b6c7d4e3 --- /dev/null +++ b/policyengine_api/services/metadata_service.py @@ -0,0 +1,12 @@ +from policyengine_api.country import COUNTRIES + + +class MetadataService: + def get_metadata(self, country_id: str) -> dict: + country = COUNTRIES.get(country_id) + if country == None: + raise RuntimeError( + f"Attempted to get metadata for a nonexistant country: '{country_id}'" + ) + + return country.metadata diff --git a/tests/python/test_units.py b/tests/python/test_units.py index b1c19ecc..341a5094 100644 --- a/tests/python/test_units.py +++ b/tests/python/test_units.py @@ -1,4 +1,4 @@ -from policyengine_api.endpoints.metadata import get_metadata +from policyengine_api.routes.metadata_routes import get_metadata def test_units(): diff --git a/tests/python/test_yearly_var_removal.py b/tests/python/test_yearly_var_removal.py index 207e1c73..72433248 100644 --- a/tests/python/test_yearly_var_removal.py +++ b/tests/python/test_yearly_var_removal.py @@ -2,7 +2,7 @@ import json from policyengine_api.endpoints.household import get_household_under_policy -from policyengine_api.endpoints.metadata import get_metadata +from policyengine_api.routes.metadata_routes import get_metadata from policyengine_api.endpoints.policy import get_policy from policyengine_api.constants import COUNTRY_PACKAGE_VERSIONS from policyengine_api.data import database