wag-managment-api-service-v.../ApiEvents/ValidationServiceApi/events/available/endpoints.py

122 lines
3.6 KiB
Python

from typing import TYPE_CHECKING, Dict, Any, Union
from ApiEvents.base_request_model import DictRequestModel, EndpointBaseRequestModel
from ApiEvents.abstract_class import (
RouteFactoryConfig,
EndpointFactoryConfig,
endpoint_wrapper,
)
from ApiValidations.Custom.token_objects import EmployeeTokenObject, OccupantTokenObject
from ErrorHandlers.Exceptions.api_exc import HTTPExceptionApi
from ApiLibrary.common.line_number import get_line_number_for_error
if TYPE_CHECKING:
from fastapi import Request, HTTPException, status, Body
# Type aliases for common types
prefix = "/available"
async def check_endpoints_available(
request: "Request"
) -> Dict[str, Any]:
"""
Check if endpoints are available.
"""
auth_dict = check_endpoints_available.auth
selection_of_user = None
if auth_dict.is_occupant:
selection_of_user = auth_dict.selected_occupant
else:
selection_of_user = auth_dict.selected_company
if not selection_of_user:
raise HTTPExceptionApi(
error_code="",
lang=auth_dict.lang,
loc=get_line_number_for_error(),
sys_msg="User selection not found",
)
return {"reachable_event_endpoints": selection_of_user.reachable_event_endpoints}
async def check_endpoint_available(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Check if endpoints are available.
"""
auth_dict = check_endpoint_available.auth
print("data", data)
data_dict = data.data
endpoint_asked = data_dict.get("endpoint", None)
if not endpoint_asked:
raise HTTPExceptionApi(
error_code="",
lang=auth_dict.lang,
loc=get_line_number_for_error(),
sys_msg="Endpoint not found",
)
selection_of_user = None
if auth_dict.is_occupant:
selection_of_user = auth_dict.selected_occupant
else:
selection_of_user = auth_dict.selected_company
if not selection_of_user:
raise HTTPExceptionApi(
error_code="",
lang=auth_dict.lang,
loc=get_line_number_for_error(),
sys_msg="User selection not found",
)
if endpoint_asked not in selection_of_user.reachable_event_endpoints:
raise HTTPExceptionApi(
error_code="",
lang=auth_dict.lang,
loc=get_line_number_for_error(),
sys_msg="Endpoint not found",
)
return {
"endpoint": endpoint_asked,
"status": "OK"
}
AVAILABLE_CONFIG = RouteFactoryConfig(
name="available_endpoints",
prefix=prefix,
tags=["Available Endpoints"],
include_in_schema=True,
endpoints=[
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/endpoints",
url_of_endpoint=f"{prefix}/endpoints",
endpoint="/endpoints",
method="POST",
summary="Retrieve all endpoints available for user",
description="",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=check_endpoints_available,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/endpoint",
url_of_endpoint=f"{prefix}/endpoint",
endpoint="/endpoint",
method="POST",
summary="Retrieve an endpoint available for user",
description="",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=check_endpoint_available,
),
],
).as_dict()