Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions asknews_sdk/api/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def search_news(
geo_radius: Optional[float] = None,
geo_polygon: Optional[str] = None,
sort_by: Optional[Literal["relevance", "pub_date"]] = None,
podcasts: Literal["include", "only", "none"] = "include",
*,
http_headers: Optional[Dict] = None,
) -> SearchResponse:
Expand Down Expand Up @@ -197,6 +198,10 @@ def search_news(
:type domain_url: Optional[str]
:param page_rank: Page rank, defaults to None
:type page_rank: Optional[int]
:param podcasts: Control whether podcasts are included in search results. 'include'
searches news and podcasts, 'only' searches podcasts only, and 'none'
excludes podcasts. Defaults to 'include'.
:type podcasts: Literal["include", "only", "none"]
:param http_headers: Additional HTTP headers.
:type http_headers: Optional[Dict]
:return: The search response.
Expand All @@ -223,6 +228,7 @@ def search_news(
"reporting_voice": reporting_voice,
"domain_url": domain_url,
"bad_domain_url": bad_domain_url,
"podcasts": podcasts,
"page_rank": page_rank,
"diversify_sources": diversify_sources,
"strategy": strategy,
Expand Down Expand Up @@ -627,13 +633,19 @@ async def search_news(
geo_radius: Optional[float] = None,
geo_polygon: Optional[str] = None,
sort_by: Optional[Literal["relevance", "pub_date"]] = None,
podcasts: Literal["include", "only", "none"] = "include",
*,
http_headers: Optional[Dict] = None,
) -> SearchResponse:
"""
Get time-series counts for a filter
Search for news articles given a query.

https://docs.asknews.app/en/reference#get-/v1/index_counts
https://docs.asknews.app/en/reference#get-/v1/news/search

:param podcasts: Control whether podcasts are included in search results. 'include'
searches news and podcasts, 'only' searches podcasts only, and 'none'
excludes podcasts. Defaults to 'include'.
:type podcasts: Literal["include", "only", "none"]
"""
response = await self.client.request(
method="GET",
Expand All @@ -656,6 +668,7 @@ async def search_news(
"reporting_voice": reporting_voice,
"domain_url": domain_url,
"bad_domain_url": bad_domain_url,
"podcasts": podcasts,
"page_rank": page_rank,
"diversify_sources": diversify_sources,
"strategy": strategy,
Expand Down
14 changes: 12 additions & 2 deletions tests/api/test_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ async def test_async_news_api_get_article(async_news_api: AsyncNewsAPI, response
assert mock_route.calls.last.response.status_code == 404


def test_sync_news_api_search_news(sync_news_api: NewsAPI, response_mock: MockRouter):
@pytest.mark.parametrize("podcasts", ["include", "only", "none"])
def test_sync_news_api_search_news(
sync_news_api: NewsAPI, response_mock: MockRouter, podcasts: str
):
mock_search_response = MockSearchResponse.build()

mock_route = response_mock.get("/v1/news/search").respond(
Expand All @@ -129,6 +132,7 @@ def test_sync_news_api_search_news(sync_news_api: NewsAPI, response_mock: MockRo

response = sync_news_api.search_news(
"query",
podcasts=podcasts,
http_headers={
"custom-header": "custom-value",
}
Expand All @@ -140,13 +144,17 @@ def test_sync_news_api_search_news(sync_news_api: NewsAPI, response_mock: MockRo

assert mock_route.called
assert mock_route.calls.last.request.url.path == "/v1/news/search"
assert mock_route.calls.last.request.url.params["podcasts"] == podcasts
assert mock_route.calls.last.request.method == "GET"
assert mock_route.calls.last.request.headers["accept"] == SearchResponse.__content_type__
assert mock_route.calls.last.request.headers["custom-header"] == "custom-value"
assert mock_route.calls.last.response.status_code == 200


async def test_async_news_api_search_news(async_news_api: AsyncNewsAPI, response_mock: MockRouter):
@pytest.mark.parametrize("podcasts", ["include", "only", "none"])
async def test_async_news_api_search_news(
async_news_api: AsyncNewsAPI, response_mock: MockRouter, podcasts: str
):
mock_search_response = MockSearchResponse.build()

mock_route = response_mock.get("/v1/news/search").respond(
Expand All @@ -155,6 +163,7 @@ async def test_async_news_api_search_news(async_news_api: AsyncNewsAPI, response

response = await async_news_api.search_news(
"query",
podcasts=podcasts,
http_headers={
"custom-header": "custom-value",
}
Expand All @@ -166,6 +175,7 @@ async def test_async_news_api_search_news(async_news_api: AsyncNewsAPI, response

assert mock_route.called
assert mock_route.calls.last.request.url.path == "/v1/news/search"
assert mock_route.calls.last.request.url.params["podcasts"] == podcasts
assert mock_route.calls.last.request.method == "GET"
assert mock_route.calls.last.request.headers["accept"] == SearchResponse.__content_type__
assert mock_route.calls.last.request.headers["custom-header"] == "custom-value"
Expand Down
Loading