[DEV] update client
This commit is contained in:
parent
839eb1a9c3
commit
31af52dc9e
@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.kar</groupId>
|
||||
<artifactId>karanage</artifactId>
|
||||
<version>0.2.0</version>
|
||||
<version>0.2.1</version>
|
||||
<properties>
|
||||
|
||||
<maven.compiler.version>3.1</maven.compiler.version>
|
||||
@ -22,7 +22,7 @@
|
||||
<dependency>
|
||||
<groupId>kangaroo-and-rabbit</groupId>
|
||||
<artifactId>archidata</artifactId>
|
||||
<version>0.2.0</version>
|
||||
<version>0.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
4
client/.connection.json
Normal file
4
client/.connection.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"url": "http://localhost:20080/karanage/api",
|
||||
"group": "home"
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
{
|
||||
"url": "http://localhost:20080/karanage/api/state",
|
||||
"group": "home"
|
||||
}
|
@ -125,7 +125,7 @@ if __name__ == '__main__':
|
||||
parser.add_argument("-C", "--connection", type=str, default="/etc/karanage/connection.json", help="json configuration file")
|
||||
|
||||
# This element are read from the connection file:
|
||||
parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api/state", help="Base URL of the web service")
|
||||
parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api", help="Base URL of the web service")
|
||||
parser.add_argument("-g", "--group", type=str, default="home", help="Group the the message")
|
||||
parser.add_argument("-T", "--token", type=str, default="", help="Token to access to the server")
|
||||
|
||||
@ -205,7 +205,7 @@ if __name__ == '__main__':
|
||||
print(json.dumps(out, indent=4))
|
||||
# send message to the server:
|
||||
try:
|
||||
restInterface.send_to_server(configuration["config"]["topic"], out)
|
||||
except karanage.KarangeSendError as ex:
|
||||
restInterface.send_state_to_server(configuration["config"]["topic"], out)
|
||||
except karanage.KarangeException as ex:
|
||||
print(f"Can not send to the server: {ex}")
|
||||
time.sleep(configuration["config"]["sleep"])
|
@ -14,15 +14,15 @@ if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-C", "--connection", type=str, default="/etc/karanage/connection.json", help="json configuration file")
|
||||
parser.add_argument("-t", "--topic", type=str, default="", help="Topic of the message")
|
||||
parser.add_argument("-s", "--since", type=str, default=None, help="Iso date since the value time must be")
|
||||
|
||||
# This element are read from the connection file:
|
||||
parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api/state", help="Base URL of the web service")
|
||||
parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api", help="Base URL of the web service")
|
||||
parser.add_argument("-g", "--group", type=str, default="home", help="Group the the message")
|
||||
parser.add_argument("-T", "--token", type=str, default="", help="Token to access to the server")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
if Path(args.connection).exists():
|
||||
f = open(args.connection, "r")
|
||||
connection = json.loads(f.read())
|
||||
@ -44,8 +44,8 @@ if __name__ == '__main__':
|
||||
connection["token"])
|
||||
|
||||
if args.topic == "":
|
||||
data = restInterface.get_all()
|
||||
data = restInterface.get_state_all(since=args.since)
|
||||
print(f"Ret = {json.dumps(data, indent=4)}")
|
||||
else:
|
||||
data = restInterface.get_topic(args.topic)
|
||||
data = restInterface.get_state_topic(args.topic, since=args.since)
|
||||
print(f"Ret = {json.dumps(data, indent=4)}")
|
52
client/python/karanage-tools/bin/karanage-tools-state-history-get
Executable file
52
client/python/karanage-tools/bin/karanage-tools-state-history-get
Executable file
@ -0,0 +1,52 @@
|
||||
#!/bin/python3
|
||||
# Importing the library
|
||||
import psutil
|
||||
import argparse
|
||||
import time
|
||||
import subprocess
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
import karanage
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Load arguments:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-C", "--connection", type=str, default="/etc/karanage/connection.json", help="json configuration file")
|
||||
parser.add_argument("-t", "--topic", type=str, default="", help="Topic of the message")
|
||||
parser.add_argument("-s", "--since", type=str, default=None, help="Iso date since the value time must be")
|
||||
parser.add_argument("-S", "--since-id", type=str, default=None, help="Remote BDD id to start request")
|
||||
parser.add_argument("-l", "--limit", type=int, default=100, help="Limit the number of request")
|
||||
|
||||
# This element are read from the connection file:
|
||||
parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api", help="Base URL of the web service")
|
||||
parser.add_argument("-g", "--group", type=str, default="home", help="Group the the message")
|
||||
parser.add_argument("-T", "--token", type=str, default="", help="Token to access to the server")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if Path(args.connection).exists():
|
||||
f = open(args.connection, "r")
|
||||
connection = json.loads(f.read())
|
||||
f.close()
|
||||
else:
|
||||
connection = {}
|
||||
# manage the connection model
|
||||
if "url" not in connection:
|
||||
connection["url"] = args.url
|
||||
if "group" not in connection:
|
||||
connection["group"] = args.group
|
||||
if "token" not in connection:
|
||||
connection["token"] = args.token
|
||||
|
||||
# create the rest interface of karanage
|
||||
restInterface = karanage.KaranageREST(
|
||||
connection["url"],
|
||||
connection["group"],
|
||||
connection["token"])
|
||||
|
||||
if args.topic == "":
|
||||
print("Missing TOPIC ...")
|
||||
else:
|
||||
data = restInterface.get_state_history_topic(args.topic, since=args.since, since_id=args.since_id, limit=args.limit)
|
||||
print(f"Ret = {json.dumps(data, indent=4)}")
|
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/python
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
##
|
||||
## @author Edouard DUPIN
|
||||
@ -49,7 +49,8 @@ setup(name='karanage-tools',
|
||||
keywords='system cpu status',
|
||||
scripts=[
|
||||
'bin/karanage-system',
|
||||
'bin/karanage-tools-get',
|
||||
'bin/karanage-tools-state-get',
|
||||
'bin/karanage-tools-state-history-get',
|
||||
],
|
||||
include_package_data = True,
|
||||
zip_safe=False)
|
||||
|
@ -1 +1 @@
|
||||
0.1.0-dev
|
||||
0.2.0-dev
|
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/python
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
##
|
||||
## @author Edouard DUPIN
|
||||
@ -7,4 +7,4 @@
|
||||
##
|
||||
## @license MPL v2.0 (see license file)
|
||||
##
|
||||
from .interface import StateSystem, KaranageREST, KarangeSendError
|
||||
from .interface import StateSystem, KaranageREST, KarangeSendError, KarangeException
|
||||
|
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/python
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
##
|
||||
## @author Edouard DUPIN
|
||||
@ -24,6 +24,13 @@ class KarangeSendError(Exception):
|
||||
def __str__(self):
|
||||
return f"{Exception.__str__(self)} Status={self.error_id} message='{self.error_message}'"
|
||||
|
||||
class KarangeException(KarangeSendError):
|
||||
def __init__(self, message, error_id, error_message):
|
||||
# Call the base class constructor with the parameters it needs
|
||||
super().__init__(message, error_id, error_message )
|
||||
|
||||
|
||||
|
||||
class StateSystem(enum.Enum):
|
||||
OK = "OK"
|
||||
FAIL = "FAIL"
|
||||
@ -43,10 +50,13 @@ class KaranageREST:
|
||||
self.url = url
|
||||
self.group = group
|
||||
self.token = token
|
||||
self.root_url = f"{self.url}/{self.group}"
|
||||
|
||||
def get_url(self, service: str, topic: Optional[str] = None):
|
||||
if topic is None:
|
||||
return f"{self.url}/{service}/{self.group}"
|
||||
return f"{self.url}/{service}/{self.group}/{topic}"
|
||||
|
||||
def send_to_server(self, topic: str, data: Optional[Dict], state: StateSystem = StateSystem.OK) -> None:
|
||||
def send_state_to_server(self, topic: str, data: Optional[Dict], state: StateSystem = StateSystem.OK) -> None:
|
||||
"""
|
||||
@brief Send a message to the server.
|
||||
@param[in] topic Topic where to publish the data.
|
||||
@ -62,39 +72,80 @@ class KaranageREST:
|
||||
if self.token is not None and len(self.token) >15:
|
||||
header['Authorization'] = f"zota {self.token}"
|
||||
try:
|
||||
ret = requests.post(f"{self.root_url}/{topic}", json=data, headers=header, params=param)
|
||||
ret = requests.post(self.get_url("state", topic), json=data, headers=header, params=param)
|
||||
except requests.exceptions.ConnectionError as ex:
|
||||
raise KarangeSendError(f"Fail connect server: {self.root_url}/{topic}", 0, str(ex))
|
||||
raise KarangeException(f"Fail connect server: {self.get_url('state', topic)}", 0, str(ex))
|
||||
if 200 <= ret.status_code <= 299:
|
||||
pass
|
||||
else:
|
||||
raise KarangeSendError(f"Fail send message: {self.root_url}/{topic}", ret.status_code, ret.content.decode("utf-8"))
|
||||
raise KarangeException(f"Fail send message: {self.get_url('state', topic)}", ret.status_code, ret.content.decode("utf-8"))
|
||||
|
||||
def get_all(self) -> Dict:
|
||||
def get_state_all(self, since: Optional[str] = None) -> Dict:
|
||||
"""
|
||||
@brief Get all the topic fom the server.
|
||||
@param since ISO1866 time value.
|
||||
@return A dictionnary with the requested data.
|
||||
"""
|
||||
param = { }
|
||||
header = { }
|
||||
if self.token is not None and len(self.token) >15:
|
||||
header['Authorization'] = f"zota {self.token}"
|
||||
ret = requests.get(self.root_url, headers=header, params=param)
|
||||
if since is not None:
|
||||
param["since"] = since
|
||||
ret = requests.get(self.get_url("state"), headers=header, params=param)
|
||||
if 200 == ret.status_code:
|
||||
return json.loads(ret.content.decode('utf-8'))
|
||||
raise KarangeSendError(f"Fail get data: {self.root_url}", ret.status_code, ret.content.decode("utf-8"))
|
||||
raise KarangeException(f"Fail get data: {self.get_url('state')}", ret.status_code, ret.content.decode("utf-8"))
|
||||
|
||||
def get_topic(self, topic: str) -> Dict:
|
||||
def get_state_topic(self, topic: str, since: Optional[str] = None) -> Dict:
|
||||
"""
|
||||
@brief Get all the topic fom the server.
|
||||
@param since ISO1866 time value.
|
||||
@return A dictionnary with the requested data.
|
||||
"""
|
||||
param = { }
|
||||
header = { }
|
||||
if self.token is not None and len(self.token) >15:
|
||||
header['Authorization'] = f"zota {self.token}"
|
||||
ret = requests.get(f"{self.root_url}/{topic}", headers=header, params=param)
|
||||
if since is not None:
|
||||
param["since"] = since
|
||||
ret = requests.get(self.get_url("state", topic), headers=header, params=param)
|
||||
#print(ret.content.decode('utf-8'))
|
||||
if 200 == ret.status_code:
|
||||
return json.loads(ret.content.decode('utf-8'))
|
||||
raise KarangeSendError(f"Fail get data: {self.root_url}/{topic}", ret.status_code, ret.content.decode("utf-8"))
|
||||
raise KarangeException(f"Fail get data: {self.get_url('state', topic)}", ret.status_code, ret.content.decode("utf-8"))
|
||||
|
||||
def get_state_history_topic(self, topic: str, since: Optional[str] = None, since_id: Optional[int] = None, limit: Optional[int] = None) -> Dict:
|
||||
"""
|
||||
@brief Get all the topic fom the server.
|
||||
@param since ISO1866 time value.
|
||||
@param since_id remote BDD index of tje fielf.
|
||||
@param limit Number of value we want to get
|
||||
@return A dictionnary with the requested data.
|
||||
"""
|
||||
param = { }
|
||||
header = { }
|
||||
if self.token is not None and len(self.token) >15:
|
||||
header['Authorization'] = f"zota {self.token}"
|
||||
if since is not None:
|
||||
param["since"] = since
|
||||
if since_id is not None:
|
||||
param["sinceId"] = since_id
|
||||
if limit is not None:
|
||||
param["limit"] = limit
|
||||
ret = requests.get(self.get_url("state_history", topic), headers=header, params=param)
|
||||
#print(ret.content.decode('utf-8'))
|
||||
if 200 == ret.status_code:
|
||||
return json.loads(ret.content.decode('utf-8'))
|
||||
raise KarangeException(f"Fail get data: {self.get_url('state_history', topic)}", ret.status_code, ret.content.decode("utf-8"))
|
||||
|
||||
|
||||
def get_all(self) -> Dict:
|
||||
"""Deprecated"""
|
||||
return self.get_state_all()
|
||||
def get_topic(self, topic: str) -> Dict:
|
||||
"""Deprecated"""
|
||||
return self.get_state_topic(topic)
|
||||
def send_to_server(self, topic: str, data: Optional[Dict], state: StateSystem = StateSystem.OK) -> None:
|
||||
"""Deprecated"""
|
||||
return self.send_state_to_server(topic, data, state)
|
@ -54,3 +54,6 @@ setup(name='karanage',
|
||||
# sudo ./setup.py develop --user
|
||||
#TO register all in pip: ./setup.py register sdist upload
|
||||
|
||||
# https://packaging.python.org/en/latest/tutorials/packaging-projects/
|
||||
# python3 -m build
|
||||
# python3 -m twine upload dist/*
|
@ -1 +1 @@
|
||||
0.1.0-dev
|
||||
0.2.0-dev
|
Loading…
Reference in New Issue
Block a user