11# ═══════════════════════════════════════════════════════════════════════════════
22# Fast_API__Service__Registry
3- # Central registry for service client discovery across deployment modes
4- # Enables zero-code-change switching between IN_MEMORY and REMOTE modes
3+ # Central registry for service client configuration
4+ # Stores configs keyed by client type - clients are stateless facades
5+ # Supports save/restore and context managers for test isolation and hot-swapping
56# ═══════════════════════════════════════════════════════════════════════════════
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
127
8+ from contextlib import contextmanager
9+ from osbot_fast_api .services .schemas .registry .Fast_API__Service__Registry__Client__Config import Fast_API__Service__Registry__Client__Config
10+ from osbot_fast_api .services .schemas .registry .collections .Dict__Fast_API__Service__Configs_By_Type import Dict__Fast_API__Service__Configs_By_Type
11+ from osbot_fast_api .services .schemas .registry .collections .List__Fast_API__Service__Configs_Stack import List__Fast_API__Service__Configs_Stack
12+ from osbot_utils .type_safe .Type_Safe import Type_Safe
13+ from osbot_utils .type_safe .type_safe_core .decorators .type_safe import type_safe
1314
14- class Fast_API__Service__Registry (Type_Safe ): # Instance-based registry for service clients
15- clients : Dict__Fast_API__Service__Clients_By_Type # Type_Safe auto-creates this
1615
17- @type_safe
18- def register (self , client : Fast_API__Service__Registry__Client__Base ) -> None : # Register a client instance
19- self .clients [type (client )] = client # Index by actual type
16+ class Fast_API__Service__Registry (Type_Safe ): # Config store keyed by client type
17+ configs : Dict__Fast_API__Service__Configs_By_Type # Current configs (auto-created)
18+ configs_stack : List__Fast_API__Service__Configs_Stack # Stack for save/restore (auto-created)
19+
20+ # ───────────────────────────────────────────────────────────────────────────
21+ # Core Registry Operations
22+ # ───────────────────────────────────────────────────────────────────────────
2023
2124 @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
24- if client_type not in self .clients :
25- return None # Not registered = None
26- return self .clients [client_type ] # Return registered client
25+ def register (self , # Register config for a client type
26+ client_type : type ,
27+ config : Fast_API__Service__Registry__Client__Config
28+ ) -> None :
29+ self .configs [client_type ] = config
30+
31+ def config (self , client_type : type ) -> Fast_API__Service__Registry__Client__Config :
32+ if client_type not in self .configs : # Retrieve config by client type
33+ return None
34+ return self .configs [client_type ]
35+
36+ def is_registered (self , client_type : type ) -> bool :
37+ return client_type in self .configs
38+
39+ def clear (self ) -> None : # Reset configs (not stack)
40+ self .configs .clear ()
41+
42+ # ───────────────────────────────────────────────────────────────────────────
43+ # Save / Restore - For setUp/tearDown patterns
44+ # ───────────────────────────────────────────────────────────────────────────
45+
46+ def configs__save (self ) -> None : # Save current configs to stack
47+ snapshot = Dict__Fast_API__Service__Configs_By_Type ()
48+ snapshot .update (self .configs )
49+ self .configs_stack .append (snapshot )
50+
51+ def configs__restore (self ) -> None : # Restore configs from stack
52+ if len (self .configs_stack ) > 0 :
53+ saved = self .configs_stack .pop ()
54+ self .configs .clear ()
55+ self .configs .update (saved )
56+
57+ def configs__stack_size (self ) -> int : # Check stack depth
58+ return len (self .configs_stack )
2759
28- def is_registered (self , client_type : type ) -> bool : # Check if type is registered
29- return client_type in self .clients
60+ # ───────────────────────────────────────────────────────────────────────────
61+ # Context Managers - For clean test isolation and hot-swapping
62+ # ───────────────────────────────────────────────────────────────────────────
3063
31- def clear (self ) -> None : # Reset registry for test isolation
32- self .clients .clear ()
64+ @contextmanager
65+ def with_registry (self , registry : 'Fast_API__Service__Registry' ): # Temporarily use another registry's configs
66+ self .configs__save ()
67+ self .configs .clear ()
68+ self .configs .update (registry .configs )
69+ try :
70+ yield self
71+ finally :
72+ self .configs__restore ()
3373
34- def registered_types (self ) -> List__Fast_API__Service__Client_Types : # List all registered client types
35- return List__Fast_API__Service__Client_Types (list (self .clients .keys ()))
74+ @contextmanager
75+ def with_config (self , client_type : type , # Temporarily override single client's config
76+ config : Fast_API__Service__Registry__Client__Config ):
77+ self .configs__save ()
78+ self .configs [client_type ] = config
79+ try :
80+ yield self
81+ finally :
82+ self .configs__restore ()
3683
3784
38- fast_api__service__registry = Fast_API__Service__Registry () # Singleton instance for convenience
85+ fast_api__service__registry = Fast_API__Service__Registry () # Singleton instance for convenience
0 commit comments