From be8588d0399fe737ca1117d05823921b54f46622 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Thu, 6 Jun 2024 21:52:58 +0200 Subject: [PATCH] Use reqparser. Fix indentation. --- thermostat.py | 64 +++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/thermostat.py b/thermostat.py index 31bc183..d0f244d 100644 --- a/thermostat.py +++ b/thermostat.py @@ -180,31 +180,24 @@ Set_mode_parser.add_argument( @ns_thermostat.route('/set_mode') class Set_mode_thermostat(Resource): - @auth_required - @api.expect(Set_mode_parser, validate=True) - def post(self): - global new_forced_mode - #try: - # data = Set_mode(**request.json) - #except Exception as e: - # logging.error(e) - # return "K0", 400 - try: - args = Set_mode_parser.parse_args() - new_forced_mode = args["value"] - xprint(new_forced_mode) - except Exception as e: - logging.error(e) - return "K0", 400 - return "OK", 201 + @auth_required + @api.expect(Set_mode_parser, validate=True) + def post(self): + global new_forced_mode + try: + args = Set_mode_parser.parse_args() + new_forced_mode = args["value"] + except Exception as e: + logging.error(e) + return "K0", 400 + return "OK", 201 @ns_thermostat.route('/status') class Status_thermostat(Resource): - @auth_required - def get(self): - result = status_as_dict() - logging.debug(result) - return result + @auth_required + def get(self): + result = status_as_dict() + return result Set_verbosity_parser = api.parser() Set_verbosity_parser.add_argument( @@ -213,16 +206,23 @@ Set_verbosity_parser.add_argument( @ns_thermostat.route('/set_verbosity') class Set_verbosity_thermostat(Resource): - @auth_required - @api.expect(Set_verbosity_parser, validate=True) - def put(self): - if api.payload["value"] == 'DEBUG': - logging.getLogger().setLevel(logging.DEBUG) - elif api.payload["value"] == 'INFO': - logging.getLogger().setLevel(logging.INFO) - elif api.payload["value"] == 'WARNING': - logging.getLogger().setLevel(logging.WARNING) - return "OK", 201 + @auth_required + @api.expect(Set_verbosity_parser, validate=True) + def put(self): + try: + args = Set_verbosity_parser.parse_args() + if args["value"] == 'DEBUG': + logging.getLogger().setLevel(logging.DEBUG) + elif args["value"] == 'INFO': + logging.getLogger().setLevel(logging.INFO) + elif args["value"] == 'WARNING': + logging.getLogger().setLevel(logging.WARNING) + else: + return "Bad request", 400 + return "OK", 201 + except Exception as e: + logging.error(e) + return "K0", 400 api.add_namespace(ns_thermostat)