diff --git a/README.rst b/README.rst index 286313e..ff4eb31 100644 --- a/README.rst +++ b/README.rst @@ -27,12 +27,12 @@ Usage .. code-block:: python from predicthq import Client - + phq = Client(access_token="$ACCESS_TOKEN") - - for event in phq.events.search(q="Foo Fighters", rank_level=[4, 5], country='US'): + + for event in phq.events.search(q="Foo Fighters", rank_level=[4, 5], place={"scope": ["5391959", "5368361"]}): print(event.rank, event.category, event.title, event.start.strftime('%Y-%m-%d'))) - + Endpoints ######### diff --git a/predicthq/endpoints/decorators.py b/predicthq/endpoints/decorators.py index 5a1b4a5..63f449c 100644 --- a/predicthq/endpoints/decorators.py +++ b/predicthq/endpoints/decorators.py @@ -22,7 +22,10 @@ def _to_url_params(data, glue=".", separator=","): params[key] = separator.join(map(six.text_type, value)) elif isinstance(value, dict): for subkey, subvalue in six.iteritems(value): - params[glue.join((key, subkey))] = subvalue + if isinstance(subvalue, list): + params[glue.join((key, subkey))] = separator.join(map(six.text_type, subvalue)) + else: + params[glue.join((key, subkey))] = subvalue else: params[key] = value return params diff --git a/tests/endpoints/v1/events_tests.py b/tests/endpoints/v1/events_tests.py index 517fb2b..0b3c241 100644 --- a/tests/endpoints/v1/events_tests.py +++ b/tests/endpoints/v1/events_tests.py @@ -12,17 +12,39 @@ class EventsTest(unittest.TestCase): @with_mock_client() - def test_search_params(self, client): + def test_search_params_underscores(self, client): client.events.search(id="id", q="query", rank_level=[4,5], rank__gt=85, country=["NZ", "AU"], within__radius="2km", within__longitude=-71.0432, within__latitude=42.346, label=["label1", "label2"], category="category", + place__scope=["place1", "place2"], place__exact=["place3"], start__gte="2016-03-01", start__lt=datetime(2016, 4, 1), start__tz="Pacific/Auckland", - signal__id='zVNLr8tHvWQw', signal__explain=datetime(2016, 4, 1)) + active__gte="2016-03-01", active__lt=datetime(2016, 4, 1), active__tz="Pacific/Auckland", + signal__id="zVNLr8tHvWQw", signal__explain=datetime(2016, 4, 1)) client.request.assert_called_once_with('get', '/v1/events/', params={ 'id': 'id', 'rank.gt': 85, 'rank_level': '4,5', 'category': 'category', 'country': 'NZ,AU', 'within': '2km@42.346,-71.0432', 'label': 'label1,label2', 'q': 'query', + 'place.scope': 'place1,place2', 'place.exact': 'place3', 'start.lt': '2016-04-01T00:00:00.000000', 'start.gte': '2016-03-01T00:00:00.000000', 'start.tz': 'Pacific/Auckland', + 'active.lt': '2016-04-01T00:00:00.000000', 'active.gte': '2016-03-01T00:00:00.000000', 'active.tz': 'Pacific/Auckland', + 'signal.id': 'zVNLr8tHvWQw', 'signal.explain': '2016-04-01'}) + + @with_mock_client() + def test_search_params_dicts(self, client): + client.events.search(id="id", q="query", rank_level=[4,5], rank={"gt": 85}, country=["NZ", "AU"], + within={"radius": "2km", "longitude": -71.0432, "latitude": 42.346}, + label=["label1", "label2"], category="category", + place={"scope": ["place1", "place2"], "exact": "place3"}, + start={"gte": "2016-03-01", "lt": datetime(2016, 4, 1), "tz": "Pacific/Auckland"}, + active={"gte": "2016-03-01", "lt": datetime(2016, 4, 1), "tz": "Pacific/Auckland"}, + signal={"id": "zVNLr8tHvWQw", "explain": datetime(2016, 4, 1)}) + + client.request.assert_called_once_with('get', '/v1/events/', params={ + 'id': 'id', 'rank.gt': 85, 'rank_level': '4,5', 'category': 'category', 'country': 'NZ,AU', + 'within': '2km@42.346,-71.0432', 'label': 'label1,label2', 'q': 'query', + 'place.scope': 'place1,place2', 'place.exact': 'place3', + 'start.lt': '2016-04-01T00:00:00.000000', 'start.gte': '2016-03-01T00:00:00.000000', 'start.tz': 'Pacific/Auckland', + 'active.lt': '2016-04-01T00:00:00.000000', 'active.gte': '2016-03-01T00:00:00.000000', 'active.tz': 'Pacific/Auckland', 'signal.id': 'zVNLr8tHvWQw', 'signal.explain': '2016-04-01'}) @with_mock_client()