totoms 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. totoms-1.0.0/LICENSE +21 -0
  2. totoms-1.0.0/MANIFEST.in +5 -0
  3. totoms-1.0.0/PKG-INFO +83 -0
  4. totoms-1.0.0/README.md +35 -0
  5. totoms-1.0.0/pyproject.toml +62 -0
  6. totoms-1.0.0/requirements.txt +10 -0
  7. totoms-1.0.0/setup.cfg +4 -0
  8. totoms-1.0.0/setup.py +35 -0
  9. totoms-1.0.0/totoms/TotoDelegateDecorator.py +134 -0
  10. totoms-1.0.0/totoms/TotoLogger.py +58 -0
  11. totoms-1.0.0/totoms/TotoMicroservice.py +280 -0
  12. totoms-1.0.0/totoms/TotoTokenVerifier.py +84 -0
  13. totoms-1.0.0/totoms/__init__.py +100 -0
  14. totoms-1.0.0/totoms/__pycache__/TotoDelegateDecorator.cpython-312.pyc +0 -0
  15. totoms-1.0.0/totoms/__pycache__/TotoLogger.cpython-312.pyc +0 -0
  16. totoms-1.0.0/totoms/__pycache__/TotoMicroservice.cpython-312.pyc +0 -0
  17. totoms-1.0.0/totoms/__pycache__/TotoTokenVerifier.cpython-312.pyc +0 -0
  18. totoms-1.0.0/totoms/__pycache__/__init__.cpython-312.pyc +0 -0
  19. totoms-1.0.0/totoms/api/APIControllerOptions.py +20 -0
  20. totoms-1.0.0/totoms/api/APIControllerProps.py +22 -0
  21. totoms-1.0.0/totoms/api/TotoAPIController.py +224 -0
  22. totoms-1.0.0/totoms/api/__init__.py +12 -0
  23. totoms-1.0.0/totoms/api/__pycache__/APIControllerOptions.cpython-312.pyc +0 -0
  24. totoms-1.0.0/totoms/api/__pycache__/APIControllerProps.cpython-312.pyc +0 -0
  25. totoms-1.0.0/totoms/api/__pycache__/TotoAPIController.cpython-312.pyc +0 -0
  26. totoms-1.0.0/totoms/api/__pycache__/__init__.cpython-312.pyc +0 -0
  27. totoms-1.0.0/totoms/evt/Interfaces.py +36 -0
  28. totoms-1.0.0/totoms/evt/MessageBusConfig.py +48 -0
  29. totoms-1.0.0/totoms/evt/MessageDestination.py +18 -0
  30. totoms-1.0.0/totoms/evt/TotoMessage.py +27 -0
  31. totoms-1.0.0/totoms/evt/TotoMessageBus.py +312 -0
  32. totoms-1.0.0/totoms/evt/TotoMessageHandler.py +79 -0
  33. totoms-1.0.0/totoms/evt/__init__.py +39 -0
  34. totoms-1.0.0/totoms/evt/__pycache__/Interfaces.cpython-312.pyc +0 -0
  35. totoms-1.0.0/totoms/evt/__pycache__/MessageBusConfig.cpython-312.pyc +0 -0
  36. totoms-1.0.0/totoms/evt/__pycache__/MessageDestination.cpython-312.pyc +0 -0
  37. totoms-1.0.0/totoms/evt/__pycache__/TotoMessage.cpython-312.pyc +0 -0
  38. totoms-1.0.0/totoms/evt/__pycache__/TotoMessageBus.cpython-312.pyc +0 -0
  39. totoms-1.0.0/totoms/evt/__pycache__/TotoMessageHandler.cpython-312.pyc +0 -0
  40. totoms-1.0.0/totoms/evt/__pycache__/__init__.cpython-312.pyc +0 -0
  41. totoms-1.0.0/totoms/evt/impl/GCPPubSub.py +144 -0
  42. totoms-1.0.0/totoms/evt/impl/SNS.py +223 -0
  43. totoms-1.0.0/totoms/evt/impl/__init__.py +9 -0
  44. totoms-1.0.0/totoms/evt/impl/__pycache__/GCPPubSub.cpython-312.pyc +0 -0
  45. totoms-1.0.0/totoms/evt/impl/__pycache__/SNS.cpython-312.pyc +0 -0
  46. totoms-1.0.0/totoms/evt/impl/__pycache__/__init__.cpython-312.pyc +0 -0
  47. totoms-1.0.0/totoms/model/ExecutionContext.py +15 -0
  48. totoms-1.0.0/totoms/model/Hyperscaler.py +4 -0
  49. totoms-1.0.0/totoms/model/PathOptions.py +20 -0
  50. totoms-1.0.0/totoms/model/TotoAPIEndpoint.py +13 -0
  51. totoms-1.0.0/totoms/model/TotoConfig.py +176 -0
  52. totoms-1.0.0/totoms/model/TotoEnvironment.py +28 -0
  53. totoms-1.0.0/totoms/model/UserContext.py +7 -0
  54. totoms-1.0.0/totoms/model/ValidationResult.py +29 -0
  55. totoms-1.0.0/totoms/model/__init__.py +27 -0
  56. totoms-1.0.0/totoms/model/__pycache__/ExecutionContext.cpython-312.pyc +0 -0
  57. totoms-1.0.0/totoms/model/__pycache__/Hyperscaler.cpython-312.pyc +0 -0
  58. totoms-1.0.0/totoms/model/__pycache__/PathOptions.cpython-312.pyc +0 -0
  59. totoms-1.0.0/totoms/model/__pycache__/TotoAPIEndpoint.cpython-312.pyc +0 -0
  60. totoms-1.0.0/totoms/model/__pycache__/TotoConfig.cpython-312.pyc +0 -0
  61. totoms-1.0.0/totoms/model/__pycache__/TotoEnvironment.cpython-312.pyc +0 -0
  62. totoms-1.0.0/totoms/model/__pycache__/UserContext.cpython-312.pyc +0 -0
  63. totoms-1.0.0/totoms/model/__pycache__/ValidationResult.cpython-312.pyc +0 -0
  64. totoms-1.0.0/totoms/model/__pycache__/__init__.cpython-312.pyc +0 -0
  65. totoms-1.0.0/totoms/model/singleton.py +11 -0
  66. totoms-1.0.0/totoms/py.typed +0 -0
  67. totoms-1.0.0/totoms/secrets/SecretsManager.py +80 -0
  68. totoms-1.0.0/totoms/secrets/__init__.py +5 -0
  69. totoms-1.0.0/totoms/secrets/__pycache__/SecretsManager.cpython-312.pyc +0 -0
  70. totoms-1.0.0/totoms/secrets/__pycache__/__init__.cpython-312.pyc +0 -0
  71. totoms-1.0.0/totoms.egg-info/PKG-INFO +83 -0
  72. totoms-1.0.0/totoms.egg-info/SOURCES.txt +73 -0
  73. totoms-1.0.0/totoms.egg-info/dependency_links.txt +1 -0
  74. totoms-1.0.0/totoms.egg-info/requires.txt +17 -0
  75. totoms-1.0.0/totoms.egg-info/top_level.txt +1 -0
totoms-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Toto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ include README.md
2
+ include LICENSE
3
+ include requirements.txt
4
+ recursive-include totoms *.py
5
+ recursive-include totoms py.typed
totoms-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.4
2
+ Name: totoms
3
+ Version: 1.0.0
4
+ Summary: Toto Microservice SDK - Python framework for building microservices
5
+ Home-page: https://github.com/nicolasances/toto-microservice-sdk
6
+ Author: Toto
7
+ Author-email: Toto <info@toto.com>
8
+ License: MIT
9
+ Project-URL: Homepage, https://github.com/nicolasances/toto-microservice-sdk
10
+ Project-URL: Repository, https://github.com/nicolasances/toto-microservice-sdk.git
11
+ Project-URL: Issues, https://github.com/nicolasances/toto-microservice-sdk/issues
12
+ Keywords: microservices,fastapi,cloud,message-bus,pubsub
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: License :: OSI Approved :: MIT License
20
+ Classifier: Operating System :: OS Independent
21
+ Classifier: Development Status :: 4 - Beta
22
+ Classifier: Intended Audience :: Developers
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
25
+ Requires-Python: >=3.8
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: fastapi>=0.68.0
29
+ Requires-Dist: uvicorn>=0.15.0
30
+ Requires-Dist: pydantic>=1.8.0
31
+ Requires-Dist: boto3>=1.17.0
32
+ Requires-Dist: google-cloud-pubsub>=2.5.0
33
+ Requires-Dist: google-cloud-storage>=1.42.0
34
+ Requires-Dist: google-cloud-secret-manager>=2.6.0
35
+ Requires-Dist: pyjwt>=2.1.0
36
+ Requires-Dist: cryptography>=3.4.0
37
+ Requires-Dist: python-dotenv>=0.19.0
38
+ Provides-Extra: dev
39
+ Requires-Dist: pytest>=6.2.0; extra == "dev"
40
+ Requires-Dist: pytest-asyncio>=0.18.0; extra == "dev"
41
+ Requires-Dist: black>=21.0; extra == "dev"
42
+ Requires-Dist: flake8>=3.9.0; extra == "dev"
43
+ Requires-Dist: isort>=5.9.0; extra == "dev"
44
+ Dynamic: author
45
+ Dynamic: home-page
46
+ Dynamic: license-file
47
+ Dynamic: requires-python
48
+
49
+ # Toto Microservice SDK - Python
50
+
51
+ Python framework for building cloud-agnostic microservices with FastAPI.
52
+
53
+ ## Installation
54
+
55
+ ```bash
56
+ pip install totoms
57
+ ```
58
+
59
+ ## Quick Start
60
+
61
+ ```python
62
+ from totoms import TotoMicroservice, TotoMicroserviceConfiguration
63
+
64
+ config = TotoMicroserviceConfiguration()
65
+ microservice = TotoMicroservice(config)
66
+ ```
67
+
68
+ ## Features
69
+
70
+ - **API Controller**: FastAPI-based REST API framework
71
+ - **Message Bus**: Event-driven architecture with PubSub and Queue support
72
+ - **Cloud Support**: AWS, GCP, and Azure
73
+ - **Secrets Management**: Secure secrets handling
74
+ - **Token Verification**: Built-in JWT token verification
75
+ - **Logging**: Structured logging with correlation IDs
76
+
77
+ ## Documentation
78
+
79
+ For full documentation, visit the [Toto SDK repository](https://github.com/nicolasances/toto-microservice-sdk).
80
+
81
+ ## License
82
+
83
+ MIT License - see LICENSE file for details
totoms-1.0.0/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Toto Microservice SDK - Python
2
+
3
+ Python framework for building cloud-agnostic microservices with FastAPI.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install totoms
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```python
14
+ from totoms import TotoMicroservice, TotoMicroserviceConfiguration
15
+
16
+ config = TotoMicroserviceConfiguration()
17
+ microservice = TotoMicroservice(config)
18
+ ```
19
+
20
+ ## Features
21
+
22
+ - **API Controller**: FastAPI-based REST API framework
23
+ - **Message Bus**: Event-driven architecture with PubSub and Queue support
24
+ - **Cloud Support**: AWS, GCP, and Azure
25
+ - **Secrets Management**: Secure secrets handling
26
+ - **Token Verification**: Built-in JWT token verification
27
+ - **Logging**: Structured logging with correlation IDs
28
+
29
+ ## Documentation
30
+
31
+ For full documentation, visit the [Toto SDK repository](https://github.com/nicolasances/toto-microservice-sdk).
32
+
33
+ ## License
34
+
35
+ MIT License - see LICENSE file for details
@@ -0,0 +1,62 @@
1
+ [build-system]
2
+ requires = ["setuptools>=45", "wheel", "setuptools-scm[toml]>=6.2"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "totoms"
7
+ version = "1.0.0"
8
+ description = "Toto Microservice SDK - Python framework for building microservices"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = {text = "MIT"}
12
+ authors = [
13
+ {name = "Toto", email = "info@toto.com"},
14
+ ]
15
+ keywords = ["microservices", "fastapi", "cloud", "message-bus", "pubsub"]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.8",
19
+ "Programming Language :: Python :: 3.9",
20
+ "Programming Language :: Python :: 3.10",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "License :: OSI Approved :: MIT License",
24
+ "Operating System :: OS Independent",
25
+ "Development Status :: 4 - Beta",
26
+ "Intended Audience :: Developers",
27
+ "Topic :: Software Development :: Libraries :: Python Modules",
28
+ "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
29
+ ]
30
+
31
+ dependencies = [
32
+ "fastapi>=0.68.0",
33
+ "uvicorn>=0.15.0",
34
+ "pydantic>=1.8.0",
35
+ "boto3>=1.17.0",
36
+ "google-cloud-pubsub>=2.5.0",
37
+ "google-cloud-storage>=1.42.0",
38
+ "google-cloud-secret-manager>=2.6.0",
39
+ "pyjwt>=2.1.0",
40
+ "cryptography>=3.4.0",
41
+ "python-dotenv>=0.19.0",
42
+ ]
43
+
44
+ [project.optional-dependencies]
45
+ dev = [
46
+ "pytest>=6.2.0",
47
+ "pytest-asyncio>=0.18.0",
48
+ "black>=21.0",
49
+ "flake8>=3.9.0",
50
+ "isort>=5.9.0",
51
+ ]
52
+
53
+ [project.urls]
54
+ Homepage = "https://github.com/nicolasances/toto-microservice-sdk"
55
+ Repository = "https://github.com/nicolasances/toto-microservice-sdk.git"
56
+ Issues = "https://github.com/nicolasances/toto-microservice-sdk/issues"
57
+
58
+ [tool.setuptools]
59
+ packages = ["totoms"]
60
+
61
+ [tool.setuptools.package-data]
62
+ totoms = ["py.typed"]
@@ -0,0 +1,10 @@
1
+ fastapi>=0.68.0
2
+ uvicorn>=0.15.0
3
+ pydantic>=1.8.0
4
+ boto3>=1.17.0
5
+ google-cloud-pubsub>=2.5.0
6
+ google-cloud-storage>=1.42.0
7
+ google-cloud-secret-manager>=2.6.0
8
+ pyjwt>=2.1.0
9
+ cryptography>=3.4.0
10
+ python-dotenv>=0.19.0
totoms-1.0.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
totoms-1.0.0/setup.py ADDED
@@ -0,0 +1,35 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ with open("README.md", "r", encoding="utf-8") as fh:
4
+ long_description = fh.read()
5
+
6
+ with open("requirements.txt", "r", encoding="utf-8") as fh:
7
+ requirements = [line.strip() for line in fh if line.strip() and not line.startswith("#")]
8
+
9
+ setup(
10
+ name="totoms",
11
+ version="1.0.0",
12
+ author="Toto",
13
+ description="Toto Microservice SDK - Python framework for building microservices",
14
+ long_description=long_description,
15
+ long_description_content_type="text/markdown",
16
+ url="https://github.com/nicolasances/toto-microservice-sdk",
17
+ packages=find_packages(),
18
+ classifiers=[
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.8",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "License :: OSI Approved :: MIT License",
26
+ "Operating System :: OS Independent",
27
+ "Development Status :: 4 - Beta",
28
+ "Intended Audience :: Developers",
29
+ "Topic :: Software Development :: Libraries :: Python Modules",
30
+ "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
31
+ ],
32
+ python_requires=">=3.8",
33
+ install_requires=requirements,
34
+ include_package_data=True,
35
+ )
@@ -0,0 +1,134 @@
1
+ from fastapi import Request
2
+ from totoms.TotoLogger import TotoLogger
3
+ from totoms.TotoMicroservice import TotoMicroservice
4
+ from totoms.TotoTokenVerifier import TotoTokenVerifier
5
+ from totoms.model.ExecutionContext import ExecutionContext
6
+ from totoms.model.TotoConfig import TotoControllerConfig
7
+ from totoms.model.UserContext import UserContext
8
+ from totoms.model.ValidationResult import ValidationResult
9
+
10
+ def toto_delegate(dlg):
11
+ """Creates a decorator for a Toto Delegate function
12
+
13
+ Args:
14
+ dlg (callable): a function to decorate, that needs to have a signature f(request: Request, user_context: UserContext, exec_context: ExecutionContext)
15
+ """
16
+
17
+ async def decorator(request: Request):
18
+ """Decorator for a Toto Delegate function
19
+
20
+ This decorator performs the following operations:
21
+ 1. Validates mandatory headers: x-correlation-id and Authorization header
22
+ 2. Validates JWT token passed as Bearer token in the Authorization header
23
+ 3. Creates the User Context to pass it to the delegate
24
+ 4. Creates the Execution Context to pass it to the delegate
25
+
26
+ Args:
27
+ request (Request): FastAPI Request object
28
+
29
+ Returns:
30
+ any: the returned value from the decorated function or a validation error
31
+ """
32
+
33
+ microservice = TotoMicroservice.get_instance()
34
+
35
+ config: TotoControllerConfig = microservice.config
36
+ logger = TotoLogger.get_instance()
37
+ message_bus = microservice.message_bus
38
+
39
+ # Extract info
40
+ cid, _ = await extract_info(request)
41
+
42
+ # Validate the request
43
+ validation_result = await validate_request(request, config)
44
+
45
+ if not validation_result.validation_passed:
46
+ return validation_result.to_fastapi_response()
47
+
48
+ # Log the incoming call
49
+ logger.log(cid, f"Incoming API Call: {request.method} {request.url.path}")
50
+
51
+ # Create a user context object
52
+ user_context = UserContext(validation_result.token_verification_result.user_email)
53
+
54
+ # Create an execution context object
55
+ execution_context = ExecutionContext(
56
+ logger=logger,
57
+ cid=cid,
58
+ config=config,
59
+ message_bus=message_bus,
60
+ environment=microservice.microservice_configuration.environment
61
+ )
62
+
63
+ # Call the delegate
64
+ return await dlg(request, user_context, execution_context)
65
+
66
+ return decorator
67
+
68
+
69
+ async def extract_info(request: Request) :
70
+ """Extracts needed info from the request
71
+
72
+ Returns cid and auth header
73
+ """
74
+
75
+ # Extract cid
76
+ cid = request.headers.get("x-correlation-id")
77
+
78
+ # Extract Authorization header
79
+ auth_header = request.headers.get("Authorization")
80
+
81
+ return cid, auth_header
82
+
83
+ async def validate_request(request: Request, config: TotoControllerConfig) -> ValidationResult:
84
+ """ Validates the core request data that is mandatory for any call
85
+
86
+ Args:
87
+ request (Request): the HTTP request
88
+
89
+ Returns:
90
+ ValidationResult: the result of the validation. The flag "validation_passed" will indicate whether the validation was successfull of not
91
+ """
92
+ # Extract the path from the request
93
+ path = request.url.path
94
+
95
+ # Extract needed info
96
+ cid, auth_header = await extract_info(request)
97
+
98
+ # Verify that the Correlation Id was provided
99
+ if not cid:
100
+ # Check if paths are excluded in the config file
101
+ if not config.is_path_excluded(request.url.path):
102
+ return throw_validation_error(cid, 400, "No correlation id header provided in the Request")
103
+
104
+ # Verify that an Authorization header was provided
105
+ if not auth_header:
106
+ return throw_validation_error(cid, 400, "No Authorization header provided in the Request")
107
+
108
+ # Verify that the Authorization header contains a Bearer Token
109
+ auth_header_tokens = auth_header.split()
110
+
111
+ if auth_header_tokens[0] != 'Bearer':
112
+ return throw_validation_error(cid, 400, "Authorization header does not contain a Bearer token")
113
+
114
+ # Verify the token
115
+ token_verification = TotoTokenVerifier(config, cid = cid).verify_token(auth_header_tokens[1])
116
+
117
+ if token_verification.code != 200:
118
+ return throw_validation_error(cid, token_verification.code, token_verification.message)
119
+
120
+ return ValidationResult(True, token_verification_result=token_verification)
121
+
122
+
123
+ def throw_validation_error(cid: str, code: int, message: str, additional_log: str = None, logger: TotoLogger = TotoLogger(None)):
124
+ """ Generates a validation error
125
+
126
+ Args:
127
+ cid (str): the Correlation Id
128
+ code (int): the HTTP Error Code to use
129
+ message (str): the Message to both log and provide back to the caller
130
+ additional_log (str): a log message to override the default log (e.g. for sensitivity or security reasons)
131
+ """
132
+ logger.log(cid, additional_log if additional_log is not None else message)
133
+
134
+ return ValidationResult(False, error_code = code, error_message = message, cid = cid)
@@ -0,0 +1,58 @@
1
+ """
2
+ TotoLogger - Singleton logger for Toto microservices.
3
+ """
4
+ from datetime import datetime
5
+ from typing import Optional
6
+
7
+
8
+ class TotoLogger:
9
+ """
10
+ Singleton logger for Toto microservices.
11
+
12
+ Usage:
13
+ logger = TotoLogger.get_instance("my-service")
14
+ logger.log("INIT", "Service started")
15
+ """
16
+
17
+ _instance: Optional['TotoLogger'] = None
18
+
19
+ def __init__(self, api_name: str = "") -> None:
20
+ """
21
+ Initialize the logger.
22
+
23
+ Args:
24
+ api_name: The name of the API/service for logging
25
+ """
26
+ self.api_name = api_name
27
+
28
+ @classmethod
29
+ def get_instance(cls, api_name: str = "") -> 'TotoLogger':
30
+ """
31
+ Get or create the singleton instance.
32
+
33
+ Args:
34
+ api_name: The name of the API/service (used on first initialization)
35
+
36
+ Returns:
37
+ The singleton TotoLogger instance
38
+ """
39
+ if cls._instance is None:
40
+ cls._instance = cls(api_name or "unknown")
41
+ return cls._instance
42
+
43
+ def log(self, correlation_id: str, msg: str) -> None:
44
+ """
45
+ Log a message to the console.
46
+
47
+ Args:
48
+ correlation_id: The correlation ID (request ID, operation ID, etc.)
49
+ msg: The message to log
50
+ """
51
+ # Get the current timestamp
52
+ current_timestamp = datetime.now()
53
+
54
+ # Format it
55
+ formatted_timestamp = current_timestamp.strftime('%Y.%m.%d %H:%M:%S,%f')[:-3]
56
+
57
+ # Log
58
+ print(f"[{self.api_name}] - [{correlation_id}] - [{formatted_timestamp}] - {msg}")