From 3616cff8a452d09654d85718bd20913507d6bfce Mon Sep 17 00:00:00 2001 From: Leon Morten Richter Date: Mon, 31 Oct 2022 14:44:41 +0100 Subject: [PATCH] monkey match the upstream API wrapper --- mktxp/flow/router_connection.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mktxp/flow/router_connection.py b/mktxp/flow/router_connection.py index cc279f9..f08444e 100644 --- a/mktxp/flow/router_connection.py +++ b/mktxp/flow/router_connection.py @@ -14,10 +14,23 @@ import ssl import socket +import collections from datetime import datetime -from routeros_api import RouterOsApiPool from mktxp.cli.config.config import config_handler +# Fix UTF-8 decode error +# See: https://github.com/akpw/mktxp/issues/47 +# The RouterOS-api implicitly assumes that the API response is UTF-8 encoded. +# But Mikrotik uses latin-1. +# Because the upstream dependency is currently abandoned, this is a quick hack to solve the issue + +MIKROTIK_ENCODING = 'latin-1' +import routeros_api.api_structure +routeros_api.api_structure.StringField.get_python_value = lambda _, bytes: bytes.decode(MIKROTIK_ENCODING) +routeros_api.api_structure.default_structure = collections.defaultdict(routeros_api.api_structure.StringField) + +from routeros_api import RouterOsApiPool + class RouterAPIConnectionError(Exception): pass