Skip to content

Commit

Permalink
feat(taps): Added a default user agent for REST and GraphQL taps (#2549)
Browse files Browse the repository at this point in the history
* feat: Added a default user agent

* Remove redundant user-agent in sample

* Use `cached_property`

* Make backwards compatible and update cookiecutter
  • Loading branch information
edgarrmondragon authored Aug 13, 2024
1 parent 477a5eb commit be349ac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class Tap{{ cookiecutter.source_name }}({{ 'SQL' if cookiecutter.stream_type ==
default="https://api.mysample.com",
description="The url for the API service",
),
{%- if cookiecutter.stream_type in ("GraphQL", "REST") %}
th.Property(
"user_agent",
th.StringType,
description=(
"A custom User-Agent header to send with each request. Default is "
"'<tap_name>/<tap_version>'"
),
),
{%- endif %}
).to_dict()
{%- if cookiecutter.stream_type in ("GraphQL", "REST", "Other") %}
Expand Down
4 changes: 0 additions & 4 deletions samples/sample_tap_dummy_json/tap_dummyjson/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ def authenticator(self):
password=self.config["password"],
)

@property
def http_headers(self):
return {"User-Agent": "tap-dummyjson"}

def get_new_paginator(self):
return BaseOffsetPaginator(start_value=0, page_size=PAGE_SIZE)

Expand Down
20 changes: 15 additions & 5 deletions singer_sdk/streams/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import copy
import logging
import typing as t
from functools import cached_property
from http import HTTPStatus
from urllib.parse import urlparse
from warnings import warn
Expand Down Expand Up @@ -100,7 +101,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
Expand Down Expand Up @@ -150,6 +151,18 @@ def requests_session(self) -> requests.Session:
self._requests_session = requests.Session()
return self._requests_session

@cached_property
def user_agent(self) -> str:
"""Get the user agent string for the stream.
Returns:
The user agent string.
"""
return self.config.get(
"user_agent",
f"{self.tap_name}/{self._tap.plugin_version}",
)

def validate_response(self, response: requests.Response) -> None:
"""Validate HTTP response.
Expand Down Expand Up @@ -553,10 +566,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:
Expand Down

0 comments on commit be349ac

Please sign in to comment.