From 938d7275ba49b6cf718d6c8c5ff03958b13c4243 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 26 Apr 2019 09:27:20 +0200 Subject: [PATCH] implement some methods --- scripts/rest_client.py | 50 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/scripts/rest_client.py b/scripts/rest_client.py index fc3478046..eb6fb97a9 100755 --- a/scripts/rest_client.py +++ b/scripts/rest_client.py @@ -10,6 +10,7 @@ so it can be used as a standalone script. import argparse import json import logging +import inspect from urllib.parse import urlencode, urlparse, urlunparse from pathlib import Path @@ -55,11 +56,11 @@ class FtRestClient(): } # Split url - schema, netloc, path, params, query, fragment = urlparse(basepath) + schema, netloc, path, par, query, fragment = urlparse(basepath) # URLEncode query string query = urlencode(params) # recombine url - url = urlunparse((schema, netloc, path, params, query, fragment)) + url = urlunparse((schema, netloc, path, par, query, fragment)) print(url) try: resp = self.session.request(method, url, headers=hd, data=data, @@ -70,6 +71,12 @@ class FtRestClient(): except ConnectionError: 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): logger.info(f"Running command `{command}` at {self.serverurl}") r = self._call("POST", command) @@ -91,6 +98,27 @@ class FtRestClient(): 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(): parser = argparse.ArgumentParser() @@ -138,12 +166,20 @@ def main(args): server_url = f"http://{url}:{port}" client = FtRestClient(server_url) - # Call commands without arguments - if args["command"] in COMMANDS_NO_ARGS: - client._call_command_noargs(args["command"]) + m = [x for x, y in inspect.getmembers(client) if not x.startswith('_')] + command = args["command"] + if command not in m: + logger.error(f"Command {command} not defined") + return - if args["command"] in INFO_COMMANDS: - client._call_info(args["command"], args["command_arguments"]) + print(getattr(client, 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__":