From b880e2787260635290ecd9334dc2a5bb79dd9f42 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Fri, 13 Dec 2024 21:42:04 +0800 Subject: [PATCH] refactor: only create connection pool when necessary --- transmission_rpc/client.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/transmission_rpc/client.py b/transmission_rpc/client.py index 6f687c2..9c674c2 100644 --- a/transmission_rpc/client.py +++ b/transmission_rpc/client.py @@ -155,11 +155,14 @@ def __init__( self.__semver_version = None common_args: dict[str, Any] = {"host": host, "timeout": self.timeout, "retries": False} - self.__http_client = { - "http": urllib3.HTTPConnectionPool(port=port, **common_args), - "https": urllib3.HTTPSConnectionPool(port=port, ca_certs=certifi.where(), **common_args), - "http+unix": UnixHTTPConnectionPool(**common_args), - }[protocol] + if protocol == "http": + self.__http_client = urllib3.HTTPConnectionPool(port=port, **common_args) + elif protocol == "https": + self.__http_client = urllib3.HTTPSConnectionPool(port=port, ca_certs=certifi.where(), **common_args) + elif protocol == "http+unix": + self.__http_client = UnixHTTPConnectionPool(**common_args) + else: + raise ValueError(f"Unknown protocol {protocol!r}, only 'http', 'https' or 'http+unix' is supported") self.get_session(arguments=["rpc-version", "rpc-version-semver", "version"]) self.__torrent_get_arguments = get_torrent_arguments(self.__protocol_version)