-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from extrawest/feature/tariff-module-v-2-1-1
[feature] Tariff module v2.1.1
- Loading branch information
Showing
11 changed files
with
421 additions
and
3 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
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 |
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,2 @@ | ||
from .cpo import router as cpo_router | ||
from .emsp import router as emsp_router |
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,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, | ||
) |
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,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, | ||
) |
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
Oops, something went wrong.