use Authorization for client

This commit is contained in:
Matthias 2019-05-25 14:14:09 +02:00
parent 04c35b465e
commit 1fab884a2f

View File

@ -26,10 +26,12 @@ logger = logging.getLogger("ft_rest_client")
class FtRestClient(): class FtRestClient():
def __init__(self, serverurl): def __init__(self, serverurl, username=None, password=None):
self._serverurl = serverurl self._serverurl = serverurl
self._session = requests.Session() self._session = requests.Session()
self._username = username
self._password = password
def _call(self, method, apipath, params: dict = None, data=None, files=None): def _call(self, method, apipath, params: dict = None, data=None, files=None):
@ -41,6 +43,12 @@ class FtRestClient():
"Content-Type": "application/json" "Content-Type": "application/json"
} }
if self._username:
hd.update({"username": self._username})
if self._password:
hd.update({"password": self._password})
# Split url # Split url
schema, netloc, path, par, query, fragment = urlparse(basepath) schema, netloc, path, par, query, fragment = urlparse(basepath)
# URLEncode query string # URLEncode query string
@ -244,8 +252,11 @@ def main(args):
config = load_config(args["config"]) config = load_config(args["config"])
url = config.get("api_server", {}).get("server_url", "127.0.0.1") url = config.get("api_server", {}).get("server_url", "127.0.0.1")
port = config.get("api_server", {}).get("listen_port", "8080") port = config.get("api_server", {}).get("listen_port", "8080")
username = config.get("api_server", {}).get("username")
password = config.get("api_server", {}).get("password")
server_url = f"http://{url}:{port}" server_url = f"http://{url}:{port}"
client = FtRestClient(server_url) client = FtRestClient(server_url, username, password)
m = [x for x, y in inspect.getmembers(client) if not x.startswith('_')] m = [x for x, y in inspect.getmembers(client) if not x.startswith('_')]
command = args["command"] command = args["command"]