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
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
#########

Expand Down
5 changes: 4 additions & 1 deletion predicthq/endpoints/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions tests/endpoints/v1/events_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down