From 6740d33de27c1f33619801a52268299b4e7360b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Thu, 18 Jul 2024 21:48:03 -0600 Subject: [PATCH] feat: Added a default user agent --- singer_sdk/streams/rest.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/singer_sdk/streams/rest.py b/singer_sdk/streams/rest.py index c65ee3f56..956f61f46 100644 --- a/singer_sdk/streams/rest.py +++ b/singer_sdk/streams/rest.py @@ -107,7 +107,7 @@ def __init__( super().__init__(name=name, schema=schema, tap=tap) if path: self.path = path - self._http_headers: dict = {} + self._http_headers: dict = {"User-Agent": self.user_agent} self._requests_session = requests.Session() self._compiled_jsonpath = None self._next_page_token_compiled_jsonpath = None @@ -157,6 +157,15 @@ def requests_session(self) -> requests.Session: self._requests_session = requests.Session() return self._requests_session + @property + def user_agent(self) -> str: + """Get the user agent string for the stream. + + Returns: + The user agent string. + """ + return f"{self.tap_name}/{self._tap.plugin_version}" + def validate_response(self, response: requests.Response) -> None: """Validate HTTP response. @@ -560,10 +569,7 @@ def http_headers(self) -> dict: Returns: Dictionary of HTTP headers to use as a base for every request. """ - result = self._http_headers - if "user_agent" in self.config: - result["User-Agent"] = self.config.get("user_agent") - return result + return self._http_headers @property def timeout(self) -> int: