Skip to content

Commit

Permalink
Merge pull request #10 from extrawest/feature/tariff-module-v-2-1-1
Browse files Browse the repository at this point in the history
[feature] Tariff module v2.1.1
  • Loading branch information
andrewdubyniak authored Sep 29, 2023
2 parents f3d795c + 000d825 commit efcbf4a
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Example: `http://127.0.0.1:8000/ocpi/docs/`
- It's now possible to initialize a few versions of ocpi for one project;
- Minimal required python version is 3.10;
- Add cdrs module;
- Add tariffs module;


## Related
Expand Down
2 changes: 1 addition & 1 deletion py_ocpi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Python Implementation of OCPI"""

__version__ = "2023.09.28"
__version__ = "2023.10.02"

from .core import enums, data_types
from .main import get_application
6 changes: 6 additions & 0 deletions py_ocpi/core/endpoints/v_2_1_1/cpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
url=URL(f"{URL_BASE}/{ModuleID.cdrs.value}"),
)

TARIFFS = Endpoint(
identifier=ModuleID.tariffs,
url=URL(f"{URL_BASE}/{ModuleID.tariffs.value}"),
)

ENDPOINTS_LIST = {
ModuleID.credentials_and_registration: CREDENTIALS_AND_REGISTRATION,
ModuleID.locations: LOCATIONS,
ModuleID.cdrs: CDRS,
ModuleID.tariffs: TARIFFS,
}
6 changes: 6 additions & 0 deletions py_ocpi/core/endpoints/v_2_1_1/emsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
url=URL(f"{URL_BASE}/{ModuleID.cdrs.value}"),
)

TARIFFS = Endpoint(
identifier=ModuleID.tariffs,
url=URL(f"{URL_BASE}/{ModuleID.tariffs.value}"),
)

ENDPOINTS_LIST = {
ModuleID.credentials_and_registration: CREDENTIALS_AND_REGISTRATION,
ModuleID.locations: LOCATIONS,
ModuleID.cdrs: CDRS,
ModuleID.tariffs: TARIFFS,
}
2 changes: 2 additions & 0 deletions py_ocpi/modules/tariffs/v_2_1_1/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .cpo import router as cpo_router
from .emsp import router as emsp_router
45 changes: 45 additions & 0 deletions py_ocpi/modules/tariffs/v_2_1_1/api/cpo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from fastapi import APIRouter, Depends, Response, Request

from py_ocpi.core.utils import get_list, get_auth_token_from_header
from py_ocpi.core import status
from py_ocpi.core.schemas import OCPIResponse
from py_ocpi.core.adapter import Adapter
from py_ocpi.core.crud import Crud
from py_ocpi.core.enums import ModuleID, RoleEnum
from py_ocpi.core.dependencies import get_crud, get_adapter, pagination_filters
from py_ocpi.modules.versions.enums import VersionNumber

router = APIRouter(
prefix="/tariffs",
)


@router.get("/", response_model=OCPIResponse)
async def get_tariffs(
request: Request,
response: Response,
crud: Crud = Depends(get_crud),
adapter: Adapter = Depends(get_adapter),
filters: dict = Depends(pagination_filters),
):
auth_token = get_auth_token_from_header(request)

data_list = await get_list(
response,
filters,
ModuleID.tariffs,
RoleEnum.cpo,
VersionNumber.v_2_1_1,
crud,
auth_token=auth_token,
)

tariffs = []
for data in data_list:
tariffs.append(
adapter.tariff_adapter(data, VersionNumber.v_2_1_1).dict()
)
return OCPIResponse(
data=tariffs,
**status.OCPI_1000_GENERIC_SUCESS_CODE,
)
177 changes: 177 additions & 0 deletions py_ocpi/modules/tariffs/v_2_1_1/api/emsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import copy

from fastapi import APIRouter, Depends, Request

from py_ocpi.core import status
from py_ocpi.core.adapter import Adapter
from py_ocpi.core.crud import Crud
from py_ocpi.core.data_types import String
from py_ocpi.core.dependencies import get_crud, get_adapter
from py_ocpi.core.enums import ModuleID, RoleEnum
from py_ocpi.core.schemas import OCPIResponse
from py_ocpi.core.utils import (
get_auth_token_from_header,
partially_update_attributes,
)
from py_ocpi.modules.tariffs.v_2_1_1.schemas import Tariff, TariffPartialUpdate
from py_ocpi.modules.versions.enums import VersionNumber

router = APIRouter(
prefix="/tariffs",
)


@router.get(
"/{country_code}/{party_id}/{tariff_id}", response_model=OCPIResponse
)
async def get_tariff(
request: Request,
country_code: String(2), # type: ignore
party_id: String(3), # type: ignore
tariff_id: String(36), # type: ignore
crud: Crud = Depends(get_crud),
adapter: Adapter = Depends(get_adapter),
):
auth_token = get_auth_token_from_header(request)

data = await crud.get(
ModuleID.tariffs,
RoleEnum.emsp,
tariff_id,
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)
return OCPIResponse(
data=[adapter.tariff_adapter(data, VersionNumber.v_2_1_1).dict()],
**status.OCPI_1000_GENERIC_SUCESS_CODE,
)


@router.put(
"/{country_code}/{party_id}/{tariff_id}", response_model=OCPIResponse
)
async def add_or_update_tariff(
request: Request,
country_code: String(2), # type: ignore
party_id: String(3), # type: ignore
tariff_id: String(36), # type: ignore
tariff: Tariff,
crud: Crud = Depends(get_crud),
adapter: Adapter = Depends(get_adapter),
):
auth_token = get_auth_token_from_header(request)

data = await crud.get(
ModuleID.tariffs,
RoleEnum.emsp,
tariff_id,
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)
if data:
data = await crud.update(
ModuleID.tariffs,
RoleEnum.emsp,
tariff.dict(),
tariff_id,
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)
else:
data = await crud.create(
ModuleID.tariffs,
RoleEnum.emsp,
tariff.dict(),
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)

return OCPIResponse(
data=[adapter.tariff_adapter(data, VersionNumber.v_2_1_1).dict()],
**status.OCPI_1000_GENERIC_SUCESS_CODE,
)


@router.patch(
"/{country_code}/{party_id}/{tariff_id}", response_model=OCPIResponse
)
async def partial_update_tariff(
request: Request,
country_code: String(2), # type: ignore
party_id: String(3), # type: ignore
tariff_id: String(36), # type: ignore
tariff: TariffPartialUpdate,
crud: Crud = Depends(get_crud),
adapter: Adapter = Depends(get_adapter),
):
auth_token = get_auth_token_from_header(request)

old_data = await crud.get(
ModuleID.tariffs,
RoleEnum.emsp,
tariff_id,
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)

old_tariff = adapter.tariff_adapter(old_data, VersionNumber.v_2_1_1)
new_tariff = copy.deepcopy(old_tariff)

partially_update_attributes(
new_tariff, tariff.dict(exclude_defaults=True, exclude_unset=True)
)

data = await crud.update(
ModuleID.tariffs,
RoleEnum.emsp,
new_tariff.dict(),
tariff_id,
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)

return OCPIResponse(
data=[adapter.tariff_adapter(data, VersionNumber.v_2_1_1).dict()],
**status.OCPI_1000_GENERIC_SUCESS_CODE,
)


@router.delete(
"/{country_code}/{party_id}/{tariff_id}", response_model=OCPIResponse
)
async def delete_tariff(
request: Request,
country_code: String(2), # type: ignore
party_id: String(3), # type: ignore
tariff_id: String(36), # type: ignore
crud: Crud = Depends(get_crud),
adapter: Adapter = Depends(get_adapter),
):
auth_token = get_auth_token_from_header(request)

await crud.delete(
ModuleID.tariffs,
RoleEnum.emsp,
tariff_id,
auth_token=auth_token,
country_code=country_code,
party_id=party_id,
version=VersionNumber.v_2_1_1,
)

return OCPIResponse(
data=[],
**status.OCPI_1000_GENERIC_SUCESS_CODE,
)
10 changes: 10 additions & 0 deletions py_ocpi/modules/tariffs/v_2_1_1/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ class Tariff(BaseModel):
elements: List[TariffElement]
energy_mix: Optional[EnergyMix]
last_updated: DateTime


class TariffPartialUpdate(BaseModel):
id: Optional[String(36)] # type: ignore
currency: Optional[String(3)] # type: ignore
tariff_alt_text: Optional[List[DisplayText]]
tariff_alt_url: Optional[URL]
elements: Optional[List[TariffElement]]
energy_mix: Optional[EnergyMix]
last_updated: Optional[DateTime]
5 changes: 4 additions & 1 deletion py_ocpi/routers/v_2_1_1/cpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
from py_ocpi.modules.cdrs.v_2_1_1.api import (
cpo_router as cdrs_cpo_2_1_1_router,
)

from py_ocpi.modules.tariffs.v_2_1_1.api import (
cpo_router as tariffs_cpo_2_1_1_router,
)

router = {
ModuleID.locations: locations_cpo_2_1_1_router,
ModuleID.credentials_and_registration: credentials_cpo_2_1_1_router,
ModuleID.cdrs: cdrs_cpo_2_1_1_router,
ModuleID.tariffs: tariffs_cpo_2_1_1_router,
}
5 changes: 4 additions & 1 deletion py_ocpi/routers/v_2_1_1/emsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
from py_ocpi.modules.cdrs.v_2_1_1.api import (
emsp_router as cdrs_emsp_2_1_1_router,
)

from py_ocpi.modules.tariffs.v_2_1_1.api import (
emsp_router as tariffs_emsp_2_1_1_router,
)

router = {
ModuleID.locations: locations_emsp_2_1_1_router,
ModuleID.credentials_and_registration: credentials_emsp_2_1_1_router,
ModuleID.cdrs: cdrs_emsp_2_1_1_router,
ModuleID.tariffs: tariffs_emsp_2_1_1_router,
}
Loading

0 comments on commit efcbf4a

Please sign in to comment.