Skip to content
Closed
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 mediacloud/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import importlib.metadata
import logging
from typing import Any, Dict, List, Optional, Union
import warnings

import requests

Expand Down Expand Up @@ -61,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"
Expand Down Expand Up @@ -136,6 +139,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()
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()
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,
platform=(platform or self.PROVIDER))
if len(source_ids):
Expand Down
25 changes: 24 additions & 1 deletion mediacloud/test/api_search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import time
from unittest import TestCase

import pytest
import mediacloud.api

COLLECTION_US_NATIONAL = 34412234
Expand Down Expand Up @@ -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
Loading