implement some methods

This commit is contained in:
Matthias 2019-04-26 09:27:20 +02:00
parent 8f9b9d31e2
commit 938d7275ba

View File

@ -10,6 +10,7 @@ so it can be used as a standalone script.
import argparse import argparse
import json import json
import logging import logging
import inspect
from urllib.parse import urlencode, urlparse, urlunparse from urllib.parse import urlencode, urlparse, urlunparse
from pathlib import Path from pathlib import Path
@ -55,11 +56,11 @@ class FtRestClient():
} }
# Split url # Split url
schema, netloc, path, params, query, fragment = urlparse(basepath) schema, netloc, path, par, query, fragment = urlparse(basepath)
# URLEncode query string # URLEncode query string
query = urlencode(params) query = urlencode(params)
# recombine url # recombine url
url = urlunparse((schema, netloc, path, params, query, fragment)) url = urlunparse((schema, netloc, path, par, query, fragment))
print(url) print(url)
try: try:
resp = self.session.request(method, url, headers=hd, data=data, resp = self.session.request(method, url, headers=hd, data=data,
@ -70,6 +71,12 @@ class FtRestClient():
except ConnectionError: except ConnectionError:
logger.warning("Connection error") logger.warning("Connection error")
def _get(self, apipath, params: dict = None):
return self._call("GET", apipath, params=params)
def _post(self, apipath, params: dict = None, data: dict = None):
return self._call("POST", apipath, params=params, data=data)
def _call_command_noargs(self, command): def _call_command_noargs(self, command):
logger.info(f"Running command `{command}` at {self.serverurl}") logger.info(f"Running command `{command}` at {self.serverurl}")
r = self._call("POST", command) r = self._call("POST", command)
@ -91,6 +98,27 @@ class FtRestClient():
logger.info(r) logger.info(r)
def version(self):
"""
Returns the version of the bot
:returns: json object containing the version
"""
return self._get("version")
def count(self):
"""
Returns the amount of open trades
:returns: json object
"""
return self._get("count")
def daily(self, days=None):
"""
Returns the amount of open trades
:returns: json object
"""
return self._get("daily", params={"timescale": days} if days else None)
def add_arguments(): def add_arguments():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -138,12 +166,20 @@ def main(args):
server_url = f"http://{url}:{port}" server_url = f"http://{url}:{port}"
client = FtRestClient(server_url) client = FtRestClient(server_url)
# Call commands without arguments m = [x for x, y in inspect.getmembers(client) if not x.startswith('_')]
if args["command"] in COMMANDS_NO_ARGS: command = args["command"]
client._call_command_noargs(args["command"]) if command not in m:
logger.error(f"Command {command} not defined")
return
if args["command"] in INFO_COMMANDS: print(getattr(client, command)(*args["command_arguments"]))
client._call_info(args["command"], args["command_arguments"])
# Call commands without arguments
# if args["command"] in COMMANDS_NO_ARGS:
# client._call_command_noargs(args["command"])
# if args["command"] in INFO_COMMANDS:
# client._call_info(args["command"], args["command_arguments"])
if __name__ == "__main__": if __name__ == "__main__":