nati-log 1.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.
Potentially problematic release.
This version of nati-log might be problematic. Click here for more details.
- nati_log-1.1.0/PKG-INFO +7 -0
- nati_log-1.1.0/nati_log/__init__.py +1 -0
- nati_log-1.1.0/nati_log/client.py +75 -0
- nati_log-1.1.0/nati_log.egg-info/PKG-INFO +7 -0
- nati_log-1.1.0/nati_log.egg-info/SOURCES.txt +10 -0
- nati_log-1.1.0/nati_log.egg-info/dependency_links.txt +1 -0
- nati_log-1.1.0/nati_log.egg-info/requires.txt +1 -0
- nati_log-1.1.0/nati_log.egg-info/top_level.txt +1 -0
- nati_log-1.1.0/pyproject.toml +11 -0
- nati_log-1.1.0/setup.cfg +4 -0
- nati_log-1.1.0/setup.py +10 -0
- nati_log-1.1.0/tests/test_client.py +109 -0
nati_log-1.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .client import NatiLogClient
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import datetime
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class NatiLogClient:
|
|
6
|
+
|
|
7
|
+
def __init__(self, api_url, app_id, username, password, token=None):
|
|
8
|
+
self.api_url = api_url.rstrip("/") # Saca la barra final si ya la tiene
|
|
9
|
+
self.app_id = app_id
|
|
10
|
+
self.username = username
|
|
11
|
+
self.password = password
|
|
12
|
+
self.token = token
|
|
13
|
+
|
|
14
|
+
def registrar_evento(self, tipo_evento, mensaje, datos=None, fecha=None):
|
|
15
|
+
"""
|
|
16
|
+
Registra un evento en la API de NatiLog
|
|
17
|
+
Args:
|
|
18
|
+
tipo_evento: str - Tipo de evento (e.g., "error", "warning", "info")
|
|
19
|
+
mensaje: str - Mensaje del evento
|
|
20
|
+
datos: dict - Datos adicionales del evento (opcional)
|
|
21
|
+
fecha: str - Fecha y hora del evento en formato ISO 8601 (opcional)
|
|
22
|
+
Returns: dict - Respuesta de la API en formato JSON
|
|
23
|
+
Raises: requests.exceptions.RequestException - Si hay un error en la solicitud
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
if fecha is None:
|
|
28
|
+
fecha = datetime.datetime.now().isoformat() # Fecha y hora actual en formato ISO 8601
|
|
29
|
+
|
|
30
|
+
payload = {
|
|
31
|
+
"aplicacion": self.app_id,
|
|
32
|
+
"tipo_evento": tipo_evento,
|
|
33
|
+
"mensaje": mensaje,
|
|
34
|
+
"datos": datos or {}, # Datos adicionales, si no hay datos, envía un diccionario vacío
|
|
35
|
+
"fecha": fecha,
|
|
36
|
+
}
|
|
37
|
+
headers = {
|
|
38
|
+
"Content-Type": "application/json",}
|
|
39
|
+
if self.token:
|
|
40
|
+
headers["Authorization"] = f"Bearer {self.token}"
|
|
41
|
+
response = requests.post(f"{self.api_url}/evento/", json=payload, headers=headers, timeout=5) # Envía el evento a la API
|
|
42
|
+
response.raise_for_status() # Lanza un error si la respuesta no es 200 OK
|
|
43
|
+
return response.json() # Devuelve la respuesta en formato JSON
|
|
44
|
+
|
|
45
|
+
def debug(self, mensaje, datos=None, fecha=None):
|
|
46
|
+
"""
|
|
47
|
+
Registra un evento de tipo "debug"
|
|
48
|
+
"""
|
|
49
|
+
return self.registrar_evento("debug", mensaje, datos, fecha)
|
|
50
|
+
|
|
51
|
+
def error(self, mensaje, datos=None, fecha=None):
|
|
52
|
+
"""
|
|
53
|
+
Registra un evento de tipo "error"
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
return self.registrar_evento("error", mensaje, datos, fecha)
|
|
57
|
+
|
|
58
|
+
def warning(self, mensaje, datos=None, fecha=None):
|
|
59
|
+
"""
|
|
60
|
+
Registra un evento de tipo "warning"
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
return self.registrar_evento("warning", mensaje, datos, fecha)
|
|
64
|
+
|
|
65
|
+
def info(self, mensaje, datos=None, fecha=None):
|
|
66
|
+
"""
|
|
67
|
+
Registra un evento de tipo "info"
|
|
68
|
+
"""
|
|
69
|
+
return self.registrar_evento("info", mensaje, datos, fecha)
|
|
70
|
+
|
|
71
|
+
def critical(self, mensaje, datos=None, fecha=None):
|
|
72
|
+
"""
|
|
73
|
+
Registra un evento de tipo "critical"
|
|
74
|
+
"""
|
|
75
|
+
return self.registrar_evento("critical", mensaje, datos, fecha)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
pyproject.toml
|
|
2
|
+
setup.py
|
|
3
|
+
nati_log/__init__.py
|
|
4
|
+
nati_log/client.py
|
|
5
|
+
nati_log.egg-info/PKG-INFO
|
|
6
|
+
nati_log.egg-info/SOURCES.txt
|
|
7
|
+
nati_log.egg-info/dependency_links.txt
|
|
8
|
+
nati_log.egg-info/requires.txt
|
|
9
|
+
nati_log.egg-info/top_level.txt
|
|
10
|
+
tests/test_client.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
requests>=2.25.1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nati_log
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=42", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "nati-log"
|
|
7
|
+
version = "1.1.0"
|
|
8
|
+
description = "Estadísticas de logs"
|
|
9
|
+
authors = [{name = "Natalí"}]
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
dependencies = ["requests>=2.25.1"]
|
nati_log-1.1.0/setup.cfg
ADDED
nati_log-1.1.0/setup.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
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"
|