Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
Parameters that weren't in the keys or values of BaseSearch.attr_map
were being dropped in the final query rather than as-is. Fixed
  • Loading branch information
arcward committed Apr 14, 2017
1 parent 619f5f8 commit 9613a5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
42 changes: 23 additions & 19 deletions ticketpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ def _search(self, method, **kwargs):

return PageIterator(self, **response)

@property
def api_key(self):
"""API key header to pass with API requests"""
return {'apikey': self.__api_key}

@api_key.setter
def api_key(self, api_key):
self.__api_key = api_key

@property
def url(self):
"""Root URL"""
return "{}/discovery/{}".format(self.base_url, self.version)

def __method_url(self, method):
return "{}/{}.{}".format(self.url, method, self.response_type)

@property
def events_url(self):
"""URL for */events/*"""
Expand All @@ -97,14 +103,15 @@ def classifications_url(self):
"""URL for */attractions/*"""
return self.__method_url('classifications')

@property
def api_key(self):
"""API key header to pass with API requests"""
return {'apikey': self.__api_key}
def __method_url(self, method):
"""Formats search method URL
:param method: Method (ex: 'events' 'venues' ...)
:return: Search method URL
"""
return "{}/{}.{}".format(self.url, method, self.response_type)


@api_key.setter
def api_key(self, api_key):
self.__api_key = api_key

# noinspection PyPep8,PySimplifyBooleanCheck
@staticmethod
Expand Down Expand Up @@ -144,15 +151,12 @@ def __str__(self):
"Status: {status}")
msgs = []
for e in self.errors:
msgs.append(tmpl.format(
url=self.url,
sp=', '.join('({}={})'.format(k, v) for
(k, v) in self.params.items()),
code=e['code'],
status=e['status'],
detail=e['detail'],
link=e['_links']['about']['href']
))
sp_joined = ', '.join('({}={})'.format(k, v)
for (k, v) in self.params.items())
msgs.append(tmpl.format(url=self.url, code=e['code'],
status=e['status'], detail=e['detail'],
link=e['_links']['about']['href'],
sp=sp_joined))
return '\n'.join(msgs)


Expand Down
5 changes: 4 additions & 1 deletion ticketpy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class BaseQuery:
'page': 'page',
'size': 'size',
'locale': 'locale',
'latlong': 'latlong'
'latlong': 'latlong',
'radius': 'radius'
}

def __init__(self, api_client, method, model):
Expand Down Expand Up @@ -109,6 +110,8 @@ def _search_params(self, **kwargs):
kw_map[self.attr_map[k]] = v
elif k in self.attr_map.values():
kw_map[k] = v
else:
kw_map[k] = v

return {k: v for (k, v) in kw_map.items() if v is not None}

Expand Down
21 changes: 19 additions & 2 deletions ticketpy/tests/test_ticketpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,26 @@ def test_venues_url(self):
expected_url = "https://app.ticketmaster.com/discovery/v2/venues.json"
self.assertEqual(self.api_client.venues_url, expected_url)

def test__search(self):
def test__bad_request(self):
# Should be 'events' or 'venues' and anything else: ValueError!
self.assertRaises(ValueError, self.api_client._search, 'asdf')

# Radius should be a whole number, so 1.5 should raise ApiException
radius = '1.5'
lat = '33.7838737'
long = '-84.366088'

self.assertRaises(ApiException, self.api_client._search, 'events',
latlon='33.7838737, -84.366088', radius='1.5')
latlon="{},{}".format(lat, long), radius=radius)

# Make sure ApiException.__str__() hasn't broken for some reason...
try:
r = self.api_client.events.by_location(latitude=lat,
longitude=long,
radius=radius)
except ApiException as e:
print(e)


def test___yes_no_only(self):
yno = self.api_client._ApiClient__yes_no_only
Expand Down Expand Up @@ -163,6 +176,10 @@ def test_classification_segment(self):
self.assertEqual(subgenre_id, sg.id)
self.assertEqual(subgenre_name, sg.name)

fake_id = "afkjsdlfjkasdf"
fake_response = self.tm.classifications.by_id(fake_id)
self.assertIsNone(fake_response)

def test_get_event_id(self):
event_id = 'vvG1zZfbJQpVWp'
e = self.tm.events.by_id(event_id)
Expand Down

0 comments on commit 9613a5e

Please sign in to comment.