Skip to content

Commit

Permalink
feat: Support configuring requests cache
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Dec 3, 2022
1 parent 8331f51 commit f4aa109
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Singer Tap for Jotform. Built with the [Meltano Singer SDK](https://sdk.meltano.
| api_url | False | https://api.jotform.com | API Base URL |
| user_agent | False | tap-jotform/0.0.1 | User-Agent header |
| start_date | False | None | Start date for data collection |
| requests_cache_config| False | None | Cache configuration for HTTP requests |
| stream_maps | False | None | Config object for stream maps capability. For more information check out [Stream Maps](https://sdk.meltano.com/en/latest/stream_maps.html). |
| stream_map_config | False | None | User-defined config values to be used within map expressions. |
| flattening_enabled | False | None | 'True' to enable schema flattening and automatically expand nested properties. |
Expand Down
11 changes: 11 additions & 0 deletions meltano.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ plugins:
kind: date_iso8601
label: Start Date
description: Start date for collecting data
- name: requests_cache.enabled
kind: boolean
label: Requests Cache Enabled
description: Enable requests cache
- name: requests_cache.config.expire_after
kind: integer
label: Requests Cache Expire After
description: Requests cache expire after
config:
requests_cache.enabled: true
requests_cache.config.expire_after: 3600
loaders:
- name: target-duckdb
variant: jwills
Expand Down
18 changes: 16 additions & 2 deletions tap_jotform/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from singer_sdk.pagination import BaseOffsetPaginator
from singer_sdk.streams import RESTStream

requests_cache.install_cache("requests_cache")


class JotformPaginator(BaseOffsetPaginator):
"""Jotform pagination class."""
Expand Down Expand Up @@ -111,6 +109,22 @@ def parse_response(
)
yield from super().parse_response(response)

@property
def requests_session(self) -> requests.Session:
"""Return a new requests session object.
Returns:
A new requests session object.
"""
if (
self.config.get("requests_cache")
and self.config["requests_cache"]["enabled"]
):
self._requests_session = requests_cache.CachedSession(
**self.config["requests_cache"]["config"]
)
return super().requests_session


class JotformPaginatedStream(JotformStream):
"""A Jotform stream with pagination."""
Expand Down
24 changes: 24 additions & 0 deletions tap_jotform/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ def config_jsonschema(cls) -> dict: # type: ignore
required=False,
description="Start date for data collection",
),
th.Property(
"requests_cache",
th.ObjectType(
th.Property(
"enabled",
th.BooleanType,
default=False,
description="Enable requests cache",
),
th.Property(
"config",
th.ObjectType(
th.Property(
"expire_after",
th.IntegerType,
description="Cache expiration time in seconds",
),
),
description="Requests cache configuration",
default={},
),
),
description="Cache configuration for HTTP requests",
),
).to_dict()

def discover_streams(self) -> list[Stream]:
Expand Down

0 comments on commit f4aa109

Please sign in to comment.