Service API Reference
Complete API reference for the service layer — Manager, Acquire, ServiceDiscovery, ServiceGenerator, BaseWebhook, and WebSocketManager.
This page provides structured API tables for every class in the Singularity service layer.
All signatures are taken directly from the source code in singularity/core/ and singularity/common/websocket.py.
Manager
services.__base.manager.Manager
The Manager is the central orchestrator. It scans for services and middlewares, registers routers with FastAPI, initializes RPC, and manages the application lifespan (startup/shutdown).
Constructor
appFastAPIrequired
prefixstr= "/api"
Attributes
appFastAPI
prefixstr
acquireAcquire
services_dirstr
mws_dirstr
ws_routesDict[str, Type]
Methods
register_services()None
register_middlewares()None
startup()None
shutdown()None
register_ws_routes(router, service_instance, service_name)None
register_services() also calls _init_rpc() internally, which registers all rpc_exposed services with the RPCRegistry. You do not need to call _init_rpc() yourself.
Acquire
services.__base.acquire.Acquire
The Acquire class is the dependency injection container. A single instance is created by the Manager and passed to every service whose __init__ accepts an acquire parameter.
Constructor
Takes no parameters. All resources are initialized internally.
Available Resources
db_sessionasync_sessionmaker
schemasdict
servicesdict
settingsSettings
utilsmodule
loggerloguru.Logger
cacheCache
deps_cachedeps_cache
ws_managerWebSocketManager
tasksTaskRunner
Services that do not need dependency injection can omit acquire from their __init__ signature entirely. The Manager will detect this and instantiate the service without it.
ServiceDiscovery
services.__base.discovery.ServiceDiscovery
Utility for discovering and inspecting services by scanning the services/ directory. Used by the CLI, not at runtime.
Constructor
Takes no parameters. Automatically resolves services_dir from its own file location.
Methods
discover_all(include_core)list[ServiceInfo]= include_core: bool = False
get_service_info(name)Optional[ServiceInfo]= name: str
build_dependency_graph(include_core)dict= include_core: bool = False
get_disabled_services()set[str]
disable_service(name)None= name: str
enable_service(name)None= name: str
is_disabled(name)bool= name: str
service_exists(name)bool= name: str
Dependency Graph Return Structure
The build_dependency_graph() method returns a dictionary with the following shape:
{
"nodes": [
{"name": "payments", "path": "v1/payments", "rpc_exposed": True, "is_webhook": False}
],
"edges": [
{"from": "orders", "to": "payments", "type": "rpc"}
],
"remote_edges": [
{"from": "orders", "to": "billing", "host_url": "http://...", "service_name": "billing", "type": "remote"}
],
"blocked": [
{"from": "payments", "blocked": "public", "type": "blacklist"}
]
}ServiceInfo
services.__base.discovery.ServiceInfo
A dataclass holding metadata about a discovered service. Returned by ServiceDiscovery.discover_all() and ServiceDiscovery.get_service_info().
Fields
namestrrequired
pathPathrequired
path_segmentslist[str]required
module_pathstrrequired
api_endpointstrrequired
class_namestrrequired
methodslist[str]= []
http_exposedlist[str]= []
rpc_exposedbool= False
blacklistlist[str]= []
docstringOptional[str]= None
is_disabledbool= False
is_corebool= False
is_webhookbool= False
webhook_eventslist[str]= []
rpc_dependencieslist[str]= []
remote_dependencieslist[dict[str, Any]]= []
ServiceGenerator
services.__base.generator.ServiceGenerator
Generator for creating new services with composable templates and mixins. Used by the services create CLI command.
Constructor
Takes no parameters. Resolves services_dir from its own file location.
Methods
validate_service_name(name)tuple[bool, Optional[str]]= name: str
generate_class_name(service_name)str= service_name: str
generate_template(...)str
create_service(...)Path
delete_service(name, hard)Path
generate_template() / create_service() Parameters
namestrrequired
descriptionstrrequired
basestr= "crud"
methodsOptional[list[str]]= None
rpcbool= False
websocketbool= False
authbool= False
dbbool= False
overwritebool= False
Base Templates
| crud | minimal | empty | webhook | |
|---|---|---|---|---|
| Methods | get, post, put, delete | get | (none) | (none) |
| Acquire | Yes | No | No | Yes |
| Use case | Full CRUD service | Read-only endpoint | Custom service shell | Webhook receiver |
BaseWebhook
services.__base.webhook.BaseWebhook
Base class for webhook receiver services. Handles raw body extraction, signature verification, event type extraction, and dispatch to user-defined handler methods.
Constructor
acquireAcquirerequired
Class Attributes
rpc_exposedbool= False
eventsdict[str, str]= {}
Methods
post(request)JSONResponse
verify(headers, raw_body)bool
get_event_type(headers, payload)str
on_unhandled_event(event_type, payload, headers)JSONResponse
Response Status Codes
| Scenario | Status Code | Body |
|---|---|---|
verify() returns False | 401 | {"error": "Verification failed"} |
| Invalid JSON body | 400 | {"error": "Invalid JSON payload"} |
| Unhandled event type | 200 | {"status": "ignored", "event": "<type>"} |
| Handler method missing from class | 500 | {"error": "Handler misconfigured"} |
| Handler raises exception | 500 | {"error": "Internal handler error"} |
| Handler succeeds | 200 | Handler return value |
BaseWebhook does not provide built-in idempotency. You are responsible for deduplicating events in your handler methods (e.g. by tracking processed event IDs in your database).
WebSocketManager
common.websocket.WebSocketManager
WebSocket connection manager with organization-level RBAC integration. Manages connection lifecycle and message broadcasting.
Constructor
Takes no parameters. Initializes empty connection and organization tracking dictionaries.
Internal Types
WebSocketConnection (dataclass)
websocketWebSocket
user_idUUID
org_idUUID
channel_idstr
permissionslist
WebSocketMessageType (Enum)
CONNECT"connect"
SUBSCRIBE"subscribe"
UNSUBSCRIBE"unsubscribe"
ERROR"error"
MESSAGE"message"
Methods
connect(websocket, org_id, user_id, permissions)None
disconnect(channel_id)None
broadcast(org_id, data, resource, required_action, exclude_channel)None
Middleware & Security
Middleware auto-discovery, the request processing chain, CORS configuration, rate limiting, exception handling, JWT authentication, and WebSocket security.
RPC API Reference
Complete API reference for the RPC subsystem — RPCRegistry, RPCProxy, ServiceProxy, Microservice descriptor, exception classes, and data models.