Skip to content

Commit

Permalink
fix __api_url_params issue
Browse files Browse the repository at this point in the history
  • Loading branch information
man-c committed Mar 30, 2021
1 parent 9293b3b commit 99468a6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pycoingecko/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ def __request(self, url):
# pass
raise

def __api_url_params(self, api_url, params):
def __api_url_params(self, api_url, params, api_url_has_params=False):
if params:
api_url += '&' if '?' in api_url else '?'
# if api_url contains already params and there is already a '?' avoid
# adding second '?' (api_url += '&' if '?' in api_url else '?'); causes
# issues with request parametes (usually for endpoints with required
# arguments passed as parameters)
api_url += '&' if api_url_has_params else '?'
for key, value in params.items():
api_url += "{0}={1}&".format(key, value)
api_url = api_url[:-1]
Expand Down Expand Up @@ -152,7 +156,7 @@ def get_coin_market_chart_by_id(self, id, vs_currency, days, **kwargs):
"""Get historical market data include price, market cap, and 24h volume (granularity auto)"""

api_url = '{0}coins/{1}/market_chart?vs_currency={2}&days={3}'.format(self.api_base_url, id, vs_currency, days)
api_url = self.__api_url_params(api_url, kwargs)
api_url = self.__api_url_params(api_url, kwargs, api_url_has_params=True)

return self.__request(api_url)

Expand All @@ -163,7 +167,7 @@ def get_coin_market_chart_range_by_id(self, id, vs_currency, from_timestamp, to_
api_url = '{0}coins/{1}/market_chart/range?vs_currency={2}&from={3}&to={4}'.format(self.api_base_url, id,
vs_currency, from_timestamp,
to_timestamp)
api_url = self.__api_url_params(api_url, kwargs)
api_url = self.__api_url_params(api_url, kwargs, api_url_has_params=True)

return self.__request(api_url)

Expand All @@ -181,7 +185,7 @@ def get_coin_ohlc_by_id(self, id, vs_currency, days, **kwargs):
"""Get coin's OHLC"""

api_url = '{0}coins/{1}/ohlc?vs_currency={2}&days={3}'.format(self.api_base_url, id, vs_currency, days)
api_url = self.__api_url_params(api_url, kwargs)
api_url = self.__api_url_params(api_url, kwargs, api_url_has_params=True)

return self.__request(api_url)

Expand All @@ -202,7 +206,7 @@ def get_coin_market_chart_from_contract_address_by_id(self, id, contract_address
api_url = '{0}coins/{1}/contract/{2}/market_chart/?vs_currency={3}&days={4}'.format(self.api_base_url, id,
contract_address,
vs_currency, days)
api_url = self.__api_url_params(api_url, kwargs)
api_url = self.__api_url_params(api_url, kwargs, api_url_has_params=True)

return self.__request(api_url)

Expand Down

0 comments on commit 99468a6

Please sign in to comment.