Skip to content

Commit

Permalink
Merge pull request #2039 from PolicyEngine/1992_metadata_endpoint_to_…
Browse files Browse the repository at this point in the history
…blueprint

Refactor metadata to blueprint/service
  • Loading branch information
anth-volk authored Dec 9, 2024
2 parents 6a53aa2 + 47be081 commit 670e6d4
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: patch
changes:
changed:
- /<country_id>/metadata is now implemented via a blueprint/service instead of endpoint.
5 changes: 3 additions & 2 deletions policyengine_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,7 +58,7 @@

app.route("/", methods=["GET"])(get_home)

app.route("/<country_id>/metadata", methods=["GET"])(get_metadata)
app.register_blueprint(metadata_bp)

app.route("/<country_id>/household/<household_id>", methods=["GET"])(
get_household
Expand Down
1 change: 0 additions & 1 deletion policyengine_api/endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .home import get_home
from .metadata import get_metadata
from .household import (
get_household,
post_household,
Expand Down
12 changes: 0 additions & 12 deletions policyengine_api/endpoints/metadata.py

This file was deleted.

18 changes: 18 additions & 0 deletions policyengine_api/routes/metadata_routes.py
Original file line number Diff line number Diff line change
@@ -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("/<country_id>/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)
12 changes: 12 additions & 0 deletions policyengine_api/services/metadata_service.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/python/test_units.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from policyengine_api.endpoints.metadata import get_metadata
from policyengine_api.routes.metadata_routes import get_metadata


def test_units():
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_yearly_var_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 670e6d4

Please sign in to comment.