Skip to content

Commit 7c375b2

Browse files
committed
refactored Enum__Client__Mode with Enum__Fast_API__Service__Registry__Client__Mode
1 parent 61af42b commit 7c375b2

10 files changed

Lines changed: 97 additions & 93 deletions

osbot_fast_api/client/Client__Generator__AST.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _generate_request_handler(self) -> str: #
152152
import requests
153153
from osbot_utils.type_safe.Type_Safe import Type_Safe
154154
155-
class Enum__Client__Mode(str, Enum):
155+
class Enum__Fast_API__Service__Registry__Client__Mode(str, Enum):
156156
REMOTE = "remote" # HTTP calls to deployed service
157157
IN_MEMORY = "in_memory" # FastAPI TestClient (same process)
158158
LOCAL_SERVER = "local_server" # Fast_API_Server (local HTTP)
@@ -167,7 +167,7 @@ class {self.client_name}__Requests__Result(Type_Safe):
167167
168168
class {self.client_name}__Requests(Type_Safe):
169169
config : Any # {self.client_name}__Config
170-
mode : Enum__Client__Mode = Enum__Client__Mode.REMOTE
170+
mode : Enum__Fast_API__Service__Registry__Client__Mode = Enum__Fast_API__Service__Registry__Client__Mode.REMOTE
171171
_app : Optional[Any] = None # FastAPI app for in-memory
172172
_server : Optional[Any] = None # Fast_API_Server for local
173173
_test_client : Optional[Any] = None # TestClient for in-memory
@@ -180,19 +180,19 @@ def __init__(self, **kwargs):
180180
def _setup_mode(self): # Initialize the appropriate execution backend
181181
182182
if self._app: # In-memory mode with TestClient
183-
self.mode = Enum__Client__Mode.IN_MEMORY
183+
self.mode = Enum__Fast_API__Service__Registry__Client__Mode.IN_MEMORY
184184
from fastapi.testclient import TestClient
185185
self._test_client = TestClient(self._app)
186186
187187
elif self._server: # Local server mode
188-
self.mode = Enum__Client__Mode.LOCAL_SERVER
188+
self.mode = Enum__Fast_API__Service__Registry__Client__Mode.LOCAL_SERVER
189189
from osbot_fast_api.utils.Fast_API_Server import Fast_API_Server
190190
if not isinstance(self._server, Fast_API_Server):
191191
self._server = Fast_API_Server(app=self._server)
192192
self._server.start()
193193
194194
else: # Remote mode
195-
self.mode = Enum__Client__Mode.REMOTE
195+
self.mode = Enum__Fast_API__Service__Registry__Client__Mode.REMOTE
196196
self._session = requests.Session()
197197
self._configure_session()
198198
@@ -209,9 +209,9 @@ def execute(self, method : str , #
209209
# Merge headers
210210
request_headers = {{**self.auth_headers(), **(headers or {{}})}}
211211
# Execute based on mode
212-
if self.mode == Enum__Client__Mode.IN_MEMORY:
212+
if self.mode == Enum__Fast_API__Service__Registry__Client__Mode.IN_MEMORY:
213213
response = self._execute_in_memory(method, path, body, request_headers)
214-
elif self.mode == Enum__Client__Mode.LOCAL_SERVER:
214+
elif self.mode == Enum__Fast_API__Service__Registry__Client__Mode.LOCAL_SERVER:
215215
response = self._execute_local_server(method, path, body, request_headers)
216216
else:
217217
response = self._execute_remote(method, path, body, request_headers)

osbot_fast_api/services/registry/Fast_API__Service__Registry.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# Central registry for service client discovery across deployment modes
44
# Enables zero-code-change switching between IN_MEMORY and REMOTE modes
55
# ═══════════════════════════════════════════════════════════════════════════════
6-
from osbot_fast_api.services.registry.Fast_API__Service__Registry__Client__Base import Fast_API__Service__Registry__Client__Base
7-
from osbot_fast_api.services.schemas.registry.collections.Dict__Fast_API__Service__Clients_By_Type import Dict__Fast_API__Service__Clients_By_Type
8-
from osbot_fast_api.services.schemas.registry.collections.List__Fast_API__Service__Client_Types import List__Fast_API__Service__Client_Types
9-
from osbot_utils.type_safe.Type_Safe import Type_Safe
10-
from osbot_utils.type_safe.type_safe_core.decorators.type_safe import type_safe
6+
from typing import Type
7+
from osbot_fast_api.services.registry.Fast_API__Service__Registry__Client__Base import Fast_API__Service__Registry__Client__Base
8+
from osbot_fast_api.services.schemas.registry.collections.Dict__Fast_API__Service__Clients_By_Type import Dict__Fast_API__Service__Clients_By_Type
9+
from osbot_fast_api.services.schemas.registry.collections.List__Fast_API__Service__Client_Types import List__Fast_API__Service__Client_Types
10+
from osbot_utils.type_safe.Type_Safe import Type_Safe
11+
from osbot_utils.type_safe.type_safe_core.decorators.type_safe import type_safe
1112

1213

1314
class Fast_API__Service__Registry(Type_Safe): # Instance-based registry for service clients
@@ -17,7 +18,9 @@ class Fast_API__Service__Registry(Type_Safe):
1718
def register(self, client: Fast_API__Service__Registry__Client__Base) -> None: # Register a client instance
1819
self.clients[type(client)] = client # Index by actual type
1920

20-
def client(self, client_type: type) -> Fast_API__Service__Registry__Client__Base: # Retrieve client by type
21+
@type_safe
22+
def client(self, client_type: Type[Fast_API__Service__Registry__Client__Base]
23+
) -> Fast_API__Service__Registry__Client__Base: # Retrieve client by type
2124
if client_type not in self.clients:
2225
return None # Not registered = None
2326
return self.clients[client_type] # Return registered client

osbot_fast_api/services/schemas/registry/Fast_API__Service__Registry__Client__Config.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
# Configuration schema shared by all service clients
44
# ═══════════════════════════════════════════════════════════════════════════════
55

6-
from fastapi import FastAPI
7-
from osbot_utils.type_safe.Type_Safe import Type_Safe
8-
from osbot_fast_api.services.schemas.registry.enums.Enum__Client__Mode import Enum__Client__Mode
9-
from osbot_fast_api.services.schemas.registry.safe_str.Safe_Str__Fast_API__Auth__Key_Name import Safe_Str__Fast_API__Auth__Key_Name
10-
from osbot_fast_api.services.schemas.registry.safe_str.Safe_Str__Fast_API__Auth__Key_Value import Safe_Str__Fast_API__Auth__Key_Value
11-
from osbot_utils.type_safe.primitives.domains.web.safe_str.Safe_Str__Url import Safe_Str__Url
6+
from fastapi import FastAPI
7+
from osbot_utils.type_safe.Type_Safe import Type_Safe
8+
from osbot_fast_api.services.schemas.registry.enums.Enum__Fast_API__Service__Registry__Client__Mode import Enum__Fast_API__Service__Registry__Client__Mode
9+
from osbot_fast_api.services.schemas.registry.safe_str.Safe_Str__Fast_API__Auth__Key_Name import Safe_Str__Fast_API__Auth__Key_Name
10+
from osbot_fast_api.services.schemas.registry.safe_str.Safe_Str__Fast_API__Auth__Key_Value import Safe_Str__Fast_API__Auth__Key_Value
11+
from osbot_utils.type_safe.primitives.domains.web.safe_str.Safe_Str__Url import Safe_Str__Url
1212

1313

1414
class Fast_API__Service__Registry__Client__Config(Type_Safe): # Pure data - client configuration
15-
mode : Enum__Client__Mode = None # IN_MEMORY or REMOTE (None = not configured)
16-
fast_api_app : FastAPI = None # FastAPI app for IN_MEMORY mode
17-
base_url : Safe_Str__Url = None # Base URL for REMOTE mode
18-
api_key_name : Safe_Str__Fast_API__Auth__Key_Name = None # HTTP header name for auth
19-
api_key_value : Safe_Str__Fast_API__Auth__Key_Value = None # HTTP header value for auth
15+
mode : Enum__Fast_API__Service__Registry__Client__Mode = None # IN_MEMORY or REMOTE (None = not configured)
16+
fast_api_app : FastAPI = None # FastAPI app for IN_MEMORY mode
17+
base_url : Safe_Str__Url = None # Base URL for REMOTE mode
18+
api_key_name : Safe_Str__Fast_API__Auth__Key_Name = None # HTTP header name for auth
19+
api_key_value : Safe_Str__Fast_API__Auth__Key_Value = None # HTTP header value for auth

osbot_fast_api/services/schemas/registry/collections/Dict__Fast_API__Service__Clients_By_Type.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# Dict__Fast_API__Service__Clients_By_Type
33
# Type-safe dictionary for storing service clients indexed by their class type
44
# ═══════════════════════════════════════════════════════════════════════════════
5+
from typing import Type
56
from osbot_fast_api.services.registry.Fast_API__Service__Registry__Client__Base import Fast_API__Service__Registry__Client__Base
67
from osbot_utils.type_safe.type_safe_core.collections.Type_Safe__Dict import Type_Safe__Dict
78

89

910
class Dict__Fast_API__Service__Clients_By_Type(Type_Safe__Dict): # Maps client type → client instance
10-
expected_key_type = type # The client class itself
11+
expected_key_type = Type[Fast_API__Service__Registry__Client__Base] # The client class itself
1112
expected_value_type = Fast_API__Service__Registry__Client__Base # Instance of client

osbot_fast_api/services/schemas/registry/enums/Enum__Client__Mode.py renamed to osbot_fast_api/services/schemas/registry/enums/Enum__Fast_API__Service__Registry__Client__Mode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# ═══════════════════════════════════════════════════════════════════════════════
2-
# Enum__Client__Mode
2+
# Enum__Fast_API__Service__Registry__Client__Mode
33
# Defines the transport mode for service clients (IN_MEMORY via TestClient or REMOTE via HTTP)
44
# ═══════════════════════════════════════════════════════════════════════════════
55

66
from enum import Enum
77

88

9-
class Enum__Client__Mode(Enum):
9+
class Enum__Fast_API__Service__Registry__Client__Mode(Enum):
1010
IN_MEMORY = 'in_memory' # Uses FastAPI TestClient (no network)
1111
REMOTE = 'remote' # Uses HTTP requests library

tests/unit/client/test_Client__Generator__AST.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test__generate_request_handler(self): #
110110

111111
assert code == AUTO_GEN__CODE__GENERATE_REQUEST_HANDLER
112112

113-
assert "class Enum__Client__Mode(str, Enum):" in code
113+
assert "class Enum__Fast_API__Service__Registry__Client__Mode(str, Enum):" in code
114114
assert "REMOTE = \"remote\"" in code
115115
assert "IN_MEMORY = \"in_memory\"" in code
116116
assert "LOCAL_SERVER = \"local_server\"" in code
@@ -513,7 +513,7 @@ def _create_test_contract(): #
513513
import requests
514514
from osbot_utils.type_safe.Type_Safe import Type_Safe
515515
516-
class Enum__Client__Mode(str, Enum):
516+
class Enum__Fast_API__Service__Registry__Client__Mode(str, Enum):
517517
REMOTE = "remote" # HTTP calls to deployed service
518518
IN_MEMORY = "in_memory" # FastAPI TestClient (same process)
519519
LOCAL_SERVER = "local_server" # Fast_API_Server (local HTTP)
@@ -528,7 +528,7 @@ class TestService__Client__Requests__Result(Type_Safe):
528528
529529
class TestService__Client__Requests(Type_Safe):
530530
config : Any # TestService__Client__Config
531-
mode : Enum__Client__Mode = Enum__Client__Mode.REMOTE
531+
mode : Enum__Fast_API__Service__Registry__Client__Mode = Enum__Fast_API__Service__Registry__Client__Mode.REMOTE
532532
_app : Optional[Any] = None # FastAPI app for in-memory
533533
_server : Optional[Any] = None # Fast_API_Server for local
534534
_test_client : Optional[Any] = None # TestClient for in-memory
@@ -541,19 +541,19 @@ def __init__(self, **kwargs):
541541
def _setup_mode(self): # Initialize the appropriate execution backend
542542
543543
if self._app: # In-memory mode with TestClient
544-
self.mode = Enum__Client__Mode.IN_MEMORY
544+
self.mode = Enum__Fast_API__Service__Registry__Client__Mode.IN_MEMORY
545545
from fastapi.testclient import TestClient
546546
self._test_client = TestClient(self._app)
547547
548548
elif self._server: # Local server mode
549-
self.mode = Enum__Client__Mode.LOCAL_SERVER
549+
self.mode = Enum__Fast_API__Service__Registry__Client__Mode.LOCAL_SERVER
550550
from osbot_fast_api.utils.Fast_API_Server import Fast_API_Server
551551
if not isinstance(self._server, Fast_API_Server):
552552
self._server = Fast_API_Server(app=self._server)
553553
self._server.start()
554554
555555
else: # Remote mode
556-
self.mode = Enum__Client__Mode.REMOTE
556+
self.mode = Enum__Fast_API__Service__Registry__Client__Mode.REMOTE
557557
self._session = requests.Session()
558558
self._configure_session()
559559
@@ -570,9 +570,9 @@ def execute(self, method : str , #
570570
# Merge headers
571571
request_headers = {**self.auth_headers(), **(headers or {})}
572572
# Execute based on mode
573-
if self.mode == Enum__Client__Mode.IN_MEMORY:
573+
if self.mode == Enum__Fast_API__Service__Registry__Client__Mode.IN_MEMORY:
574574
response = self._execute_in_memory(method, path, body, request_headers)
575-
elif self.mode == Enum__Client__Mode.LOCAL_SERVER:
575+
elif self.mode == Enum__Fast_API__Service__Registry__Client__Mode.LOCAL_SERVER:
576576
response = self._execute_local_server(method, path, body, request_headers)
577577
else:
578578
response = self._execute_remote(method, path, body, request_headers)

0 commit comments

Comments
 (0)