From f270468ff5484b5c6e9598eda4aa167588c28c13 Mon Sep 17 00:00:00 2001 From: William Wills Date: Tue, 8 Oct 2024 13:21:31 -0400 Subject: [PATCH] feat: revised /by-cw-unit-id resource response fix: None type error when not climate data returned by CADT --- app/api/v1/activities.py | 19 ++++++++++--------- app/crud/chia.py | 4 ++++ app/schemas/__init__.py | 1 + app/schemas/activity.py | 6 ++---- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/api/v1/activities.py b/app/api/v1/activities.py index 6770ee4..6b4b312 100644 --- a/app/api/v1/activities.py +++ b/app/api/v1/activities.py @@ -124,18 +124,17 @@ async def get_activity( return schemas.ActivitiesResponse(activities=activities_with_cw, total=total) -@router.get("/by-cw-unit-id", response_model=schemas.ActivitiesResponse) +@router.get("/by-cw-unit-id", response_model=schemas.ActivityByCwUnitIdResponse) @disallow([ExecutionMode.REGISTRY, ExecutionMode.CLIENT]) async def get_activity_by_cw_unit_id( cw_unit_id: str, db: Session = Depends(deps.get_db_session), -) -> schemas.ActivitiesResponse: +) -> schemas.ActivityByCwUnitIdResponse: """Get a single activity based on the unit's unitWarehouseId. This endpoint is to be called by the explorer. """ - print(f"****** {cw_unit_id}") db_crud = crud.DBCrud(db=db) activity_filters: Dict[str, Any] = {"or": [], "and": []} @@ -147,25 +146,27 @@ async def get_activity_by_cw_unit_id( ).combine_climate_units_and_metadata(search=cw_filters) if len(climate_data) == 0: logger.warning(f"Failed to retrieve unit from climate warehouse. search:{cw_filters}") - return schemas.ActivitiesResponse() + return schemas.ActivityByCwUnitIdResponse() units = {unit["marketplaceIdentifier"]: unit for unit in climate_data} if len(units) != 0: activity_filters["and"].append(models.Activity.asset_id.in_(units.keys())) - activities: models.Activity + activities: [models.Activity] total: int + page = 1 + limit = 10 (activities, total) = db_crud.select_activity_with_pagination( model=models.Activity, filters=activity_filters, order_by='desc', - page=1, - limit=10, + page=page, + limit=limit, ) if len(activities) == 0: logger.warning(f"No data to get from activities. filters:{activity_filters} page:{page} limit:{limit}") - return schemas.ActivitiesResponse() + return schemas.ActivityByCwUnitIdResponse() activity = activities[0] unit = units.get(activity.asset_id) @@ -192,4 +193,4 @@ async def get_activity_by_cw_unit_id( **jsonable_encoder(activity), ) - return schemas.ActivitiesResponse(activity=activity_with_cw) + return schemas.ActivityByCwUnitIdResponse(activity=activity_with_cw) diff --git a/app/crud/chia.py b/app/crud/chia.py index a9573d9..8d55252 100644 --- a/app/crud/chia.py +++ b/app/crud/chia.py @@ -65,6 +65,10 @@ def _get_paginated_data(self, path: str, search_params: Dict[str, Any]) -> List[ raise error_code.internal_server_error(message="API Call Failure") data = response.json() + if data is None: + # some cadt endpoints return null with no pagination info if no data is found + # to prevent an infinite loop need to assume that there is no data matching the search from this iteration on + return all_data all_data.extend(data["data"]) # Add data from the current page diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py index 8d74d16..6ff7174 100644 --- a/app/schemas/__init__.py +++ b/app/schemas/__init__.py @@ -6,6 +6,7 @@ ActivityBase, ActivitySearchBy, ActivityWithCW, + ActivityByCwUnitIdResponse ) from app.schemas.key import Key # noqa: F401 from app.schemas.metadata import ( # noqa: F401 diff --git a/app/schemas/activity.py b/app/schemas/activity.py index 34f30ac..72e204a 100644 --- a/app/schemas/activity.py +++ b/app/schemas/activity.py @@ -1,6 +1,7 @@ from __future__ import annotations import enum +from email.policy import default from typing import Any, Dict, List, Optional, Union from pydantic import Field, validator @@ -59,7 +60,4 @@ class ActivitiesResponse(BaseModel): total: int = 0 class ActivityByCwUnitIdResponse(BaseModel): - activity: ActivityWithCW = Field(default_factory=ActivityWithCW) - -class ActivityByCwUnitIdRequest(BaseModel): - cw_unit_id: str = Field(example='bd941858-f8b0-4327-839f-0c13f46e181e') \ No newline at end of file + activity: ActivityWithCW = Field(default=None) \ No newline at end of file