Skip to content

Commit

Permalink
Merge pull request #59 from Police-Data-Accessibility-Project/mc_issu…
Browse files Browse the repository at this point in the history
…e_364_consolidate_body_response_formatting

Refactored resource_helpers to include create_outer_model function
  • Loading branch information
maxachis authored Jul 19, 2024
2 parents b50e345 + 3150254 commit 33fb091
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
17 changes: 5 additions & 12 deletions resources/DataSources.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
update_data_source_wrapper,
needs_identification_data_sources_wrapper,
)
from resources.resource_helpers import add_api_key_header_arg
from resources.resource_helpers import add_api_key_header_arg, create_outer_model
from utilities.namespace import create_namespace
from resources.PsycopgResource import PsycopgResource, handle_exceptions

Expand All @@ -28,24 +28,19 @@
"attribute_3": fields.String(
description="Continue for as many attributes as you intend to modify",
),
}
},
)


data_sources_outer_model = namespace_data_source.model(
"DataSourcesOuter",
{
"count": fields.Integer(required=True, description="The count of data objects"),
"data": fields.List(
fields.Nested(data_sources_inner_model),
)
},
data_sources_outer_model = create_outer_model(
namespace_data_source, data_sources_inner_model, "DataSourcesOuter"
)


authorization_parser = namespace_data_source.parser()
add_api_key_header_arg(authorization_parser)


@namespace_data_source.route("/data-sources-by-id/<data_source_id>")
@namespace_data_source.param(
name="data_source_id",
Expand Down Expand Up @@ -160,7 +155,6 @@ def post(self) -> Response:
@namespace_data_source.route("/data-sources-needs-identification")
class DataSourcesNeedsIdentification(PsycopgResource):


@namespace_data_source.response(200, "Success", data_sources_outer_model)
@namespace_data_source.response(500, "Internal server error")
@namespace_data_source.response(400, "Bad request; missing or bad API key")
Expand All @@ -176,7 +170,6 @@ def get(self):
return needs_identification_data_sources_wrapper(db_client)



@namespace_data_source.route("/data-sources-map")
class DataSourcesMap(PsycopgResource):
"""
Expand Down
11 changes: 4 additions & 7 deletions resources/TypeaheadSuggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from middleware.typeahead_suggestion_logic import get_typeahead_suggestions_wrapper
from resources.PsycopgResource import handle_exceptions, PsycopgResource
from resources.resource_helpers import create_outer_model

from utilities.namespace import create_namespace, AppNamespaces

Expand Down Expand Up @@ -49,14 +50,10 @@
},
)

typeahead_suggestions_outer_model = namespace_typeahead_suggestions.model(
typeahead_suggestions_outer_model = create_outer_model(
namespace_typeahead_suggestions,
typeahead_suggestions_inner_model,
"TypeaheadSuggestionsOuter",
{
"suggestions": fields.List(
fields.Nested(typeahead_suggestions_inner_model),
description="A list of suggestions",
),
},
)


Expand Down
19 changes: 13 additions & 6 deletions resources/resource_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,27 @@ def create_search_model(namespace: Namespace) -> Model:
},
)

search_result_outer_model = namespace.model(
"SearchResultOuter",
search_result_outer_model = create_outer_model(
namespace, search_result_inner_model, "SearchResultOuter"
)

return search_result_outer_model


def create_outer_model(namespace: Namespace, inner_model: Model, name: str) -> Model:
return namespace.model(
name,
{
"count": fields.Integer(
required=True, description="Count of data items", attribute="count"
required=True, description=f"Count of {inner_model.name} items", attribute="count"
),
"data": fields.List(
fields.Nested(
search_result_inner_model,
inner_model,
required=True,
description="List of data items",
description=f"List of {inner_model.name} items",
),
attribute="data",
),
},
)
return search_result_outer_model

0 comments on commit 33fb091

Please sign in to comment.