mirror of
https://github.com/calesthio/OpenMontage.git
synced 2026-08-15 13:14:38 +08:00
Clamp Pixabay per_page to API-required 3-200 range
Pixabay rejects per_page outside 3-200 with HTTP 400. The stock_sources adapter already clamped, but the PixabayVideo and PixabayImage tools passed the value through raw, so callers using per_page < 3 got a 400. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
62
tests/tools/test_pixabay_per_page_clamp.py
Normal file
62
tests/tools/test_pixabay_per_page_clamp.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""Pixabay rejects per_page outside 3-200 with HTTP 400, so every
|
||||
Pixabay caller must clamp before building the request."""
|
||||
|
||||
import requests
|
||||
|
||||
from tools.graphics.pixabay_image import PixabayImage
|
||||
from tools.video.pixabay_video import PixabayVideo
|
||||
from tools.video.stock_sources.base import SearchFilters
|
||||
from tools.video.stock_sources.pixabay_video import PixabayVideoSource
|
||||
|
||||
|
||||
class _FakeResponse:
|
||||
def raise_for_status(self):
|
||||
pass
|
||||
|
||||
def json(self):
|
||||
return {"hits": [], "total": 0}
|
||||
|
||||
|
||||
def _capture_get(captured):
|
||||
def fake_get(url, params=None, timeout=None, **kwargs):
|
||||
captured["params"] = params
|
||||
return _FakeResponse()
|
||||
|
||||
return fake_get
|
||||
|
||||
|
||||
def test_pixabay_video_tool_clamps_per_page(monkeypatch):
|
||||
monkeypatch.setenv("PIXABAY_API_KEY", "test-key")
|
||||
captured = {}
|
||||
monkeypatch.setattr(requests, "get", _capture_get(captured))
|
||||
|
||||
PixabayVideo().execute({"query": "sky", "per_page": 1})
|
||||
assert captured["params"]["per_page"] == 3
|
||||
|
||||
PixabayVideo().execute({"query": "sky", "per_page": 500})
|
||||
assert captured["params"]["per_page"] == 200
|
||||
|
||||
|
||||
def test_pixabay_image_tool_clamps_per_page(monkeypatch):
|
||||
monkeypatch.setenv("PIXABAY_API_KEY", "test-key")
|
||||
captured = {}
|
||||
monkeypatch.setattr(requests, "get", _capture_get(captured))
|
||||
|
||||
PixabayImage().execute({"query": "sky", "per_page": 1})
|
||||
assert captured["params"]["per_page"] == 3
|
||||
|
||||
PixabayImage().execute({"query": "sky", "per_page": 500})
|
||||
assert captured["params"]["per_page"] == 200
|
||||
|
||||
|
||||
def test_pixabay_stock_source_clamps_per_page(monkeypatch):
|
||||
monkeypatch.setenv("PIXABAY_API_KEY", "test-key")
|
||||
captured = {}
|
||||
monkeypatch.setattr(requests, "get", _capture_get(captured))
|
||||
|
||||
source = PixabayVideoSource()
|
||||
source.search("sky", SearchFilters(per_page=1))
|
||||
assert captured["params"]["per_page"] == 3
|
||||
|
||||
source.search("sky", SearchFilters(per_page=500))
|
||||
assert captured["params"]["per_page"] == 200
|
||||
@@ -127,7 +127,8 @@ class PixabayImage(BaseTool):
|
||||
params: dict[str, Any] = {
|
||||
"key": api_key,
|
||||
"q": query,
|
||||
"per_page": inputs.get("per_page", 5),
|
||||
# Pixabay rejects per_page outside 3-200 with HTTP 400
|
||||
"per_page": max(3, min(inputs.get("per_page", 5), 200)),
|
||||
"page": inputs.get("page", 1),
|
||||
"safesearch": str(inputs.get("safesearch", True)).lower(),
|
||||
}
|
||||
|
||||
@@ -130,7 +130,8 @@ class PixabayVideo(BaseTool):
|
||||
params: dict[str, Any] = {
|
||||
"key": api_key,
|
||||
"q": query,
|
||||
"per_page": inputs.get("per_page", 5),
|
||||
# Pixabay rejects per_page outside 3-200 with HTTP 400
|
||||
"per_page": max(3, min(inputs.get("per_page", 5), 200)),
|
||||
"page": inputs.get("page", 1),
|
||||
"safesearch": str(inputs.get("safesearch", True)).lower(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user