Skip to content

Commit

Permalink
Merge pull request #792 from NatLibFi/fix-http-backend-params-not-pas…
Browse files Browse the repository at this point in the history
…sed-to-request

Fix limit parameter not passed to requests by HTTP backend
  • Loading branch information
juhoinkinen authored Jul 1, 2024
2 parents 3b5f7a1 + 011e602 commit e6a20fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions annif/backend/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def _suggest(self, text: str, params: dict[str, Any]) -> list[SubjectSuggestion]
data = {"text": text}
if "project" in params:
data["project"] = params["project"]
if "limit" in params:
data["limit"] = params["limit"]

try:
req = requests.post(params["endpoint"], data=data, headers=self.headers)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_backend_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ def test_http_suggest_with_results(app_project):
assert hits[0].score == 1.0


def test_http_suggest_post_args(app_project):
with unittest.mock.patch("requests.post"):
http_type = annif.backend.get_backend("http")
http = http_type(
backend_id="http",
config_params={
"endpoint": "http://api.example.org/analyze",
"project": "dummy",
"limit": "42",
},
project=app_project,
)
http.suggest(["this is some text"])

assert requests.post.call_args.args == ("http://api.example.org/analyze",)
assert "text" in requests.post.call_args.kwargs["data"]
assert requests.post.call_args.kwargs["data"]["text"] == "this is some text"
assert "project" in requests.post.call_args.kwargs["data"]
assert requests.post.call_args.kwargs["data"]["project"] == "dummy"
assert "limit" in requests.post.call_args.kwargs["data"]
assert requests.post.call_args.kwargs["data"]["limit"] == "42"


def test_http_suggest_zero_score(project):
with unittest.mock.patch("requests.post") as mock_request:
# create a mock response whose .json() method returns the list that we
Expand Down

0 comments on commit e6a20fc

Please sign in to comment.