Skip to content

Commit

Permalink
feat: Change app initialization from only v_2_2_1 to support differen…
Browse files Browse the repository at this point in the history
…t versions, but with one version for one app.
  • Loading branch information
oleksandr-bozbei-ew committed Sep 1, 2023
1 parent 5f488e2 commit c76a273
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
10 changes: 10 additions & 0 deletions py_ocpi/core/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from py_ocpi.modules.versions.enums import VersionNumber

from .v_2_2_1 import ROUTERS_DICT as V_2_2_1_ROUTERS_DICT
from .v_2_1_1 import ROUTERS_DICT as V_2_1_1_ROUTERS_DICT


ROUTERS = {
VersionNumber.v_2_2_1: V_2_2_1_ROUTERS_DICT,
VersionNumber.v_2_1_1: V_2_1_1_ROUTERS_DICT,
}
8 changes: 8 additions & 0 deletions py_ocpi/core/routers/v_2_1_1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from py_ocpi.routers import v_2_1_1_cpo_router, v_2_1_1_emsp_router
from py_ocpi.modules.versions import versions_v_2_1_1_router

ROUTERS_DICT = {
"version_router": versions_v_2_1_1_router,
"cpo_router": v_2_1_1_cpo_router,
"emsp_router": v_2_1_1_emsp_router,
}
8 changes: 8 additions & 0 deletions py_ocpi/core/routers/v_2_2_1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from py_ocpi.routers import v_2_2_1_cpo_router, v_2_2_1_emsp_router
from py_ocpi.modules.versions import versions_v_2_2_1_router

ROUTERS_DICT = {
"version_router": versions_v_2_2_1_router,
"cpo_router": v_2_2_1_cpo_router,
"emsp_router": v_2_2_1_emsp_router,
}
78 changes: 38 additions & 40 deletions py_ocpi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
)
from py_ocpi.core.endpoints import ENDPOINTS

from py_ocpi.modules.versions import (
router as versions_router,
versions_v_2_2_1_router,
)
from py_ocpi.modules.versions import router as versions_router
from py_ocpi.modules.versions.enums import VersionNumber
from py_ocpi.modules.versions.schemas import Version
from py_ocpi.core.dependencies import (
Expand All @@ -32,7 +29,7 @@
http_router as http_push_router,
websocket_router as websocket_push_router,
)
from py_ocpi.routers import v_2_2_1_cpo_router, v_2_2_1_emsp_router
from py_ocpi.core.routers import ROUTERS


class ExceptionHandlerMiddleware(BaseHTTPMiddleware):
Expand Down Expand Up @@ -98,45 +95,46 @@ def get_application(
versions = []
version_endpoints: dict[str, list] = {}

if VersionNumber.v_2_2_1 in version_numbers:
if not version_numbers or len(version_numbers) > 1:
raise ValueError("Should be exactly one version.")

version = version_numbers[0]
mapped_version = ROUTERS.get(version)
if not mapped_version:
raise ValueError("Version isn't supported yet.")

_app.include_router(
mapped_version["version_router"],
prefix=f"/{settings.OCPI_PREFIX}",
)

versions.append(
Version(
version=version,
url=URL(
f"https://{settings.OCPI_HOST}/{settings.OCPI_PREFIX}/"
f"{version.value}/details"
),
).dict(),
)

version_endpoints[version] = []

if RoleEnum.cpo in roles:
_app.include_router(
versions_v_2_2_1_router,
prefix=f"/{settings.OCPI_PREFIX}",
mapped_version["cpo_router"],
prefix=f"/{settings.OCPI_PREFIX}/cpo/{version.value}",
tags=["CPO"],
)
version_endpoints[version] += ENDPOINTS[version][RoleEnum.cpo]

versions.append(
Version(
version=VersionNumber.v_2_2_1,
url=URL(
f"https://{settings.OCPI_HOST}/{settings.OCPI_PREFIX}/"
f"{VersionNumber.v_2_2_1.value}/details"
),
).dict(),
if RoleEnum.emsp in roles:
_app.include_router(
mapped_version["emsp_router"],
prefix=f"/{settings.OCPI_PREFIX}/emsp/{version.value}",
tags=["EMSP"],
)

version_endpoints[VersionNumber.v_2_2_1] = []

if RoleEnum.cpo in roles:
_app.include_router(
v_2_2_1_cpo_router,
prefix=f"/{settings.OCPI_PREFIX}/cpo/"
f"{VersionNumber.v_2_2_1.value}",
tags=["CPO"],
)
version_endpoints[VersionNumber.v_2_2_1] += ENDPOINTS[
VersionNumber.v_2_2_1
][RoleEnum.cpo]

if RoleEnum.emsp in roles:
_app.include_router(
v_2_2_1_emsp_router,
prefix=f"/{settings.OCPI_PREFIX}/emsp/"
f"{VersionNumber.v_2_2_1.value}",
tags=["EMSP"],
)
version_endpoints[VersionNumber.v_2_2_1] += ENDPOINTS[
VersionNumber.v_2_2_1
][RoleEnum.emsp]
version_endpoints[version] += ENDPOINTS[version][RoleEnum.emsp]

def override_get_crud():
return crud
Expand Down

0 comments on commit c76a273

Please sign in to comment.