This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #12] Setup the opportunity v1 endpoint which will be backed by…
… the index
- Loading branch information
Showing
16 changed files
with
777 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from src.api.opportunities_v1.opportunity_blueprint import opportunity_blueprint | ||
|
||
# import opportunity_routes module to register the API routes on the blueprint | ||
import src.api.opportunities_v1.opportunity_routes # noqa: F401 E402 isort:skip | ||
|
||
__all__ = ["opportunity_blueprint"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from apiflask import APIBlueprint | ||
|
||
opportunity_blueprint = APIBlueprint( | ||
"opportunity_v1", | ||
__name__, | ||
tag="Opportunity v1", | ||
cli_group="opportunity_v1", | ||
url_prefix="/v1", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
|
||
import src.adapters.db as db | ||
import src.adapters.db.flask_db as flask_db | ||
import src.api.opportunities_v1.opportunity_schemas as opportunity_schemas | ||
import src.api.response as response | ||
from src.api.opportunities_v1.opportunity_blueprint import opportunity_blueprint | ||
from src.auth.api_key_auth import api_key_auth | ||
from src.logging.flask_logger import add_extra_data_to_current_request_logs | ||
from src.services.opportunities_v1.get_opportunity import get_opportunity | ||
from src.services.opportunities_v1.search_opportunities import search_opportunities | ||
from src.util.dict_util import flatten_dict | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# Descriptions in OpenAPI support markdown https://swagger.io/specification/ | ||
SHARED_ALPHA_DESCRIPTION = """ | ||
__ALPHA VERSION__ | ||
This endpoint in its current form is primarily for testing and feedback. | ||
Features in this endpoint are still under heavy development, and subject to change. Not for production use. | ||
See [Release Phases](https://github.com/github/roadmap?tab=readme-ov-file#release-phases) for further details. | ||
""" | ||
|
||
|
||
@opportunity_blueprint.post("/opportunities/search") | ||
@opportunity_blueprint.input( | ||
opportunity_schemas.OpportunitySearchRequestV1Schema, arg_name="search_params" | ||
) | ||
# many=True allows us to return a list of opportunity objects | ||
@opportunity_blueprint.output(opportunity_schemas.OpportunityV1Schema(many=True)) | ||
@opportunity_blueprint.auth_required(api_key_auth) | ||
@opportunity_blueprint.doc(description=SHARED_ALPHA_DESCRIPTION) | ||
def opportunity_search(search_params: dict) -> response.ApiResponse: | ||
add_extra_data_to_current_request_logs(flatten_dict(search_params, prefix="request.body")) | ||
logger.info("POST /v1/opportunities/search") | ||
|
||
opportunities, pagination_info = search_opportunities(search_params) | ||
|
||
add_extra_data_to_current_request_logs( | ||
{ | ||
"response.pagination.total_pages": pagination_info.total_pages, | ||
"response.pagination.total_records": pagination_info.total_records, | ||
} | ||
) | ||
logger.info("Successfully fetched opportunities") | ||
|
||
return response.ApiResponse( | ||
message="Success", data=opportunities, pagination_info=pagination_info | ||
) | ||
|
||
|
||
@opportunity_blueprint.get("/opportunities/<int:opportunity_id>") | ||
@opportunity_blueprint.output(opportunity_schemas.OpportunityV1Schema) | ||
@opportunity_blueprint.auth_required(api_key_auth) | ||
@opportunity_blueprint.doc(description=SHARED_ALPHA_DESCRIPTION) | ||
@flask_db.with_db_session() | ||
def opportunity_get(db_session: db.Session, opportunity_id: int) -> response.ApiResponse: | ||
add_extra_data_to_current_request_logs({"opportunity.opportunity_id": opportunity_id}) | ||
logger.info("GET /v1/opportunities/:opportunity_id") | ||
with db_session.begin(): | ||
opportunity = get_opportunity(db_session, opportunity_id) | ||
|
||
return response.ApiResponse(message="Success", data=opportunity) |
Oops, something went wrong.