Skip to content

Commit

Permalink
Use a pydantic model for client to serialize it in auth
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLester committed Apr 29, 2023
1 parent 94c3767 commit 6329cb1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
17 changes: 16 additions & 1 deletion pv_site_api/_db_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
import structlog
from fastapi import Depends
from pvsite_datamodel.read.generation import get_pv_generation_by_sites
from pvsite_datamodel.sqlmodels import ForecastSQL, ForecastValueSQL, InverterSQL, SiteSQL
from pvsite_datamodel.sqlmodels import (
ClientSQL,
ForecastSQL,
ForecastValueSQL,
InverterSQL,
SiteSQL,
)
from sqlalchemy.orm import Session, aliased

from .pydantic_models import (
Forecast,
MultiplePVActual,
PVActualValue,
PVClientMetadata,
PVSiteMetadata,
SiteForecastValues,
)
Expand Down Expand Up @@ -230,6 +237,14 @@ def site_to_pydantic(site: SiteSQL) -> PVSiteMetadata:
return pv_site


def client_to_pydantic(client: ClientSQL) -> PVClientMetadata:
"""Converts a ClientSQL object into a PVClientMetadata object."""
pv_client = PVClientMetadata(
client_uuid=str(client.client_uuid), client_name=client.client_name
)
return pv_client


def does_site_exist(session: Session, site_uuid: str) -> bool:
"""Checks if a site exists."""
return (
Expand Down
9 changes: 8 additions & 1 deletion pv_site_api/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import structlog

from ._db_helpers import client_to_pydantic

logger = structlog.stdlib.get_logger()

CACHE_TIME_SECONDS = 120
Expand Down Expand Up @@ -39,10 +41,15 @@ def wrapper(*args, **kwargs): # noqa
route_variables = kwargs.copy()

# drop session and user
for var in ["session", "user", "auth"]:
for var in ["session", "user"]:
if var in route_variables:
route_variables.pop(var)

# translate authenticated client to serializable type
route_variables["auth"] = (
route_variables["auth"] and client_to_pydantic(route_variables["auth"]).json()
)

# make into string
route_variables = json.dumps(route_variables)
args_as_json = json.dumps(args)
Expand Down
6 changes: 3 additions & 3 deletions pv_site_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def get_pv_actual(
To test the route, you can input any number for the site_uuid (ex. 567)
to generate a list of datetimes and actual kw generation for that site.
"""
return (get_pv_actual_many_sites(site_uuids=site_uuid, session=session))[0]
return (get_pv_actual_many_sites(site_uuids=site_uuid, session=session, auth=auth))[0]


@app.get("/sites/pv_actual", response_model=list[MultiplePVActual])
Expand Down Expand Up @@ -330,7 +330,7 @@ def get_pv_forecast(
if not site_exists:
raise HTTPException(status_code=404)

forecasts = get_pv_forecast_many_sites(site_uuids=site_uuid, session=session)
forecasts = get_pv_forecast_many_sites(site_uuids=site_uuid, session=session, auth=auth)

if len(forecasts) == 0:
return JSONResponse(status_code=204, content="no data")
Expand Down Expand Up @@ -380,7 +380,7 @@ def get_pv_estimate_clearsky(
if not site_exists:
raise HTTPException(status_code=404)

clearsky_estimates = get_pv_estimate_clearsky_many_sites(site_uuid, session)
clearsky_estimates = get_pv_estimate_clearsky_many_sites(site_uuid, session, auth=auth)
return clearsky_estimates[0]


Expand Down
7 changes: 7 additions & 0 deletions pv_site_api/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class PVSiteMetadata(BaseModel):
installed_capacity_kw: float = Field(..., description="The site's capacity in kw", ge=0)


class PVClientMetadata(BaseModel):
"""Client metadata"""

client_uuid: str = Field(..., description="Unique internal ID for client.")
client_name: str = Field(..., description="Name for the client.")


# post_pv_actual
# get_pv_actual_date
# posting data too the database
Expand Down

0 comments on commit 6329cb1

Please sign in to comment.