Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(taps): Added a default user agent for REST and GraphQL taps #2549

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading