Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

Add configurable request timeout to HTTP client (#161)

Punit Arani2c787db78200
diff --git a/fli/search/client.py b/fli/search/client.py
index 6126574..7885283 100644
--- a/fli/search/client.py
+++ b/fli/search/client.py
@@ -20,6 +20,7 @@ callers cooperate cleanly under Google's 10 req/sec ceiling.
from __future__ import annotations
+import os
import threading
from typing import TYPE_CHECKING, Any
@@ -57,6 +58,20 @@ _client_lock = threading.Lock()
# Google's published ceiling.
DEFAULT_CALLS_PER_SECOND = 10
+# Request timeout in seconds. Override with the FLI_TIMEOUT env var.
+DEFAULT_TIMEOUT: float = 60.0
+_env_timeout = os.environ.get("FLI_TIMEOUT")
+if _env_timeout is not None:
+ try:
+ REQUEST_TIMEOUT: float = float(_env_timeout)
+ except ValueError:
+ msg = f"FLI_TIMEOUT must be a number of seconds, got: {_env_timeout!r}"
+ raise ValueError(msg) from None
+ if REQUEST_TIMEOUT <= 0:
+ raise ValueError(f"FLI_TIMEOUT must be a positive number, got: {_env_timeout!r}")
+else:
+ REQUEST_TIMEOUT = DEFAULT_TIMEOUT
+
class Client:
"""HTTP client with built-in rate limiting, retry and user agent impersonation functionality.
@@ -111,6 +126,7 @@ class Client:
def get(self, url: str, **kwargs: Any) -> Response:
"""Make a rate-limited GET request with automatic retries."""
self._rate_limiter.acquire()
+ kwargs.setdefault("timeout", REQUEST_TIMEOUT)
try:
response = self._session().get(url, **kwargs)
response.raise_for_status()
@@ -122,6 +138,7 @@ class Client:
def post(self, url: str, **kwargs: Any) -> Response:
"""Make a rate-limited POST request with automatic retries."""
self._rate_limiter.acquire()
+ kwargs.setdefault("timeout", REQUEST_TIMEOUT)
try:
response = self._session().post(url, **kwargs)
response.raise_for_status()