From f497b487080ffcfa2d4ae36fef329e4be12ee036 Mon Sep 17 00:00:00 2001 From: Paige Gulley Date: Wed, 8 Oct 2025 12:24:21 -0400 Subject: [PATCH 1/4] superficial fix --- mediacloud/api.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mediacloud/api.py b/mediacloud/api.py index d514d91..5a49b0d 100644 --- a/mediacloud/api.py +++ b/mediacloud/api.py @@ -2,6 +2,7 @@ import importlib.metadata import logging from typing import Any, Dict, List, Optional, Union +import warning import requests @@ -136,6 +137,16 @@ class SearchApi(BaseApi): def _prep_default_params(self, query: str, start_date: dt.date, end_date: dt.date, collection_ids: Optional[List[int]] = [], source_ids: Optional[List[int]] = [], platform: Optional[str] = None): + + if isinstance(start_date, dt.datetime): + start_date = start_date.date() + warning.warn("start_date was passed as datetime, but expected as date, and has been recast") + + if isinstance(end_date, dt.datetime): + end_date = end_date.date() + warning.warn("end_date was passed as datetime, but expected as date, and has been recast") + + params: Dict[Any, Any] = dict(start=start_date.isoformat(), end=end_date.isoformat(), q=query, platform=(platform or self.PROVIDER)) if len(source_ids): From 7851ec09fe09b599a7d27e0b36bd963821ea0eec Mon Sep 17 00:00:00 2001 From: Paige Gulley Date: Wed, 8 Oct 2025 12:28:28 -0400 Subject: [PATCH 2/4] I'ms warnings yous --- mediacloud/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mediacloud/api.py b/mediacloud/api.py index 5a49b0d..7ab8b41 100644 --- a/mediacloud/api.py +++ b/mediacloud/api.py @@ -2,7 +2,7 @@ import importlib.metadata import logging from typing import Any, Dict, List, Optional, Union -import warning +import warnings import requests @@ -140,11 +140,11 @@ def _prep_default_params(self, query: str, start_date: dt.date, end_date: dt.dat if isinstance(start_date, dt.datetime): start_date = start_date.date() - warning.warn("start_date was passed as datetime, but expected as date, and has been recast") + warnings.warn("start_date was passed as datetime, but expected as date, and has been recast") if isinstance(end_date, dt.datetime): end_date = end_date.date() - warning.warn("end_date was passed as datetime, but expected as date, and has been recast") + warnings.warn("end_date was passed as datetime, but expected as date, and has been recast") params: Dict[Any, Any] = dict(start=start_date.isoformat(), end=end_date.isoformat(), q=query, From 2fb66837fba0517708ace7955b6dff5b952906aa Mon Sep 17 00:00:00 2001 From: Paige Gulley Date: Wed, 8 Oct 2025 13:58:20 -0400 Subject: [PATCH 3/4] Added test --- mediacloud/test/api_search_test.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/mediacloud/test/api_search_test.py b/mediacloud/test/api_search_test.py index 434bbd2..8b9327f 100644 --- a/mediacloud/test/api_search_test.py +++ b/mediacloud/test/api_search_test.py @@ -2,7 +2,7 @@ import os import time from unittest import TestCase - +import pytest import mediacloud.api COLLECTION_US_NATIONAL = 34412234 @@ -323,3 +323,26 @@ def test_negation_source(self): assert not_count > 0 assert not_count < all_count assert minus_count == not_count + +class SearchErrorHandlingTest(TestCase): + #New test cases for how the api handles bad input and errors from the server. + + START_DATE = dt.date(2024, 1, 1) + END_DATE = dt.date(2024, 1, 30) + START_DATETIME = dt.datetime(2024, 1, 1) + END_DATETIME = dt.datetime(2024, 1, 30) + + def setUp(self): + self._mc_api_key = os.getenv("MC_API_TOKEN") + self._search = mediacloud.api.SearchApi(self._mc_api_key) + + def test_datetime(self): + query = "biden" + result_via_date = self._search.story_count(query=query, start_date=self.START_DATE, end_date=self.END_DATE, + collection_ids=[COLLECTION_US_NATIONAL])['relevant'] + + with pytest.warns(UserWarning): + result_via_datetime = self._search.story_count(query=query, start_date=self.START_DATETIME, end_date=self.END_DATETIME, + collection_ids=[COLLECTION_US_NATIONAL])['relevant'] + + assert result_via_date == result_via_datetime From 7c61d46c144bcb4a12e999c8a95d192de28ddaee Mon Sep 17 00:00:00 2001 From: Paige Gulley Date: Wed, 22 Oct 2025 12:47:48 -0400 Subject: [PATCH 4/4] server provided note added to exception text --- mediacloud/api.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mediacloud/api.py b/mediacloud/api.py index 7ab8b41..0abc39a 100644 --- a/mediacloud/api.py +++ b/mediacloud/api.py @@ -62,11 +62,13 @@ def _query(self, endpoint: str, params: Optional[Dict] = None, method: str = 'GE r = self._session.post(endpoint_url, json=params, timeout=self.TIMEOUT_SECS) else: raise RuntimeError(f"Unsupported method of '{method}'") - if r.status_code != 200: - raise RuntimeError(f"API Server Error {r.status_code}. Params: {params}") + if r.status_code != 200: + message = r.json()["note"] + raise RuntimeError(f"API Server Error {r.status_code}. MESSAGE: {message}. PARAMS: {params}") return r.json() + class DirectoryApi(BaseApi): PLATFORM_ONLINE_NEWS = "online_news"