fdc-shared-kernel 0.0.25__tar.gz → 0.0.26__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 (59) hide show
  1. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/PKG-INFO +1 -1
  2. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/fdc_shared_kernel.egg-info/PKG-INFO +1 -1
  3. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/pyproject.toml +1 -1
  4. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/interfaces/databus.py +20 -20
  5. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/messaging/__init__.py +2 -2
  6. fdc_shared_kernel-0.0.26/shared_kernel/messaging/http_databus.py +125 -0
  7. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/messaging/nats_databus.py +46 -41
  8. fdc_shared_kernel-0.0.25/shared_kernel/messaging/http_databus.py +0 -82
  9. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/README.md +0 -0
  10. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/README_pypi.md +0 -0
  11. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/fdc_shared_kernel.egg-info/SOURCES.txt +0 -0
  12. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/fdc_shared_kernel.egg-info/dependency_links.txt +0 -0
  13. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/fdc_shared_kernel.egg-info/requires.txt +0 -0
  14. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/fdc_shared_kernel.egg-info/top_level.txt +0 -0
  15. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/requirements.txt +0 -0
  16. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/setup.cfg +0 -0
  17. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/__init__.py +0 -0
  18. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/auth/__init__.py +0 -0
  19. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/auth/jwt_helper.py +0 -0
  20. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/auth/token_handler.py +0 -0
  21. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/config/__init__.py +0 -0
  22. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/database/__init__.py +0 -0
  23. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/__init__.py +0 -0
  24. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/configuration_exceptions.py +0 -0
  25. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/custom_exceptions.py +0 -0
  26. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/data_validation_exceptions.py +0 -0
  27. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/http_exceptions.py +0 -0
  28. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/infrastructure_exceptions.py +0 -0
  29. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/operational_exceptions.py +0 -0
  30. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/exceptions/security_exceptions.py +0 -0
  31. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/http/__init__.py +0 -0
  32. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/http/httpx_http_client.py +0 -0
  33. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/http/request_http_client.py +0 -0
  34. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/interfaces/__init__.py +0 -0
  35. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/interfaces/http.py +0 -0
  36. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/interfaces/keyvault.py +0 -0
  37. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/logger/__init__.py +0 -0
  38. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/metrics/__init__.py +0 -0
  39. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/metrics/status_tracker.py +0 -0
  40. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/models/__init__.py +0 -0
  41. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/security/__init__.py +0 -0
  42. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/security/key_vault/__init__.py +0 -0
  43. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/security/key_vault/aws_secret_manager.py +0 -0
  44. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/security/key_vault/azure_keyvault.py +0 -0
  45. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/__init__.py +0 -0
  46. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/config/__init__.py +0 -0
  47. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/config/test_config.py +0 -0
  48. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/logger/__init__.py +0 -0
  49. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/logger/test_logger.py +0 -0
  50. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/messaging/__init__.py +0 -0
  51. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/messaging/test_nats_interface.py +0 -0
  52. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/utils/__init__.py +0 -0
  53. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/utils/test_data_validators.py +0 -0
  54. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/utils/test_date_format_utils.py +0 -0
  55. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/tests/utils/test_string_utils.py +0 -0
  56. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/utils/__init__.py +0 -0
  57. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/utils/data_validators_utils.py +0 -0
  58. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/utils/date_format_utils.py +0 -0
  59. {fdc_shared_kernel-0.0.25 → fdc_shared_kernel-0.0.26}/shared_kernel/utils/string_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fdc_shared_kernel
3
- Version: 0.0.25
3
+ Version: 0.0.26
4
4
  Summary: Shared library for microservice
5
5
  Author-email: Shikhil S <shikhil.s@dbizsolution.com>, Ahammed Akdham N <ahammedakdham.n@dbizsolution.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fdc_shared_kernel
3
- Version: 0.0.25
3
+ Version: 0.0.26
4
4
  Summary: Shared library for microservice
5
5
  Author-email: Shikhil S <shikhil.s@dbizsolution.com>, Ahammed Akdham N <ahammedakdham.n@dbizsolution.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "fdc_shared_kernel"
7
- version = "0.0.25"
7
+ version = "0.0.26"
8
8
  requires-python = ">=3.7"
9
9
  readme = "README_pypi.md"
10
10
  description = "Shared library for microservice"
@@ -17,17 +17,17 @@ class DataBus(ABC):
17
17
  close_connection():
18
18
  Closes the connection to the DataBus server.
19
19
 
20
- publish_event(topic: str, event_payload: dict):
21
- Publishes an asynchronous message to a DataBus topic.
20
+ publish_event(event_name: str, event_payload: dict):
21
+ Publishes an asynchronous message to a DataBus event_name.
22
22
 
23
- request_event(topic: str, event_payload: dict) -> Any:
24
- Sends a synchronous request/message to a DataBus topic and receives a response.
23
+ request_event(event_name: str, event_payload: dict) -> Any:
24
+ Sends a synchronous request/message to a DataBus event_name and receives a response.
25
25
 
26
- subscribe_sync_event(topic: str, callback: Callable[[Any], None]):
27
- Subscribes to a DataBus topic and processes messages synchronously.
26
+ subscribe_sync_event(event_name: str, callback: Callable[[Any], None]):
27
+ Subscribes to a DataBus event_name and processes messages synchronously.
28
28
 
29
- subscribe_async_event(topic: str, callback: Callable[[Any], None]):
30
- Subscribes to a DataBus topic and processes messages asynchronously.
29
+ subscribe_async_event(event_name: str, callback: Callable[[Any], None]):
30
+ Subscribes to a DataBus event_name and processes messages asynchronously.
31
31
 
32
32
  delete_message(message_id: str):
33
33
  Deletes a message from the DataBus.
@@ -55,23 +55,23 @@ class DataBus(ABC):
55
55
  pass
56
56
 
57
57
  @abstractmethod
58
- def publish_event(self, topic: str, event_payload: dict):
58
+ def publish_event(self, event_name: str, event_payload: dict):
59
59
  """
60
- Publish an asynchronous message to a DataBus topic.
60
+ Publish an asynchronous message to a DataBus event_name.
61
61
 
62
62
  Parameters:
63
- - topic (str): The topic to publish the message to.
63
+ - event_name (str): The event name to publish the message to.
64
64
  - event_payload (dict): The message to be published.
65
65
  """
66
66
  pass
67
67
 
68
68
  @abstractmethod
69
- def request_event(self, topic: str, event_payload: dict) -> Any:
69
+ def request_event(self, event_name: str, event_payload: dict) -> Any:
70
70
  """
71
- Send a synchronous request/message to a DataBus topic and receive a response.
71
+ Send a synchronous request/message to a DataBus event name and receive a response.
72
72
 
73
73
  Parameters:
74
- - topic (str): The topic to send the message to.
74
+ - event_name (str): The event name to send the message to.
75
75
  - event_payload (dict): The message to be sent.
76
76
 
77
77
  Returns:
@@ -80,23 +80,23 @@ class DataBus(ABC):
80
80
  pass
81
81
 
82
82
  @abstractmethod
83
- def subscribe_sync_event(self, topic: str, callback: Callable[[Any], None]):
83
+ def subscribe_sync_event(self, event_name: str, callback: Callable[[Any], None]):
84
84
  """
85
- Subscribe to a DataBus topic and process messages synchronously.
85
+ Subscribe to a DataBus event name and process messages synchronously.
86
86
 
87
87
  Parameters:
88
- - topic (str): The topic to subscribe to.
88
+ - event_name (str): The event name to subscribe to.
89
89
  - callback (Callable[[Any], None]): A callback function to handle received messages.
90
90
  """
91
91
  pass
92
92
 
93
93
  @abstractmethod
94
- def subscribe_async_event(self, topic: str, callback: Callable[[Any], None]):
94
+ def subscribe_async_event(self, event_name: str, callback: Callable[[Any], None]):
95
95
  """
96
- Subscribe to a DataBus topic and process messages asynchronously.
96
+ Subscribe to a DataBus event name and process messages asynchronously.
97
97
 
98
98
  Parameters:
99
- - topic (str): The topic to subscribe to.
99
+ - event_name (str): The event name to subscribe to.
100
100
  - callback (Callable[[Any], None]): A callback function to handle received messages.
101
101
  """
102
102
  pass
@@ -1,10 +1,10 @@
1
1
  from shared_kernel.messaging.nats_databus import NATSDataBus
2
- from shared_kernel.messaging.http_databus import HTTPDatabus
2
+ from shared_kernel.messaging.http_databus import HTTPDataBus
3
3
  from shared_kernel.interfaces import DataBus
4
4
 
5
5
 
6
6
  class DataBus:
7
- data_bus_classes = {"NATS": NATSDataBus, "HTTP": HTTPDatabus}
7
+ data_bus_classes = {"NATS": NATSDataBus, "HTTP": HTTPDataBus}
8
8
 
9
9
  @staticmethod
10
10
  def create_data_bus(bus_type: str, config: dict) -> DataBus:
@@ -0,0 +1,125 @@
1
+ import asyncio
2
+ import logging
3
+ from typing import Callable, Any, Dict
4
+ from shared_kernel.interfaces import DataBus
5
+ from concurrent.futures import ThreadPoolExecutor
6
+ from shared_kernel.http import HttpClient
7
+
8
+ logging.getLogger().setLevel(logging.INFO)
9
+
10
+
11
+ class HTTPDataBus(DataBus):
12
+ """
13
+ A class to handle HTTP-based event operations, including EventBridge and SQS.
14
+ """
15
+
16
+ _instance = None
17
+
18
+ def __new__(cls, *args, **kwargs):
19
+ if cls._instance is None:
20
+ cls._instance = super(HTTPDataBus, cls).__new__(cls)
21
+ return cls._instance
22
+
23
+ def __init__(self, config: Dict = None):
24
+ """
25
+ Initialize the HTTPDataBus.
26
+
27
+ Args:
28
+ config (Dict): A dictionary containing the HTTP client configuration.
29
+ """
30
+ if not hasattr(self, "initialized"): # Prevent reinitialization
31
+ super().__init__()
32
+ self.http_client = HttpClient().create_client()
33
+ self.initialized = True
34
+
35
+ async def publish_event(self, event_name: str, event_payload: dict):
36
+ """
37
+ Send an event payload to multiple HTTP endpoints and return the responses.
38
+
39
+ Args:
40
+ event_name (str): The URLs to invoke the HTTP endpoints.
41
+ event_payload (dict): The payload containing all necessary information for the HTTP requests.
42
+
43
+ Returns:
44
+ None
45
+ """
46
+ with ThreadPoolExecutor(max_workers=5) as executor:
47
+ futures = [executor.submit(self.http_client.post, url=url, json=event_payload) for url in event_name]
48
+ for future in futures:
49
+ future.result()
50
+
51
+ def subscribe_async_event(self, event_name: str, callback: Callable[[Any], None]):
52
+ """
53
+ Subscribe to asynchronous events and invoke the provided callback upon receiving an event.
54
+
55
+ Args:
56
+ event_name (str): The event name or URL to subscribe to.
57
+ callback (Callable[[Any], None]): The callback function to invoke with the event data.
58
+
59
+ Returns:
60
+ asyncio.Task: The task handling the asynchronous event subscription.
61
+ """
62
+ return asyncio.create_task(callback(event_name))
63
+
64
+ def request_event(self, event_name: str, event_payload: dict) -> Any:
65
+ """
66
+ Send an event payload to an HTTP endpoint and return the response.
67
+
68
+ Args:
69
+ event_name (str): The URL to invoke the HTTP endpoint.
70
+ event_payload (dict): The payload containing all necessary information for the HTTP request.
71
+
72
+ Returns:
73
+ Any: The response from the HTTP endpoint.
74
+ """
75
+ response = self.http_client.post(url=event_name, json=event_payload)
76
+ return response
77
+
78
+ def subscribe_sync_event(self, event_name: str, callback: Callable[[Any], None]):
79
+ """
80
+ Subscribe to synchronous events and invoke the provided callback upon receiving an event.
81
+
82
+ Args:
83
+ event_name (str): The event name or URL to subscribe to.
84
+ callback (Callable[[Any], None]): The callback function to invoke with the event data.
85
+
86
+ Returns:
87
+ Any: The result of the callback function.
88
+ """
89
+ return callback(event_name)
90
+
91
+ def delete_message(self, receipt_handle: str):
92
+ """
93
+ Placeholder method for deleting a message.
94
+
95
+ Args:
96
+ receipt_handle (str): The receipt handle of the message to be deleted.
97
+
98
+ Returns:
99
+ None
100
+ """
101
+ pass
102
+
103
+ def make_connection(self, receipt_handle: str):
104
+ """
105
+ Placeholder method for establishing a connection.
106
+
107
+ Args:
108
+ receipt_handle (str): The receipt handle related to the connection.
109
+
110
+ Returns:
111
+ None
112
+ """
113
+ pass
114
+
115
+ def close_connection(self, receipt_handle: str):
116
+ """
117
+ Placeholder method for closing a connection.
118
+
119
+ Args:
120
+ receipt_handle (str): The receipt handle related to the connection.
121
+
122
+ Returns:
123
+ None
124
+ """
125
+ pass
@@ -10,7 +10,7 @@ logging.getLogger().setLevel(logging.INFO)
10
10
 
11
11
  class NATSDataBus(DataBus):
12
12
  """
13
- A NATS Interface class to handle both standard NATS and JetStream operations.
13
+ A NATS interface class to handle both standard NATS and JetStream operations.
14
14
  """
15
15
 
16
16
  _instance = None
@@ -22,12 +22,12 @@ class NATSDataBus(DataBus):
22
22
 
23
23
  def __init__(self, config: Dict = None):
24
24
  """
25
- Initialize the NATSInterface.
25
+ Initialize the NATSDataBus.
26
26
 
27
27
  Args:
28
- config (Dict): A Dict containing the NATS config.
28
+ config (Dict): A dictionary containing the NATS configuration.
29
29
  """
30
- if not hasattr(self, "initialized"): # to prevent reinitialization
30
+ if not hasattr(self, "initialized"): # Prevent reinitialization
31
31
  super().__init__()
32
32
  self.nc = NATS()
33
33
  self.servers = config.get('servers')
@@ -37,20 +37,21 @@ class NATSDataBus(DataBus):
37
37
  self.js = None # JetStream context
38
38
  self.initialized = True
39
39
  self.stream_name = config.get('stream_name', '')
40
- self.topics = config.get('topics', [])
40
+ self.event_names = config.get('event_names', [])
41
41
 
42
- if self.stream_name:
43
- if self.topics:
44
- self.create_stream(self.stream_name, self.topics)
42
+ if self.stream_name and self.event_names:
43
+ self.create_stream(self.stream_name, self.event_names)
45
44
 
46
45
  async def make_connection(self):
47
46
  """
48
47
  Connect to the NATS server.
49
48
  """
50
49
  if not self.connected:
51
- await self.nc.connect(servers=self.servers,
52
- user=self.user,
53
- password=self.password)
50
+ await self.nc.connect(
51
+ servers=self.servers,
52
+ user=self.user,
53
+ password=self.password
54
+ )
54
55
  self.js = self.nc.jetstream(timeout=10)
55
56
  self.connected = True
56
57
 
@@ -58,19 +59,20 @@ class NATSDataBus(DataBus):
58
59
  """
59
60
  Close the connection to the NATS server.
60
61
  """
61
- try:
62
- if self.connected:
62
+ if self.connected:
63
+ try:
63
64
  await self.nc.close()
64
65
  self.connected = False
65
- except Exception as e:
66
- raise e
66
+ except Exception as e:
67
+ raise e
67
68
 
68
- async def create_stream(self, stream_name: str, topics: List[Any]):
69
+ async def create_stream(self, stream_name: str, event_names: List[Any]):
69
70
  """
70
- Create a stream for topics to persist the messages
71
+ Create a stream for event names to persist the messages.
71
72
 
72
73
  Args:
73
- topics (List): The messages in this topic with be persisted.
74
+ stream_name (str): The name of the stream.
75
+ event_names (List[Any]): The subjects whose messages will be persisted.
74
76
  """
75
77
  try:
76
78
  self.stream_name = stream_name
@@ -81,40 +83,41 @@ class NATSDataBus(DataBus):
81
83
  # Stream does not exist, so create it
82
84
  stream_config = StreamConfig(
83
85
  name=stream_name,
84
- subjects=topics,
85
- max_age=600, # retain messages for 10 mins
86
+ subjects=event_names,
87
+ max_age=600 # Retain messages for 10 minutes
86
88
  )
87
89
  await self.js.add_stream(stream_config)
88
- logging.info(f"Stream created :: {stream_name}")
90
+ logging.info(f"Stream '{stream_name}' created.")
89
91
 
90
92
  async def publish_event(
91
- self, topic: str, event_payload: dict
93
+ self, event_name: str, event_payload: dict
92
94
  ) -> Union[bool, Exception]:
93
95
  """
94
96
  Publish a message to a JetStream subject.
95
97
 
96
98
  Args:
97
- topic (str): The topic to publish the message to.
99
+ event_name (str): The subject to publish the message to.
98
100
  event_payload (dict): The message to be published.
99
101
 
100
102
  Returns:
101
103
  bool: True if the event was published successfully.
102
104
  """
103
105
  ack = await self.js.publish(
104
- topic, json.dumps(event_payload).encode("utf-8")
106
+ event_name, json.dumps(event_payload).encode("utf-8")
105
107
  )
106
108
  logging.info(
107
- f"Published event '{event_payload.get('event_name')}' to topic '{topic}', ack: {ack}"
109
+ f"Published event '{event_payload.get('event_name')}' to subject '{event_name}', ack: {ack}"
108
110
  )
111
+ return True
109
112
 
110
113
  async def request_event(
111
- self, topic: str, event_payload: str, timeout: float = 10.0
114
+ self, event_name: str, event_payload: dict, timeout: float = 10.0
112
115
  ) -> Union[dict, Exception]:
113
116
  """
114
117
  Send a request and wait for a response.
115
118
 
116
119
  Args:
117
- topic (str): The topic to publish the message to.
120
+ event_name (str): The subject to publish the message to.
118
121
  event_payload (dict): The message to be published.
119
122
  timeout (float): The timeout for the request.
120
123
 
@@ -122,19 +125,20 @@ class NATSDataBus(DataBus):
122
125
  dict: The response message.
123
126
  """
124
127
  response = await self.nc.request(
125
- topic, json.dumps(event_payload).encode("utf-8"), timeout=timeout
128
+ event_name, json.dumps(event_payload).encode("utf-8"), timeout=timeout
126
129
  )
127
130
  return json.loads(response.data.decode("utf-8"))
128
131
 
129
132
  async def subscribe_async_event(
130
- self, topic: str, callback: Callable[[Any], None], durable_name: str
133
+ self, event_name: str, callback: Callable[[Any], None], durable_name: str
131
134
  ):
132
135
  """
133
136
  Subscribe to a JetStream subject with a durable consumer and process messages asynchronously.
134
137
 
135
138
  Args:
136
- topic: The topic to subscribe to.
137
- callback: A callback function to handle received messages.
139
+ event_name (str): The subject to subscribe to.
140
+ callback (Callable[[Any], None]): A callback function to handle received messages.
141
+ durable_name (str): The name of the durable consumer.
138
142
  """
139
143
  try:
140
144
  # Check if the consumer already exists
@@ -152,21 +156,22 @@ class NATSDataBus(DataBus):
152
156
  await self.js.add_consumer(stream=self.stream_name, config=self.consumer_config)
153
157
  logging.info(f"Consumer '{durable_name}' created.")
154
158
 
155
- await self.js.subscribe_bind(stream=self.stream_name, cb=callback, config=self.consumer_config,
156
- consumer=durable_name)
157
- logging.info(f"Subscribed to async event on topic '{topic}'")
159
+ await self.js.subscribe_bind(
160
+ stream=self.stream_name, cb=callback, config=self.consumer_config,
161
+ consumer=durable_name
162
+ )
163
+ logging.info(f"Subscribed to async event on subject '{event_name}'")
158
164
 
159
- async def subscribe_sync_event(self, topic: str, callback: Callable[[Any], None]):
165
+ async def subscribe_sync_event(self, event_name: str, callback: Callable[[Any], None]):
160
166
  """
161
- Subscribe to a NATS subject and return a response after processing the message.
167
+ Subscribe to a NATS subject and process the message synchronously.
162
168
 
163
169
  Args:
164
- topic: The topic to subscribe to.
165
- callback: A callback function to handle received messages.
170
+ event_name (str): The subject to subscribe to.
171
+ callback (Callable[[Any], None]): A callback function to handle received messages.
166
172
  """
167
- await self.nc.subscribe(topic, cb=callback)
168
- logging.info(f"Subscribed to sync event on topic '{topic}'")
173
+ await self.nc.subscribe(event_name, cb=callback)
174
+ logging.info(f"Subscribed to sync event on subject '{event_name}'")
169
175
 
170
176
  def delete_message(self, receipt_handle: str):
171
177
  pass
172
-
@@ -1,82 +0,0 @@
1
- import asyncio
2
- import logging
3
- from typing import Callable, Any, Dict
4
- from shared_kernel.interfaces import DataBus
5
- from concurrent.futures import ThreadPoolExecutor
6
- from shared_kernel.http import HttpClient
7
-
8
- logging.getLogger().setLevel(logging.INFO)
9
-
10
-
11
- class HTTPDatabus(DataBus):
12
- """
13
- A class to handle EventBridge and SQS operations.
14
- """
15
-
16
- _instance = None
17
-
18
- def __new__(cls, *args, **kwargs):
19
- if cls._instance is None:
20
- cls._instance = super(HTTPDatabus, cls).__new__(cls)
21
- return cls._instance
22
-
23
- def __init__(self, config: Dict = None):
24
- """
25
- Initialize the NATSInterface.
26
-
27
- Args:
28
- config (Dict): A Dict containing the NATS config.
29
- """
30
- if not hasattr(self, "initialized"): # to prevent reinitialization
31
- super().__init__()
32
- self.http_client = HttpClient().create_client()
33
-
34
- async def publish_event(self, topic: str, event_payload: dict):
35
- """
36
- Sends an event payload to an HTTP endpoint and returns the response.
37
-
38
- :param topic: The URL for invoking the HTTP endpoint.
39
- :param event_payload: The payload containing all necessary information for invoking the HTTP endpoint.
40
- :return: The response from the HTTP endpoint.
41
- """
42
-
43
- with ThreadPoolExecutor(max_workers=5) as executor:
44
- futures = [executor.submit(self.http_client.post, url=url, json=event_payload) for url in topic]
45
- for future in futures:
46
- future.result()
47
-
48
- def subscribe_async_event(self, topic: str, callback: Callable[[Any], None]):
49
- """
50
- Subscribes to synchronous events and invokes the provided callback upon receiving an event.
51
-
52
- :param callback: The callback function to invoke with the event data.
53
- """
54
- return asyncio.create_task(callback(topic))
55
-
56
- def request_event(self, topic: str, event_payload: dict) -> Any:
57
- """
58
- Sends an event payload to an HTTP endpoint and returns the response.
59
-
60
- :param topic: The URL for invoking the HTTP endpoint.
61
- :param event_payload: The payload containing all necessary information for invoking the HTTP endpoint.
62
- :return: The response from the HTTP endpoint.
63
- """
64
- response = self.http_client.post(url=topic, json=event_payload)
65
- return response
66
-
67
- def subscribe_sync_event(self, topic: str, callback: Callable[[Any], None]):
68
- """
69
- Subscribes to synchronous events and invokes the provided callback upon receiving an event.
70
-
71
- :param callback: The callback function to invoke with the event data.
72
- """
73
- return callback(topic)
74
-
75
- def delete_message(self, receipt_handle: str):
76
- pass
77
-
78
- def make_connection(self, receipt_handle: str):
79
- pass
80
-
81
- def close_connection(self, receipt_handle: str):
82
- pass