Support backend connection timeout configuration

This commit is contained in:
Koha9
2025-11-29 08:18:32 +09:00
parent da6056c1ae
commit 0ede137170
4 changed files with 44 additions and 8 deletions
+19 -7
View File
@@ -50,6 +50,7 @@ class PlexClient:
scheme: str = "https",
url: str = "",
port: str = "32400",
timeout: int | None = None,
) -> tuple[PlexServer, str]:
"""Connect to the Plex server using username/password or token.
@@ -69,11 +70,11 @@ class PlexClient:
try:
if not str_is_empty(token):
self.server, self.token = self._connect_with_token(
token, scheme, url, port
token, scheme, url, port, timeout
)
else:
self.server, self.token = self._connect_with_pw(
username, password, scheme, url, port
username, password, scheme, url, port, timeout
)
# Update the base URL and connection status
self.base_url = build_plex_url(scheme, url, port)
@@ -88,30 +89,41 @@ class PlexClient:
raise
def _connect_with_pw(
self, username: str, password: str, scheme: str, url: str, port: str = "32400"
self,
username: str,
password: str,
scheme: str,
url: str,
port: str = "32400",
timeout: int | None = None,
):
"""Return a connected PlexServer instance and update config with token and server info."""
# url 初始化
self.base_url = build_plex_url(scheme, url, port)
# account 初始化
account = MyPlexAccount(username, password)
account = MyPlexAccount(username, password, timeout=timeout)
# token 获取
self.token = account.authenticationToken
self.server = PlexServer(self.base_url, self.token)
self.server = PlexServer(self.base_url, self.token, timeout=timeout)
logger.debug(
f"Connected to Plex server with username: {username}, token: {self.token}"
)
return self.server, self.token
def _connect_with_token(
self, token: str, scheme: str, url: str, port: str = "32400"
self,
token: str,
scheme: str,
url: str,
port: str = "32400",
timeout: int | None = None,
):
"""Return a connected PlexServer instance using a token."""
# URL 初始化
self.base_url = build_plex_url(scheme, url, port)
self.server = PlexServer(self.base_url, token)
self.server = PlexServer(self.base_url, token, timeout=timeout)
logger.debug(f"Connected to Plex server with token: {token}")
return self.server, token