Skip to content

Commit 3463f2a

Browse files
Add "providers" argument to "search" function
Since it's very similar to "popular" function it can also be used in "search". Docstrings need to be updated. Unit tests should be updated.
1 parent 0416014 commit 3463f2a

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/simplejustwatchapi/justwatch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def search(
2828
count: int = 4,
2929
best_only: bool = True,
3030
offset: int = 0,
31+
providers: list[str] | str | None = None,
3132
) -> list[MediaEntry]:
3233
"""
3334
Search JustWatch for given title.
@@ -62,7 +63,7 @@ def search(
6263
httpx.HTTPStatusError: If JustWatch API doesn't respond with success code.
6364
6465
"""
65-
request = prepare_search_request(title, country, language, count, best_only, offset)
66+
request = prepare_search_request(title, country, language, count, best_only, offset, providers)
6667
response = post(_GRAPHQL_API_URL, json=request)
6768
response.raise_for_status()
6869
return parse_search_response(response.json())

src/simplejustwatchapi/query.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def prepare_search_request(
3636
count: int,
3737
best_only: bool,
3838
offset: int,
39+
providers: list[str] | str | None,
3940
) -> dict:
4041
"""
4142
Prepare search request for JustWatch GraphQL API.
@@ -63,7 +64,7 @@ def prepare_search_request(
6364
"operationName": "GetSearchTitles",
6465
"variables": {
6566
"first": count,
66-
"searchTitlesFilter": {"searchQuery": title},
67+
"searchTitlesFilter": {"searchQuery": title, "packages": providers},
6768
"language": language,
6869
"country": country.upper(),
6970
"formatPoster": "JPG",

test/simplejustwatchapi/test_justwatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
JUSTWATCH_GRAPHQL_URL = "https://apis.justwatch.com/graphql"
88

9-
SEARCH_INPUT = ("TITLE", "COUNTRY", "LANGUAGE", 5, True, 10)
9+
SEARCH_INPUT = ("TITLE", "COUNTRY", "LANGUAGE", 5, True, 10, ["prov1", "prov2"])
1010
DETAILS_INPUT = ("NODE ID", "COUNTRY", "LANGUAGE", False)
1111
OFFERS_COUNTRIES_INPUT = {"COUNTRY1", "COUNTRY2", "COUNTRY3"}
1212
OFFERS_INPUT = ("NODE ID", OFFERS_COUNTRIES_INPUT, "LANGUAGE", True)

test/simplejustwatchapi/test_request.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020

2121
@patch("simplejustwatchapi.query.graphql_search_query", return_value=DUMMY_SEARCH_QUERY)
2222
@mark.parametrize(
23-
argnames=("title", "country", "language", "count", "best_only", "offset"),
23+
argnames=("title", "country", "language", "count", "best_only", "offset", "providers"),
2424
argvalues=[
25-
("TITLE 1", "US", "language 1", 5, True, 0),
26-
("TITLE 2", "gb", "language 2", 10, False, 20),
25+
("TITLE 1", "US", "language 1", 5, True, 0, ""),
26+
("TITLE 2", "gb", "language 2", 10, False, 20, ["provider1", "provider2"]),
27+
("TITLE 3", "fr", "language 3", 20, True, 20, "provider3"),
28+
("TITLE 4", "it", "language 4", 30, True, 30, []),
29+
("TITLE 5", "dk", "language 5", 40, True, 40, None),
2730
],
2831
)
2932
def test_prepare_search_request(
@@ -34,12 +37,13 @@ def test_prepare_search_request(
3437
count: int,
3538
best_only: bool,
3639
offset: int,
40+
providers: list[str] | str | None,
3741
):
3842
expected_request = {
3943
"operationName": "GetSearchTitles",
4044
"variables": {
4145
"first": count,
42-
"searchTitlesFilter": {"searchQuery": title},
46+
"searchTitlesFilter": {"searchQuery": title, "packages": providers},
4347
"language": language,
4448
"country": country.upper(),
4549
"formatPoster": "JPG",
@@ -51,7 +55,7 @@ def test_prepare_search_request(
5155
},
5256
"query": DUMMY_SEARCH_QUERY,
5357
}
54-
request = prepare_search_request(title, country, language, count, best_only, offset)
58+
request = prepare_search_request(title, country, language, count, best_only, offset, providers)
5559
assert expected_request == request
5660

5761

@@ -69,7 +73,7 @@ def test_prepare_search_request_asserts_on_invalid_country_code(
6973
):
7074
expected_error_message = f"Invalid country code: {invalid_code}, code must be 2 characters long"
7175
with raises(AssertionError) as error:
72-
prepare_search_request("", invalid_code, "", 1, True, 2)
76+
prepare_search_request("", invalid_code, "", 1, True, 2, None)
7377
assert str(error.value) == expected_error_message
7478
query_mock.assert_not_called()
7579

0 commit comments

Comments
 (0)