Skip to content

RistoAle97/pokeapi-python-wrapper

Repository files navigation

PyPokéClient

Synchronous and asynchronous clients to fetch data from PokéAPI.

Python Pydantic

PyPI Python versions


📌 Features

  • Coverage: all PokéAPI endpoints are covered.
  • Data validation: uses Pydantic dataclasses for the API implementation.
  • Flexibility: can choose between synchronous and asynchronous clients.
  • Caching: can employ a local cache system for faster responses and to respect PokéAPI Fair Use policy.

Please have a look at the documentation for more details about the package.


📦 Installation

# It is highly recommended to use uv
uv pip install pypokeclient

# But you can also install the package via pip
pip install pypokeclient

🛠️ How to use

You can choose whether to use the synchronous client

from pypokeclient import Client

# Simple usage
client = Client()
pokemon = client.get_pokemon("fuecoco")

# Or with context manager
with Client() as client:
  pokemon = client.get_pokemon("fuecoco")

or the asynchronous one

import asyncio

from pypokeclient import AsyncClient


async def fetch_data():
  # Simple usage
  client = AsyncClient()
  pokemon = await client.get_pokemon("fuecoco")

  # With context manager
  async with AsyncClient() as client:
    pokemon = await client.get_pokemon("fuecoco")

asyncio.run(fetch_data())

💾 Caching the results

Important

Please refer to the hishel documentation for more details about the caching system.

import logging

from hishel import SyncSqliteStorage
from hishel.httpx import SyncCacheClient
from pypokeclient import Client

# Set up the logger
logger = logging.getLogger("pypokeclient")
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter("%(name)s - %(levelname)s - %(message)s"))
logger.addHandler(console_handler)

# Set up the underlying HTTP client
http_client = SyncCacheClient(
    storage=SyncSqliteStorage(database_path="pypokeclient_cache.db")
)

# Fetch data
with Client(http_client) as sync_client:
    pokemon = sync_client.get_pokemon("fuecoco")
    pokemon = sync_client.get_pokemon("fuecoco")

    # The sprites are cached too
    sprite = sync_client.get_sprite(pokemon.sprites.front_default)
    sprite = sync_client.get_sprite(pokemon.sprites.front_default)

    # You can also save the sprites locally if needed
    sprite.save("fuecoco.png")

The output will be the following

pypokeclient - INFO - The synchronous client is ready and using the cache at .cache\hishel\pypokeclient_cache.db'.
pypokeclient - INFO - [200] Request to https://pokeapi.co/api/v2/pokemon/fuecoco.
pypokeclient - INFO - [200] Cached request to https://pokeapi.co/api/v2/pokemon/fuecoco.
pypokeclient - INFO - [200] Request to https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/909.png.
pypokeclient - INFO - [200] Cached request to https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/909.png.
pypokeclient - INFO - Closed session for the synchronous client.

📝 License

This project is MIT licensed.

About

Synchronous and asynchronous clients to interact with pokeapi.co

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages