Skip to content

Commit

Permalink
feat: revised /by-cw-unit-id resource response
Browse files Browse the repository at this point in the history
fix: None type error when not climate data returned by CADT
  • Loading branch information
wwills2 committed Oct 8, 2024
1 parent dc17882 commit f270468
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
19 changes: 10 additions & 9 deletions app/api/v1/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": []}
Expand All @@ -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)
Expand All @@ -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)
4 changes: 4 additions & 0 deletions app/crud/chia.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ActivityBase,
ActivitySearchBy,
ActivityWithCW,
ActivityByCwUnitIdResponse
)
from app.schemas.key import Key # noqa: F401
from app.schemas.metadata import ( # noqa: F401
Expand Down
6 changes: 2 additions & 4 deletions app/schemas/activity.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
activity: ActivityWithCW = Field(default=None)

0 comments on commit f270468

Please sign in to comment.