This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathregistry.py
More file actions
42 lines (31 loc) · 1.32 KB
/
registry.py
File metadata and controls
42 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from threading import Lock
from typing import Dict, Optional
from fastapi import FastAPI
from codegate.providers.base import BaseProvider
_provider_registry_lock = Lock()
_provider_registry_singleton: Optional["ProviderRegistry"] = None
def get_provider_registry(app: FastAPI = None) -> "ProviderRegistry":
global _provider_registry_singleton
if _provider_registry_singleton is None:
if app is None:
raise ValueError("Cannot initialize a ProviderRegistry without an app")
with _provider_registry_lock:
if _provider_registry_singleton is None:
_provider_registry_singleton = ProviderRegistry(app)
return _provider_registry_singleton
class ProviderRegistry:
def __init__(self, app: FastAPI):
self.app = app
self.providers: Dict[str, BaseProvider] = {}
def add_provider(self, name: str, provider: BaseProvider):
"""
Adds a provider to the registry. This will also add the provider's routes
to the FastAPI app.
"""
self.providers[name] = provider
self.app.include_router(provider.get_routes(), include_in_schema=False)
def get_provider(self, name: str) -> Optional[BaseProvider]:
"""
Retrieves a provider from the registry by name.
"""
return self.providers.get(name)