[DEBUG] update upload

This commit is contained in:
Edouard DUPIN 2023-01-18 21:20:25 +01:00
parent 1d0e8899d5
commit aa9331ab05
2 changed files with 17 additions and 6 deletions

View File

@ -10,4 +10,5 @@
from .exception import KaranageException from .exception import KaranageException
from .connection import KaranageConnection from .connection import KaranageConnection
from .state import StateSystem, KaranageState from .state import StateSystem, KaranageState
from .log import KaranageLog from .log import KaranageLog, GroupLogElement

View File

@ -10,12 +10,13 @@
import enum import enum
import requests import requests
import json import json
from typing import Dict, Optional from datetime import datetime, timezone
from typing import Dict, Optional, List
from .connection import KaranageConnection from .connection import KaranageConnection
from .exception import KaranageException from .exception import KaranageException
class GroupLogElement: class GroupLogElement:
def __init__(self, id: int, time: Datetime:None, data: str): def __init__(self, id: int, data: str, time: datetime = None):
self.id = id self.id = id
self.time = time self.time = time
self.data = data self.data = data
@ -35,7 +36,7 @@ class KaranageLog:
return self.connection.get_url(self.service) return self.connection.get_url(self.service)
return f"{self.connection.get_url(self.service)}/{self.system}" return f"{self.connection.get_url(self.service)}/{self.system}"
def send(self, data: Dict, id: int = None, uuid_group: int= None, time: Datetime = None) -> None: def send(self, data: Dict, id: Optional[int] = None, uuid_group: Optional[int] = None, time: Optional[datetime] = None) -> None:
"""Send a message to the server. """Send a message to the server.
:param data: Data to send to the server :param data: Data to send to the server
:param id: Local internal ID :param id: Local internal ID
@ -48,7 +49,7 @@ class KaranageLog:
if uuid_group is not None: if uuid_group is not None:
param["uuid"] = uuid_group param["uuid"] = uuid_group
if time is not None: if time is not None:
param["time"] = id param["time"] = time.astimezone(timezone.utc).isoformat()
header = self.connection.get_header() header = self.connection.get_header()
try: try:
ret = requests.post(self.get_url(), json=data, headers=header, params=param) ret = requests.post(self.get_url(), json=data, headers=header, params=param)
@ -57,11 +58,20 @@ class KaranageLog:
if not 200 <= ret.status_code <= 299: if not 200 <= ret.status_code <= 299:
raise KaranageException(f"Fail send message: {self.get_url()}", ret.status_code, ret.content.decode("utf-8")) raise KaranageException(f"Fail send message: {self.get_url()}", ret.status_code, ret.content.decode("utf-8"))
def send_multiple(self, data: List[GroupLogElement], uuid_group: int= None) -> None: def send_multiple(self, data_input: List[GroupLogElement], uuid_group: Optional[int]= None) -> None:
"""Send multiple log message to the server. """Send multiple log message to the server.
:param data: Data to send to the server :param data: Data to send to the server
:param uuid_group: local internal group UUID :param uuid_group: local internal group UUID
""" """
# Convert:
data = []
for elem in data_input:
data.append({
"id": elem.id,
"time": elem.time.astimezone(timezone.utc).isoformat(),
"data": elem.data,
})
param = {} param = {}
if uuid_group is not None: if uuid_group is not None:
param["uuid"] = uuid_group param["uuid"] = uuid_group