1
- from fastapi import APIRouter , FastAPI
1
+ import os
2
+ from typing import Annotated
3
+
4
+ from fastapi import APIRouter , Depends , FastAPI
5
+ from starlette .requests import Request
2
6
3
7
from cpf .adapters .inbound .rest_api .ion import IonLink
4
- from cpf .adapters .inbound .rest_api .models .responses .core import RootResponse
5
- from cpf .core .ports .provided .services import ManageService , QueryService
8
+ from cpf .adapters .inbound .rest_api .models .responses .core import (
9
+ AuthenticatedRootResponse ,
10
+ UnauthenticatedRootResponse ,
11
+ UserResponse ,
12
+ )
13
+ from cpf .adapters .inbound .rest_api .utils import env_to_bool , fake_user_factory
14
+ from cpf .core .ports .provided .services import (
15
+ ManageService ,
16
+ QueryService ,
17
+ UserManagementService ,
18
+ )
19
+ from cpf .core .ports .required .dtos import UserDTO
6
20
7
21
router = APIRouter (prefix = "/cpf/api" )
8
22
app = FastAPI ()
9
23
10
24
library_manage_service : ManageService | None = None
11
25
library_query_service : QueryService | None = None
26
+ user_management_service : UserManagementService | None = None
12
27
13
28
14
29
def set_library_manage_service (service : ManageService ):
@@ -33,9 +48,47 @@ def get_library_query_service() -> QueryService:
33
48
return library_query_service
34
49
35
50
36
- @router .get (path = "" )
37
- def get_api_root () -> RootResponse :
38
- return RootResponse (get_ladders = IonLink (href = "/cpf/api/library/ladders/" ))
51
+ def set_user_management_service (service : UserManagementService ) -> None :
52
+ global user_management_service
53
+ user_management_service = service
54
+
55
+
56
+ def get_user_management_service () -> UserManagementService :
57
+ if not user_management_service :
58
+ raise RuntimeError ("User management service not set" )
59
+ return user_management_service
60
+
61
+
62
+ class FastAPIAuth :
63
+
64
+ def __call__ (self , request : Request ) -> UserDTO :
65
+ # TODO Remove after auth will be implemented on frontend
66
+ if env_to_bool (os .getenv ("USE_MOCK_USER" )):
67
+ return fake_user_factory ()
68
+ service_instance : UserManagementService = get_user_management_service ()
69
+ return service_instance .get_user (access_token = request .cookies .get ("access_token" ))
70
+
71
+
72
+ auth = FastAPIAuth ()
73
+
74
+
75
+ @router .get (path = "" , response_model_exclude_none = True )
76
+ def get_api_root (
77
+ user : Annotated [UserDTO | None , Depends (auth )]
78
+ ) -> AuthenticatedRootResponse | UnauthenticatedRootResponse :
79
+ if not user :
80
+ return UnauthenticatedRootResponse (
81
+ # TODO Create social auth login redirect
82
+ login = IonLink (href = "/api/login" )
83
+ )
84
+
85
+ return AuthenticatedRootResponse (
86
+ user = UserResponse (
87
+ first_name = user .first_name ,
88
+ last_name = user .last_name ,
89
+ ),
90
+ get_ladders = IonLink (href = "/cpf/api/library/ladders" ),
91
+ )
39
92
40
93
41
94
@router .get (path = "/health" )
@@ -44,6 +97,8 @@ def health_check():
44
97
45
98
46
99
from cpf .adapters .inbound .rest_api .library .api import router as library_router # noqa
100
+ from cpf .adapters .inbound .rest_api .users .api import router as users_router # noqa
47
101
48
102
app .include_router (router = router )
49
103
app .include_router (router = library_router )
104
+ app .include_router (router = users_router )
0 commit comments