Skip to content

Commit

Permalink
feat: Cache HTTP responses (#84)
Browse files Browse the repository at this point in the history
* Implement HTTP caching

* Respect cache in test workflow

* Expire cache after 1 day

* Save cache regardless of hit during restore
  • Loading branch information
ReubenFrankel authored Nov 18, 2024
1 parent 016acc9 commit 0ce345d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ jobs:
run: |
poetry env use ${{ matrix.python-version }}
poetry install
- name: Cache HTTP responses
uses: actions/cache@v4
with:
path: ~/.cache/tap-f1.sqlite
key: http-responses-${{ github.run_id }}
restore-keys: |
http-responses-
- name: Test with pytest
run: |
poetry run pytest
88 changes: 87 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ python = "^3.8"
singer-sdk = { version="~=0.42.1" }
fs-s3fs = { version = "~=1.1.1", optional = true }
requests = "~=2.32.3"
requests-cache = "^1.2.1"

[tool.poetry.group.dev.dependencies]
pytest = ">=7.4.0"
Expand All @@ -30,6 +31,8 @@ warn_unused_configs = true
[tool.ruff]
ignore = [
"ANN001", # missing-type-function-argument
"ANN002", # missing-type-args
"ANN003", # missing-type-kwargs
"ANN101", # missing-type-self
"ANN102", # missing-type-cls
"ANN201", # missing-return-type-undocumented-public-function
Expand Down
12 changes: 12 additions & 0 deletions tap_f1/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""REST client handling, including F1Stream base class."""

from datetime import timedelta

from requests_cache import CachedSession
from singer_sdk.streams import RESTStream
from typing_extensions import override

Expand All @@ -12,6 +15,15 @@ class F1Stream(RESTStream):
url_base = "https://ergast.com/api/f1"
_limit = 1000

def __init__(self, *args, **kwargs) -> None:
"""Initialise the F1 stream."""
super().__init__(*args, **kwargs)
self._requests_session = CachedSession(
self.tap_name,
use_cache_dir=True,
expire_after=timedelta(days=1),
)

@override
def get_new_paginator(self):
return F1Paginator(0, self._limit)
Expand Down

0 comments on commit 0ce345d

Please sign in to comment.