Skip to content

feat(async): add asynchronous CoinGeckoAPI implementation #92

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

arielen
Copy link
Contributor

@arielen arielen commented Feb 26, 2025

Changelog:

  • Updated func_args_preprocessing decorator in utils.py to support async methods.
  • Implemented AsyncCoinGeckoAPI class with asynchronous functionality mirroring CoinGeckoAPI.
  • Added mock tests for all AsyncCoinGeckoAPI methods.
  • Imported AsyncCoinGeckoAPI in __init__.py.
  • Added dependencies: aiohttp, aiohttp_retry, and orjson in setup.py.
  • Replaced json with orjson in api.py for better performance in CoinGeckoAPI.
  • Updated README.md to include instructions for installing test dependencies.

BREAKING CHANGE:

  • Tests now require pytest-asyncio and aioresponses.
  • AsyncCoinGeckoAPI depends on orjson and aiohttp; orjson is significantly faster than the standard json module and should be considered for replacing it throughout CoinGeckoAPI.

- Updated func_args_preprocessing decorator in utils.py to support async methods.
- Implemented AsyncCoinGeckoAPI class with asynchronous functionality mirroring CoinGeckoAPI.
- Added mock tests for all AsyncCoinGeckoAPI methods.
- Imported AsyncCoinGeckoAPI in __init__.py.
@arielen
Copy link
Contributor Author

arielen commented Feb 26, 2025

Usage Example

import asyncio
from pycoingecko import AsyncCoinGeckoAPI

async def main():
    # Using the AsyncCoinGeckoAPI class via an async context manager
    # You can also initialize the key here
    async with AsyncCoinGeckoAPI() as api:
        price_data = await api.get_price(ids="bitcoin", vs_currencies="usd")
        print("Price data:", price_data)
        
        coins_category = await api.get_coins_categories()
        print("Coins categories:", coins_category)

    # Alternatively, using the create() method:
    obj_api = await AsyncCoinGeckoAPI.create()
    price_data = await obj_api.get_price(ids="bitcoin", vs_currencies="usd")
    print("Price data using create():", price_data)
    await obj_api.close()

if __name__ == "__main__":
    asyncio.run(main())

Explanation

  • Async Context Manager:
    Using async with AsyncCoinGeckoAPI() as api: automatically initializes and later closes the session, ensuring proper cleanup without manual intervention.

  • create() Method:
    You can also instantiate the API using the asynchronous factory method create(), which lets you manage the session manually (remember to call close() when done).

  • Performance Benefits:
    The async implementation is significantly more efficient than the synchronous version. It allows you to perform multiple API calls concurrently and benefits from the speed of orjson for JSON parsing.

@man-c, always happy to interact and thanks for the review and further merge !

- Added dependencies: `aiohttp`, `aiohttp_retry`, and `orjson` in setup.py.
- Replaced `json` with `orjson` in api.py for better performance in CoinGeckoAPI.
- Updated README.md to include instructions for installing test dependencies.
@arielen arielen changed the base branch from master to develop February 26, 2025 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant