From 8872158a6a53e99a919bd70cf204d64b4c81e3a4 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2019 10:29:50 +0000 Subject: [PATCH 1/3] Bump mypy from 0.730 to 0.740 Bumps [mypy](https://github.com/python/mypy) from 0.730 to 0.740. - [Release notes](https://github.com/python/mypy/releases) - [Commits](https://github.com/python/mypy/compare/v0.730...v0.740) Signed-off-by: dependabot-preview[bot] --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 0578c03bd..f5cde59e8 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,7 +7,7 @@ coveralls==1.8.2 flake8==3.7.8 flake8-type-annotations==0.1.0 flake8-tidy-imports==3.0.0 -mypy==0.730 +mypy==0.740 pytest==5.2.1 pytest-asyncio==0.10.0 pytest-cov==2.8.1 From a43d436f9863543fc9a010c256a8b0659f087713 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 21 Oct 2019 19:43:44 +0200 Subject: [PATCH 2/3] Move decorators out of API Class --- freqtrade/rpc/api_server.py | 54 ++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 711202b27..95ee51eaf 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -2,7 +2,7 @@ import logging import threading from datetime import date, datetime from ipaddress import IPv4Address -from typing import Dict +from typing import Dict, Callable, Any from arrow import Arrow from flask import Flask, jsonify, request @@ -34,6 +34,34 @@ class ArrowJSONEncoder(JSONEncoder): return JSONEncoder.default(self, obj) +# Type should really be Callable[[ApiServer, Any], Any], but that will create a circular dependency +def require_login(func: Callable[[Any, Any], Any]): + + def func_wrapper(obj, *args, **kwargs): + + auth = request.authorization + if auth and obj.check_auth(auth.username, auth.password): + return func(obj, *args, **kwargs) + else: + return jsonify({"error": "Unauthorized"}), 401 + + return func_wrapper + + +# Type should really be Callable[[ApiServer], Any], but that will create a circular dependency +def rpc_catch_errors(func: Callable[[Any], Any]): + + def func_wrapper(obj, *args, **kwargs): + + try: + return func(obj, *args, **kwargs) + except RPCException as e: + logger.exception("API Error calling %s: %s", func.__name__, e) + return obj.rest_error(f"Error querying {func.__name__}: {e}") + + return func_wrapper + + class ApiServer(RPC): """ This class runs api server and provides rpc.rpc functionality to it @@ -41,34 +69,10 @@ class ApiServer(RPC): This class starts a none blocking thread the api server runs within """ - def rpc_catch_errors(func): - - def func_wrapper(self, *args, **kwargs): - - try: - return func(self, *args, **kwargs) - except RPCException as e: - logger.exception("API Error calling %s: %s", func.__name__, e) - return self.rest_error(f"Error querying {func.__name__}: {e}") - - return func_wrapper - def check_auth(self, username, password): return (username == self._config['api_server'].get('username') and password == self._config['api_server'].get('password')) - def require_login(func): - - def func_wrapper(self, *args, **kwargs): - - auth = request.authorization - if auth and self.check_auth(auth.username, auth.password): - return func(self, *args, **kwargs) - else: - return jsonify({"error": "Unauthorized"}), 401 - - return func_wrapper - def __init__(self, freqtrade) -> None: """ Init the api server, and init the super class RPC From 73fa5bae968ecbf964e2360a4eb0a91c8d6db90c Mon Sep 17 00:00:00 2001 From: hroff-1902 <47309513+hroff-1902@users.noreply.github.com> Date: Tue, 22 Oct 2019 00:03:11 +0300 Subject: [PATCH 3/3] minor: Fix wording in a docstring --- freqtrade/rpc/api_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 95ee51eaf..67bbfdc78 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -66,7 +66,7 @@ class ApiServer(RPC): """ This class runs api server and provides rpc.rpc functionality to it - This class starts a none blocking thread the api server runs within + This class starts a non-blocking thread the api server runs within """ def check_auth(self, username, password):