abs-utils 0.1.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.
- abs_utils-0.1.0/PKG-INFO +16 -0
- abs_utils-0.1.0/README.md +0 -0
- abs_utils-0.1.0/abs_utils/azure_service_bus/__init__.py +0 -0
- abs_utils-0.1.0/abs_utils/azure_service_bus/azure_service_bus.py +19 -0
- abs_utils-0.1.0/abs_utils/azure_service_bus/event_decorator.py +52 -0
- abs_utils-0.1.0/abs_utils/logger/__init__.py +0 -0
- abs_utils-0.1.0/abs_utils/logger/logger.py +31 -0
- abs_utils-0.1.0/pyproject.toml +24 -0
abs_utils-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: abs-utils
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: Your Name
|
|
6
|
+
Author-email: you@example.com
|
|
7
|
+
Requires-Python: >=3.13,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
10
|
+
Requires-Dist: aiohttp (>=3.11.18,<4.0.0)
|
|
11
|
+
Requires-Dist: azure-identity (>=1.22.0,<2.0.0)
|
|
12
|
+
Requires-Dist: azure-servicebus (>=7.14.2,<8.0.0)
|
|
13
|
+
Requires-Dist: fastapi[standard] (>=0.115.12,<0.116.0)
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from azure.servicebus import ServiceBusClient, ServiceBusMessage
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
class AzureServiceBus:
|
|
5
|
+
def __init__(self, connection_string: str, queue_name: str):
|
|
6
|
+
self.client = ServiceBusClient.from_connection_string(conn_str=connection_string)
|
|
7
|
+
self.queue_name = queue_name
|
|
8
|
+
|
|
9
|
+
async def send(self, event_payload: dict):
|
|
10
|
+
"""Send message to Azure Service Bus"""
|
|
11
|
+
if not self.client or not self.queue_name:
|
|
12
|
+
return
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
sender = self.client.get_queue_sender(queue_name=self.queue_name)
|
|
16
|
+
with sender:
|
|
17
|
+
sender.send_messages(ServiceBusMessage(json.dumps(event_payload)))
|
|
18
|
+
except Exception as e:
|
|
19
|
+
print(f"Failed to send message to Azure Service Bus: {e}")
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
from fastapi import Request
|
|
3
|
+
from typing import Any, Callable
|
|
4
|
+
from .azure_service_bus import AzureServiceBus
|
|
5
|
+
from uuid import uuid4
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
def azure_event_decorator(event_type: str):
|
|
9
|
+
"""
|
|
10
|
+
A decorator that sends a message to Azure Service Bus after the function execution.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
event_name (str): The name of the event to be sent
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
Callable: The decorated function
|
|
17
|
+
"""
|
|
18
|
+
def decorator(func: Callable) -> Callable:
|
|
19
|
+
@wraps(func)
|
|
20
|
+
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
21
|
+
# Extract required parameters from kwargs
|
|
22
|
+
entity_name = kwargs.get('entity_name')
|
|
23
|
+
request = kwargs.get('request')
|
|
24
|
+
data = kwargs.get('data')
|
|
25
|
+
azure_service_bus:AzureServiceBus = kwargs.get('azure_service_bus')
|
|
26
|
+
id = kwargs.get('id')
|
|
27
|
+
|
|
28
|
+
# Execute the original function
|
|
29
|
+
result = await func(*args, **kwargs)
|
|
30
|
+
|
|
31
|
+
event_payload = {
|
|
32
|
+
"event_id": str(uuid4()),
|
|
33
|
+
"event_type": event_type,
|
|
34
|
+
"entity_name": entity_name,
|
|
35
|
+
"entity_id": id or None,
|
|
36
|
+
"payload": data,
|
|
37
|
+
"user": {
|
|
38
|
+
"id": request.state.user.id,
|
|
39
|
+
"uuid": request.state.user.uuid,
|
|
40
|
+
"email": request.state.user.email,
|
|
41
|
+
"name": request.state.user.name,
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
# Send message to Azure Service Bus
|
|
45
|
+
event_payload = json.dumps(event_payload)
|
|
46
|
+
print("==============event_payload======================")
|
|
47
|
+
print(event_payload)
|
|
48
|
+
print("==============event_payload======================")
|
|
49
|
+
await azure_service_bus.send(event_payload)
|
|
50
|
+
return result
|
|
51
|
+
return wrapper
|
|
52
|
+
return decorator
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
def setup_logger(
|
|
6
|
+
name: str,
|
|
7
|
+
level: int = logging.INFO
|
|
8
|
+
) -> logging.Logger:
|
|
9
|
+
"""
|
|
10
|
+
Simple logger setup function.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
name (str): Name of the logger
|
|
14
|
+
log_file (Optional[str]): Path to log file (default: None)
|
|
15
|
+
level (int): Logging level (default: logging.INFO)
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
logging.Logger: Configured logger instance
|
|
19
|
+
"""
|
|
20
|
+
# Create logger
|
|
21
|
+
logger = logging.getLogger(name)
|
|
22
|
+
logger.setLevel(level)
|
|
23
|
+
|
|
24
|
+
# Create formatter
|
|
25
|
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
26
|
+
|
|
27
|
+
# Console handler
|
|
28
|
+
console_handler = logging.StreamHandler()
|
|
29
|
+
console_handler.setFormatter(formatter)
|
|
30
|
+
logger.addHandler(console_handler)
|
|
31
|
+
return logger
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "abs-utils"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Your Name",email = "you@example.com"}
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.13,<4.0"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"azure-servicebus (>=7.14.2,<8.0.0)",
|
|
12
|
+
"azure-identity (>=1.22.0,<2.0.0)",
|
|
13
|
+
"aiohttp (>=3.11.18,<4.0.0)",
|
|
14
|
+
"fastapi[standard] (>=0.115.12,<0.116.0)",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[build-system]
|
|
18
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
19
|
+
build-backend = "poetry.core.masonry.api"
|
|
20
|
+
|
|
21
|
+
[tool.poetry]
|
|
22
|
+
packages = [
|
|
23
|
+
{include = "abs_utils"}
|
|
24
|
+
]
|