Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify get params to use api_key defined in params #85

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,42 @@ def test_tmdb_get_params_bool(self):
search.movie(query=MOVIEQUERY3, include_adult='true')
total_results2 = search.total_results
self.assertEqual(total_results1, total_results2)

# _get_params (1) adds the api key as a parameter and (2) turns all booleans into their string form.
# this test ensures these pieces of functionality when api_key is defined globally.
def test_get_params_only_api_key(self):
tmdb.API_KEY = API_KEY
tmdb_object = tmdb.base.TMDB()
expected = {'api_key': API_KEY}
actual = tmdb_object._get_params(params={})
self.assertEqual(actual, expected)

# confirm _get_params will error when API_KEY is not defined
def test_get_params_api_key_undefined(self):
tmdb_no_api_defined = tmdb.base.TMDB()
tmdb.API_KEY = None
with self.assertRaises(tmdb.base.APIKeyError):
tmdb_no_api_defined._get_params(params={})
tmdb.API_KEY = API_KEY
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everytime I change the API_KEY to None, I "reset" it like so to preserve the other tests


# confirm _get_params will NOT error when API_KEY is not defined but is added to the params
# also confirm boolean conversion works
def test_get_params_api_key_undefined_but_in_parameters(self):
tmdb_no_api_defined = tmdb.base.TMDB()
tmdb.API_KEY = None
input_params = {'api_key': API_KEY, 'one_plus_one_is_two': True, 'cats=dogs': False}
expected = {'api_key': API_KEY, 'one_plus_one_is_two': 'true', 'cats=dogs': 'false'}
actual = tmdb_no_api_defined._get_params(params=input_params)
self.assertEqual(actual, expected)
tmdb.API_KEY = API_KEY

# simulate a normal search query with no API key previously defined
def test_tmdb_get_params_no_api_with_search(self):
search = tmdb.Search()
tmdb.API_KEY = None
search.movie(query=MOVIEQUERY3, include_adult=True, api_key=API_KEY)
total_results1 = search.total_results
search.movie(query=MOVIEQUERY3, include_adult='true', api_key=API_KEY)
total_results2 = search.total_results
self.assertEqual(total_results1, total_results2)
tmdb.API_KEY = API_KEY
21 changes: 10 additions & 11 deletions tmdbsimple/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,18 @@ def _get_complete_url(self, path):
return '{base_uri}/{path}'.format(base_uri=self.base_uri, path=path)

def _get_params(self, params):
from . import API_KEY
if not API_KEY:
raise APIKeyError
params = params if params else {}

api_dict = {'api_key': API_KEY}
if params:
params.update(api_dict)
for key, value in params.items():
if isinstance(params[key], bool):
params[key] = 'true' if value is True else 'false'
if not params.get('api_key'):
from . import API_KEY
if not API_KEY:
raise APIKeyError
params['api_key'] = API_KEY

for key, value in params.items():
if isinstance(value, bool):
params[key] = str(value).lower()

else:
params = api_dict
return params

def _request(self, method, path, params=None, payload=None):
Expand Down