feat(generator): add typed HTTP client to the fetcher service

Implements thin wrapper over fetcher's two main endpoints: GET /data/<user>
and POST /fetch. Used by worker.py (rebuilds), api.py (user lookups), and
cron.py (bulk refresh). Includes authentication via X-Internal-Token header.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
shay
2026-04-17 16:47:38 -04:00
co-authored by Claude Opus 4.7
parent 540d172011
commit 97ad1c2b39
2 changed files with 58 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
"""HTTP client to the fetcher service. Used by api.py, worker.py, cron.py."""
import requests
from . import config
def _headers() -> dict:
return {"X-Internal-Token": config.FETCHER_INTERNAL_TOKEN}
def get_data(username: str) -> dict:
r = requests.get(f"{config.FETCHER_URL}/data/{username}", headers=_headers(), timeout=30)
r.raise_for_status()
return r.json()
def force_fetch(username: str) -> dict:
r = requests.post(f"{config.FETCHER_URL}/fetch", headers=_headers(),
json={"username": username}, timeout=30)
r.raise_for_status()
return r.json()
+37
View File
@@ -0,0 +1,37 @@
import responses
import pytest
from src import fetcher_client as fc
@pytest.fixture(autouse=True)
def cfg(monkeypatch):
monkeypatch.setattr(fc.config, "FETCHER_URL", "http://fetcher:5001")
monkeypatch.setattr(fc.config, "FETCHER_INTERNAL_TOKEN", "secret")
@responses.activate
def test_get_data_returns_payload_and_hash():
responses.add(
responses.GET, "http://fetcher:5001/data/alice",
json={"data": {"user": {"login": "alice"}}, "payload_hash": "abc"},
status=200,
)
result = fc.get_data("alice")
assert result["payload_hash"] == "abc"
assert result["data"]["user"]["login"] == "alice"
@responses.activate
def test_get_data_sends_auth_header():
responses.add(responses.GET, "http://fetcher:5001/data/alice",
json={"data": {}, "payload_hash": "h"}, status=200)
fc.get_data("alice")
assert responses.calls[0].request.headers["X-Internal-Token"] == "secret"
@responses.activate
def test_force_fetch():
responses.add(responses.POST, "http://fetcher:5001/fetch",
json={"stored": True, "payload_hash": "new", "changed": True}, status=200)
r = fc.force_fetch("alice")
assert r["changed"] is True