Skip to content

Commit

Permalink
Check if the value is a string, an iterable, or other, in that order …
Browse files Browse the repository at this point in the history
…(order is important, as a string is also an iterable). Then do the replacements.
  • Loading branch information
cgrtrifork committed Mar 20, 2020
1 parent 146c2c5 commit 8b61842
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __pycache__/
# Distribution / packaging
.Python
env/
venv/
build/
develop-eggs/
dist/
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
keywords='stackexchange',
packages=find_packages(exclude=['contrib', 'docs', 'tests*', 'test']),
version='0.1.12',
install_requires=['requests'],
install_requires=['requests', 'six'],
tests_require=['mock'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
16 changes: 15 additions & 1 deletion stackapi/stackapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import calendar
import requests.compat
import six


class StackAPIError(Exception):
Expand Down Expand Up @@ -148,7 +149,20 @@ def fetch(self, endpoint=None, page=1, key=None, filter='default', **kwargs):
# badges/222;1306;99999
for k, value in list(kwargs.items()):
if "{" + k + "}" in endpoint:
endpoint = endpoint.replace("{"+k+"}", ';'.join(requests.compat.quote_plus(str(x)) for x in value))
# using six for backwards compatibility
if isinstance(value, six.string_types):
endpoint = endpoint.replace("{" + k + "}", requests.compat.quote_plus(str(value)))
else:
# check if value is iterable, based on
# https://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable
# notice that a string is also an iterable, that's why it's checked first
try:
iterator = iter(value)
endpoint = endpoint.replace("{" + k + "}", ';'.join(requests.compat.quote_plus(str(x)) for x in iterator))
except TypeError:
# it's not an iterable, represent as string
endpoint = endpoint.replace("{" + k + "}", requests.compat.quote_plus(str(value)))

kwargs.pop(k, None)

date_time_keys = ['fromdate', 'todate', 'since', 'min', 'max']
Expand Down

0 comments on commit 8b61842

Please sign in to comment.