feat: add rate limiting, retry with backoff, and max count cap
- Add configurable request delay between paginated API calls (default 1.5s) - Add retry with exponential backoff on HTTP 429 and Twitter error code 88 - Add hard max count cap (default 200, absolute ceiling 500) - Add rateLimit config section with requestDelay, maxRetries, retryBaseDelay, maxCount - Add normalization tests for rateLimit config
This commit is contained in:
@@ -12,7 +12,7 @@ def test_cli_user_command_works_with_client_factory(monkeypatch) -> None:
|
||||
def fetch_user(self, screen_name: str) -> UserProfile:
|
||||
return UserProfile(id="1", name="Alice", screen_name=screen_name)
|
||||
|
||||
monkeypatch.setattr("twitter_cli.cli._get_client", lambda: FakeClient())
|
||||
monkeypatch.setattr("twitter_cli.cli._get_client", lambda config=None: FakeClient())
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, ["user", "alice"])
|
||||
assert result.exit_code == 0
|
||||
|
||||
@@ -33,3 +33,30 @@ def test_filter_normalization_for_invalid_values(tmp_path: Path) -> None:
|
||||
assert config["filter"]["lang"] == []
|
||||
assert config["filter"]["weights"]["likes"] == 1.0
|
||||
assert config["filter"]["weights"]["retweets"] == 4.0
|
||||
# rateLimit should get defaults since it wasn't in the yaml
|
||||
assert config["rateLimit"]["requestDelay"] == 1.5
|
||||
assert config["rateLimit"]["maxRetries"] == 3
|
||||
assert config["rateLimit"]["retryBaseDelay"] == 5.0
|
||||
assert config["rateLimit"]["maxCount"] == 200
|
||||
|
||||
|
||||
def test_rate_limit_normalization(tmp_path: Path) -> None:
|
||||
config_file = tmp_path / "config.yaml"
|
||||
config_file.write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"rateLimit:",
|
||||
" requestDelay: -2",
|
||||
" maxRetries: bad",
|
||||
" retryBaseDelay: 0.1",
|
||||
" maxCount: 0",
|
||||
]
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
config = load_config(str(config_file))
|
||||
assert config["rateLimit"]["requestDelay"] == 0.0 # clamped to >= 0
|
||||
assert config["rateLimit"]["maxRetries"] == 3 # fallback to default
|
||||
assert config["rateLimit"]["retryBaseDelay"] == 1.0 # clamped to >= 1.0
|
||||
assert config["rateLimit"]["maxCount"] == 1 # clamped to >= 1
|
||||
|
||||
Reference in New Issue
Block a user