Stollen is an asynchronous framework designed to streamline the process of building API clients, written in Python 3.9+ using aiohttp and pydantic. With a declarative approach, Stollen allows developers to define API methods, handle responses, and manage errors in a structured way, focusing on clarity and scalability.
pip install -U stollenfrom __future__ importannotationsimportasyncioimportloggingfromstollenimportStollen, StollenMethod, StollenObjectfromstollen.enumsimportHTTPMethodfromstollen.exceptionsimportStollenAPIErrorclassCoingeckoAPIError(StollenAPIError):
passclassRateLimitError(CoingeckoAPIError):
passclassCoingecko(Stollen):
def__init__(self) ->None:
super().__init__(
base_url="https://api.coingecko.com/api/v3",
error_message_key=["status", "error_message"],
general_error_class=CoingeckoAPIError,
error_codes={429: RateLimitError},
stringify_detailed_errors=False,
)
asyncdefping(self) ->GeckoSays:
call: Ping=Ping()
returnawaitself(call)
classGeckoSays(StollenObject[Coingecko]):
gecko_says: strclassPing(
StollenMethod[GeckoSays, Coingecko],
http_method=HTTPMethod.GET,
api_method="/ping",
returning=GeckoSays,
):
passasyncdefmain() ->None:
logging.basicConfig(level=logging.DEBUG)
asyncwithCoingecko() ascoingecko:
gecko_says: GeckoSays=awaitcoingecko.ping()
logging.info(gecko_says)
if__name__=="__main__":
asyncio.run(main())