nati-log 1.1.0__tar.gz → 2.1.2__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.
Potentially problematic release.
This version of nati-log might be problematic. Click here for more details.
- nati_log-2.1.2/PKG-INFO +10 -0
- {nati_log-1.1.0 → nati_log-2.1.2}/nati_log/client.py +14 -4
- nati_log-2.1.2/nati_log/middleware.py +61 -0
- nati_log-2.1.2/nati_log.egg-info/PKG-INFO +10 -0
- {nati_log-1.1.0 → nati_log-2.1.2}/nati_log.egg-info/SOURCES.txt +2 -2
- {nati_log-1.1.0 → nati_log-2.1.2}/pyproject.toml +2 -2
- {nati_log-1.1.0 → nati_log-2.1.2}/setup.py +2 -2
- nati_log-1.1.0/PKG-INFO +0 -7
- nati_log-1.1.0/nati_log.egg-info/PKG-INFO +0 -7
- nati_log-1.1.0/tests/test_client.py +0 -109
- {nati_log-1.1.0 → nati_log-2.1.2}/nati_log/__init__.py +0 -0
- {nati_log-1.1.0 → nati_log-2.1.2}/nati_log.egg-info/dependency_links.txt +0 -0
- {nati_log-1.1.0 → nati_log-2.1.2}/nati_log.egg-info/requires.txt +0 -0
- {nati_log-1.1.0 → nati_log-2.1.2}/nati_log.egg-info/top_level.txt +0 -0
- {nati_log-1.1.0 → nati_log-2.1.2}/setup.cfg +0 -0
nati_log-2.1.2/PKG-INFO
ADDED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
import requests
|
|
2
2
|
import datetime
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
class NatiLogClient:
|
|
6
5
|
|
|
7
|
-
def __init__(self, api_url, app_id, username, password
|
|
6
|
+
def __init__(self, api_url, api_url_login, app_id, username, password):
|
|
8
7
|
self.api_url = api_url.rstrip("/") # Saca la barra final si ya la tiene
|
|
8
|
+
self.api_url_login = api_url_login
|
|
9
9
|
self.app_id = app_id
|
|
10
10
|
self.username = username
|
|
11
11
|
self.password = password
|
|
12
|
-
self.token =
|
|
12
|
+
self.token = None
|
|
13
|
+
self.get_natilog_token()
|
|
14
|
+
|
|
15
|
+
def get_natilog_token(self):
|
|
16
|
+
payload = {"username": self.username, "password": self.password}
|
|
17
|
+
try:
|
|
18
|
+
response = requests.post(self.api_url_login, json=payload, timeout=10)
|
|
19
|
+
response.raise_for_status() # Lanza un error si el código de estado no es 200
|
|
20
|
+
self.token = response.json().get("access")
|
|
21
|
+
except requests.exceptions.RequestException as e:
|
|
22
|
+
self.token = None
|
|
13
23
|
|
|
14
24
|
def registrar_evento(self, tipo_evento, mensaje, datos=None, fecha=None):
|
|
15
25
|
"""
|
|
16
26
|
Registra un evento en la API de NatiLog
|
|
17
27
|
Args:
|
|
18
|
-
tipo_evento: str - Tipo de evento (e.g., "error", "warning", "info")
|
|
28
|
+
tipo_evento: str - Tipo de evento (e.g., "critical", "error", "warning", "info")
|
|
19
29
|
mensaje: str - Mensaje del evento
|
|
20
30
|
datos: dict - Datos adicionales del evento (opcional)
|
|
21
31
|
fecha: str - Fecha y hora del evento en formato ISO 8601 (opcional)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from .client import NatiLogClient
|
|
3
|
+
|
|
4
|
+
class NatiLogMiddleware:
|
|
5
|
+
def __init__(self, get_response):
|
|
6
|
+
"""
|
|
7
|
+
Middleware para registrar eventos automáticamente en NatiLog.
|
|
8
|
+
"""
|
|
9
|
+
self.get_response = get_response
|
|
10
|
+
|
|
11
|
+
# Obtener parametros desde settings
|
|
12
|
+
self.api_url = settings.NATILOG_API_URL
|
|
13
|
+
self.api_url_login = settings.NATILOG_API_URL_LOGIN
|
|
14
|
+
self.app_id = settings.NATILOG_APP_ID
|
|
15
|
+
self.username = settings.NATILOG_USERNAME
|
|
16
|
+
self.password = settings.NATILOG_PASSWORD
|
|
17
|
+
|
|
18
|
+
# Inicializar el cliente
|
|
19
|
+
self.natilog = NatiLogClient(
|
|
20
|
+
api_url=self.api_url,
|
|
21
|
+
api_url_login=self.api_url_login,
|
|
22
|
+
app_id=self.app_id,
|
|
23
|
+
username=self.username,
|
|
24
|
+
password=self.password,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
def __call__(self, request):
|
|
28
|
+
"""
|
|
29
|
+
Procesa la solicitud y registra eventos en NatiLog.
|
|
30
|
+
"""
|
|
31
|
+
response = self.get_response(request)
|
|
32
|
+
|
|
33
|
+
# INFO: Cada request exitoso
|
|
34
|
+
if 200 <= response.status_code < 300:
|
|
35
|
+
self.natilog.info(
|
|
36
|
+
f"Request OK: {request.method} {request.path}",
|
|
37
|
+
datos={"status_code": response.status_code}
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# WARNING: Redirecciones
|
|
41
|
+
elif 300 <= response.status_code < 400:
|
|
42
|
+
self.natilog.warning(
|
|
43
|
+
f"Redirect: {request.method} {request.path}",
|
|
44
|
+
datos={"status_code": response.status_code}
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# ERROR: 404 y otros errores de cliente
|
|
48
|
+
elif 400 <= response.status_code < 500:
|
|
49
|
+
self.natilog.error(
|
|
50
|
+
f"Client Error {response.status_code}: {request.method} {request.path}",
|
|
51
|
+
datos={"status_code": response.status_code}
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# CRITICAL: 500 y otros errores de servidor
|
|
55
|
+
elif response.status_code >= 500:
|
|
56
|
+
self.natilog.critical(
|
|
57
|
+
f"Server Error {response.status_code}: {request.method} {request.path}",
|
|
58
|
+
datos={"status_code": response.status_code}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
return response
|
|
@@ -2,9 +2,9 @@ pyproject.toml
|
|
|
2
2
|
setup.py
|
|
3
3
|
nati_log/__init__.py
|
|
4
4
|
nati_log/client.py
|
|
5
|
+
nati_log/middleware.py
|
|
5
6
|
nati_log.egg-info/PKG-INFO
|
|
6
7
|
nati_log.egg-info/SOURCES.txt
|
|
7
8
|
nati_log.egg-info/dependency_links.txt
|
|
8
9
|
nati_log.egg-info/requires.txt
|
|
9
|
-
nati_log.egg-info/top_level.txt
|
|
10
|
-
tests/test_client.py
|
|
10
|
+
nati_log.egg-info/top_level.txt
|
|
@@ -3,8 +3,8 @@ requires = ["setuptools>=42", "wheel"]
|
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
|
-
name = "
|
|
7
|
-
version = "
|
|
6
|
+
name = "nati_log"
|
|
7
|
+
version = "2.1.2"
|
|
8
8
|
description = "Estadísticas de logs"
|
|
9
9
|
authors = [{name = "Natalí"}]
|
|
10
10
|
requires-python = ">=3.8"
|
nati_log-1.1.0/PKG-INFO
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import pytest
|
|
2
|
-
import requests
|
|
3
|
-
from nati_log.client import NatiLogClient
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def test_registrar_evento_success(requests_mock):
|
|
7
|
-
api_url = "http://fake-api"
|
|
8
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
9
|
-
|
|
10
|
-
# Mockear la respuesta de la API
|
|
11
|
-
requests_mock.post(f"{api_url}/eventos", json={"status": "ok"}, status_code=201)
|
|
12
|
-
|
|
13
|
-
resp = client.info("Prueba de evento", {"key": "value"})
|
|
14
|
-
assert resp == {"status": "ok"}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def test_registrar_evento_error(requests_mock):
|
|
18
|
-
api_url = "http://fake-api"
|
|
19
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
20
|
-
|
|
21
|
-
# Mockear error
|
|
22
|
-
requests_mock.post(f"{api_url}/eventos", status_code=500)
|
|
23
|
-
|
|
24
|
-
with pytest.raises(requests.exceptions.HTTPError):
|
|
25
|
-
client.error("Algo falló")
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def test_registrar_evento_timeout(requests_mock):
|
|
29
|
-
api_url = "http://fake-api"
|
|
30
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
31
|
-
|
|
32
|
-
# Mockear timeout
|
|
33
|
-
requests_mock.post(f"{api_url}/eventos", exc=requests.exceptions.Timeout)
|
|
34
|
-
|
|
35
|
-
with pytest.raises(requests.exceptions.Timeout):
|
|
36
|
-
client.warning("Esto tardó demasiado")
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def test_registrar_evento_fecha_personalizada(requests_mock):
|
|
40
|
-
api_url = "http://fake-api"
|
|
41
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
42
|
-
|
|
43
|
-
# Mockear la respuesta de la API
|
|
44
|
-
requests_mock.post(f"{api_url}/eventos", json={"status": "ok"}, status_code=201)
|
|
45
|
-
|
|
46
|
-
fecha_custom = "2024-01-01T12:00:00"
|
|
47
|
-
resp = client.info("Evento con fecha personalizada", fecha=fecha_custom)
|
|
48
|
-
assert resp == {"status": "ok"}
|
|
49
|
-
|
|
50
|
-
# Verificar que la petición se hizo con la fecha correcta
|
|
51
|
-
last_request = requests_mock.last_request
|
|
52
|
-
assert last_request.json()["fecha"] == fecha_custom
|
|
53
|
-
assert last_request.json()["aplicacion"] == "test-app"
|
|
54
|
-
assert last_request.json()["tipo_evento"] == "info"
|
|
55
|
-
assert last_request.json()["mensaje"] == "Evento con fecha personalizada"
|
|
56
|
-
assert last_request.json()["datos"] == {}
|
|
57
|
-
assert last_request.method == "POST"
|
|
58
|
-
assert last_request.url == f"{api_url}/eventos"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def test_registrar_evento_sin_datos(requests_mock):
|
|
62
|
-
api_url = "http://fake-api"
|
|
63
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
64
|
-
|
|
65
|
-
# Mockear la respuesta de la API
|
|
66
|
-
requests_mock.post(f"{api_url}/eventos", json={"status": "ok"}, status_code=201)
|
|
67
|
-
|
|
68
|
-
resp = client.info("Evento sin datos")
|
|
69
|
-
assert resp == {"status": "ok"}
|
|
70
|
-
|
|
71
|
-
# Verificar que la petición se hizo con datos como diccionario vacío
|
|
72
|
-
last_request = requests_mock.last_request
|
|
73
|
-
assert last_request.json()["datos"] == {}
|
|
74
|
-
assert last_request.method == "POST"
|
|
75
|
-
assert last_request.url == f"{api_url}/eventos"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def test_registrar_evento_con_datos(requests_mock):
|
|
79
|
-
api_url = "http://fake-api"
|
|
80
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
81
|
-
|
|
82
|
-
# Mockear la respuesta de la API
|
|
83
|
-
requests_mock.post(f"{api_url}/eventos", json={"status": "ok"}, status_code=201)
|
|
84
|
-
|
|
85
|
-
datos_evento = {"user_id": 123, "action": "login"}
|
|
86
|
-
resp = client.info("Evento con datos", datos=datos_evento)
|
|
87
|
-
assert resp == {"status": "ok"}
|
|
88
|
-
|
|
89
|
-
# Verificar que la petición se hizo con los datos correctos
|
|
90
|
-
last_request = requests_mock.last_request
|
|
91
|
-
assert last_request.json()["datos"] == datos_evento
|
|
92
|
-
assert last_request.method == "POST"
|
|
93
|
-
assert last_request.url == f"{api_url}/eventos"
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def test_registrar_evento_app_name_con_barra_final(requests_mock):
|
|
97
|
-
api_url = "http://fake-api/"
|
|
98
|
-
client = NatiLogClient(api_url=api_url, app_name="test-app")
|
|
99
|
-
|
|
100
|
-
# Mockear la respuesta de la API
|
|
101
|
-
requests_mock.post(f"{api_url.rstrip('/')}/eventos", json={"status": "ok"}, status_code=201)
|
|
102
|
-
|
|
103
|
-
resp = client.info("Prueba de evento con barra final en api_url")
|
|
104
|
-
assert resp == {"status": "ok"}
|
|
105
|
-
|
|
106
|
-
# Verificar que la petición se hizo correctamente
|
|
107
|
-
last_request = requests_mock.last_request
|
|
108
|
-
assert last_request.method == "POST"
|
|
109
|
-
assert last_request.url == f"{api_url.rstrip('/')}/eventos"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|