DEV Community

Aahnik Daw
Aahnik Daw

Posted on

7 2

I want to build a Python wrapper for an API, how should I approach it?

I am planning to build a python wrapper around a rest API.

  • I want to support friendly functions, with autocompletion in the editor.
  • I want 100% test coverage
  • I want the package to be modular, so that, any changes to API, can be easily reflected
  • I want to support both async and normal methods (the core will be implemented thru async, but there will be a wrapper for those who don't want to use async)

example:

import asyncio
from library import Api

api = Api()

async def do_job():
    thing = await api.get_thing()
    print(thing)

asyncio.run(do_job())
Enter fullscreen mode Exit fullscreen mode

or

from library.sync import Api

thing = api.get_thing()

print(thing)
Enter fullscreen mode Exit fullscreen mode

the data that will be passed to the functions should be validated. planning to use pydantic.

planning to create a sub-package library.models to contain all the modules defining the models.

how should I design the entire thing? how should I structure the project?

best practices? guides?

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (3)

Collapse
 
pretzellogix profile image
PretzelLogix

Here is a 15-part guide that I wrote that addresses 3 of your 4 requirements: pretzellogix.net/2021/12/08/how-to...

Eventually, I'll get around to writing a few extra chapters on how to make async methods. In the meantime, I think this will get you started!

Collapse
 
aahnik profile image
Aahnik Daw

thank you so much... its soooooo helpful

Collapse
 
nicolasrondon profile image
Nicolás Rondón

You could guide yourself by the code of FastAPI github.com/tiangolo/fastapi They are using wrapers so maybe could be useful

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!