Skip to content

Commit

Permalink
Function argurgments allow lists and booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
man-c committed Apr 22, 2021
1 parent b747cfd commit a4d7b7a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ cg = CoinGeckoAPI()
```

### Examples
The required parameters for each endpoint are defined as required (mandatory) parameters for the corresponding functions.
**Any optional parameters** can be also passed using same names, as defined in CoinGecko API doc (https://www.coingecko.com/api/docs/v3)
The required parameters for each endpoint are defined as required (mandatory) parameters for the corresponding functions.\
**Any optional parameters** can be passed using same names, as defined in CoinGecko API doc (https://www.coingecko.com/api/docs/v3)

*Lists are also supported as input for multiple-valued comma-separated parameters (e.g. see /simple/price usage examples).*
For any parameter:
- ***Lists** are supported as input for multiple-valued comma-separated parameters\
(e.g. see /simple/price usage examples).*
- ***Booleans** are supported as input for boolean type parameters; they can be `str` ('true', 'false'') or `bool` (`True`, `False`)\
(e.g. see /simple/price usage examples).*

Usage examples:
```python
Expand All @@ -47,6 +51,9 @@ Usage examples:
# optional parameters can be passed as defined in the API doc (https://www.coingecko.com/api/docs/v3)
>>> cg.get_price(ids='bitcoin', vs_currencies='usd', include_market_cap='true', include_24hr_vol='true', include_24hr_change='true', include_last_updated_at='true')
{'bitcoin': {'usd': 3458.74, 'usd_market_cap': 60574330199.29028, 'usd_24h_vol': 4182664683.6247883, 'usd_24h_change': 1.2295378479069035, 'last_updated_at': 1549071865}}
# OR (also booleans can be used for boolean type arguments)
>>> cg.get_price(ids='bitcoin', vs_currencies='usd', include_market_cap=True, include_24hr_vol=True, include_24hr_change=True, include_last_updated_at=True)
{'bitcoin': {'usd': 3458.74, 'usd_market_cap': 60574330199.29028, 'usd_24h_vol': 4182664683.6247883, 'usd_24h_change': 1.2295378479069035, 'last_updated_at': 1549071865}}
```

### API documentation
Expand Down
62 changes: 41 additions & 21 deletions pycoingecko/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

from .utils import list_args_to_comma_separated
from .utils import func_args_preprocessing


class CoinGeckoAPI:
__API_URL_BASE = 'https://api.coingecko.com/api/v3/'
Expand Down Expand Up @@ -62,7 +63,7 @@ def ping(self):
return self.__request(api_url)

# ---------- SIMPLE ----------#
@list_args_to_comma_separated
@func_args_preprocessing
def get_price(self, ids, vs_currencies, **kwargs):
"""Get the current price of any cryptocurrencies in any other supported currencies that you need"""

Expand All @@ -76,7 +77,7 @@ def get_price(self, ids, vs_currencies, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_token_price(self, id, contract_addresses, vs_currencies, **kwargs):
"""Get the current price of any tokens on this coin (ETH only at this stage as per api docs) in any other supported currencies that you need"""

Expand All @@ -89,6 +90,7 @@ def get_token_price(self, id, contract_addresses, vs_currencies, **kwargs):
api_url = self.__api_url_params(api_url, kwargs)
return self.__request(api_url)

@func_args_preprocessing
def get_supported_vs_currencies(self, **kwargs):
"""Get list of supported_vs_currencies"""

Expand All @@ -98,7 +100,7 @@ def get_supported_vs_currencies(self, **kwargs):
return self.__request(api_url)

# ---------- COINS ----------#
@list_args_to_comma_separated
@func_args_preprocessing
def get_coins(self, **kwargs):
"""List all coins with data (name, price, market, developer, community, etc)"""

Expand All @@ -108,6 +110,7 @@ def get_coins(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_coins_list(self, **kwargs):
"""List all supported coins id, name and symbol (no pagination required)"""

Expand All @@ -116,7 +119,7 @@ def get_coins_list(self, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coins_markets(self, vs_currency, **kwargs):
"""List all supported coins price, market cap, volume, and market related data"""

Expand All @@ -127,7 +130,7 @@ def get_coins_markets(self, vs_currency, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_by_id(self, id, **kwargs):
"""Get current data (name, price, market, ... including exchange tickers) for a coin"""

Expand All @@ -136,7 +139,7 @@ def get_coin_by_id(self, id, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_ticker_by_id(self, id, **kwargs):
"""Get coin tickers (paginated to 100 items)"""

Expand All @@ -145,7 +148,7 @@ def get_coin_ticker_by_id(self, id, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_history_by_id(self, id, date, **kwargs):
"""Get historical data (name, price, market, stats) at a given date for a coin"""

Expand All @@ -156,7 +159,7 @@ def get_coin_history_by_id(self, id, date, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
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)"""

Expand All @@ -165,7 +168,7 @@ def get_coin_market_chart_by_id(self, id, vs_currency, days, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_market_chart_range_by_id(self, id, vs_currency, from_timestamp, to_timestamp, **kwargs):
"""Get historical market data include price, market cap, and 24h volume within a range of timestamp (granularity auto)"""

Expand All @@ -176,7 +179,7 @@ def get_coin_market_chart_range_by_id(self, id, vs_currency, from_timestamp, to_

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_status_updates_by_id(self, id, **kwargs):
"""Get status updates for a given coin"""

Expand All @@ -185,7 +188,7 @@ def get_coin_status_updates_by_id(self, id, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_ohlc_by_id(self, id, vs_currency, days, **kwargs):
"""Get coin's OHLC"""

Expand All @@ -195,7 +198,7 @@ def get_coin_ohlc_by_id(self, id, vs_currency, days, **kwargs):
return self.__request(api_url)

# ---------- Contract ----------#
@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_info_from_contract_address_by_id(self, id, contract_address, **kwargs):
"""Get coin info from contract address"""

Expand All @@ -204,7 +207,7 @@ def get_coin_info_from_contract_address_by_id(self, id, contract_address, **kwar

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_market_chart_from_contract_address_by_id(self, id, contract_address, vs_currency, days, **kwargs):
"""Get historical market data include price, market cap, and 24h volume (granularity auto) from a contract address"""

Expand All @@ -215,7 +218,7 @@ def get_coin_market_chart_from_contract_address_by_id(self, id, contract_address

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_coin_market_chart_range_from_contract_address_by_id(self, id, contract_address, vs_currency, from_timestamp,
to_timestamp, **kwargs):
"""Get historical market data include price, market cap, and 24h volume within a range of timestamp (granularity auto) from a contract address"""
Expand All @@ -227,6 +230,7 @@ def get_coin_market_chart_range_from_contract_address_by_id(self, id, contract_a
return self.__request(api_url)

# ---------- EXCHANGES ----------#
@func_args_preprocessing
def get_exchanges_list(self, **kwargs):
"""List all exchanges"""

Expand All @@ -235,6 +239,7 @@ def get_exchanges_list(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_exchanges_id_name_list(self, **kwargs):
"""List all supported markets id and name (no pagination required)"""

Expand All @@ -243,7 +248,7 @@ def get_exchanges_id_name_list(self, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_exchanges_by_id(self, id, **kwargs):
"""Get exchange volume in BTC and tickers"""

Expand All @@ -252,7 +257,7 @@ def get_exchanges_by_id(self, id, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_exchanges_tickers_by_id(self, id, **kwargs):
"""Get exchange tickers (paginated, 100 tickers per page)"""

Expand All @@ -261,7 +266,7 @@ def get_exchanges_tickers_by_id(self, id, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_exchanges_status_updates_by_id(self, id, **kwargs):
"""Get status updates for a given exchange"""

Expand All @@ -270,7 +275,7 @@ def get_exchanges_status_updates_by_id(self, id, **kwargs):

return self.__request(api_url)

@list_args_to_comma_separated
@func_args_preprocessing
def get_exchanges_volume_chart_by_id(self, id, days, **kwargs):
"""Get volume chart data for a given exchange"""

Expand All @@ -282,6 +287,7 @@ def get_exchanges_volume_chart_by_id(self, id, days, **kwargs):
return self.__request(api_url)

# ---------- FINANCE ----------#
@func_args_preprocessing
def get_finance_platforms(self, **kwargs):
"""Get cryptocurrency finance platforms data"""

Expand All @@ -290,6 +296,7 @@ def get_finance_platforms(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_finance_products(self, **kwargs):
"""Get cryptocurrency finance products data"""

Expand All @@ -299,6 +306,7 @@ def get_finance_products(self, **kwargs):
return self.__request(api_url)

# ---------- INDEXES ----------#
@func_args_preprocessing
def get_indexes(self, **kwargs):
"""List all market indexes"""

Expand All @@ -307,6 +315,7 @@ def get_indexes(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_indexes_by_id(self, id, **kwargs):
"""Get market index by id"""

Expand All @@ -315,6 +324,7 @@ def get_indexes_by_id(self, id, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_indexes_list(self, **kwargs):
"""List market indexes id and name"""

Expand All @@ -324,6 +334,7 @@ def get_indexes_list(self, **kwargs):
return self.__request(api_url)

# ---------- DERIVATIVES ----------#
@func_args_preprocessing
def get_derivatives(self, **kwargs):
"""List all derivative tickers"""

Expand All @@ -332,6 +343,7 @@ def get_derivatives(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_derivatives_exchanges(self, **kwargs):
"""List all derivative tickers"""

Expand All @@ -340,6 +352,7 @@ def get_derivatives_exchanges(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_derivatives_exchanges_by_id(self, id, **kwargs):
"""List all derivative tickers"""

Expand All @@ -348,6 +361,7 @@ def get_derivatives_exchanges_by_id(self, id, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_derivatives_exchanges_list(self, **kwargs):
"""List all derivative tickers"""

Expand All @@ -357,7 +371,7 @@ def get_derivatives_exchanges_list(self, **kwargs):
return self.__request(api_url)

# ---------- STATUS UPDATES ----------#
@list_args_to_comma_separated
@func_args_preprocessing
def get_status_updates(self, **kwargs):
"""List all status_updates with data (description, category, created_at, user, user_title and pin)"""

Expand All @@ -367,7 +381,7 @@ def get_status_updates(self, **kwargs):
return self.__request(api_url)

# ---------- EVENTS ----------#
@list_args_to_comma_separated
@func_args_preprocessing
def get_events(self, **kwargs):
"""Get events, paginated by 100"""

Expand All @@ -376,6 +390,7 @@ def get_events(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_events_countries(self, **kwargs):
"""Get list of event countries"""

Expand All @@ -384,6 +399,7 @@ def get_events_countries(self, **kwargs):

return self.__request(api_url)

@func_args_preprocessing
def get_events_types(self, **kwargs):
"""Get list of event types"""

Expand All @@ -393,6 +409,7 @@ def get_events_types(self, **kwargs):
return self.__request(api_url)

# ---------- EXCHANGE-RATES ----------#
@func_args_preprocessing
def get_exchange_rates(self, **kwargs):
"""Get BTC-to-Currency exchange rates"""

Expand All @@ -402,6 +419,7 @@ def get_exchange_rates(self, **kwargs):
return self.__request(api_url)

# ---------- TRENDING ----------#
@func_args_preprocessing
def get_search_trending(self, **kwargs):
"""Get top 7 trending coin searches"""

Expand All @@ -411,6 +429,7 @@ def get_search_trending(self, **kwargs):
return self.__request(api_url)

# ---------- GLOBAL ----------#
@func_args_preprocessing
def get_global(self, **kwargs):
"""Get cryptocurrency global data"""

Expand All @@ -419,6 +438,7 @@ def get_global(self, **kwargs):

return self.__request(api_url)['data']

@func_args_preprocessing
def get_global_decentralized_finance_defi(self, **kwargs):
"""Get cryptocurrency global decentralized finance(defi) data"""

Expand Down
Loading

0 comments on commit a4d7b7a

Please sign in to comment.