Package: apiwrappers
apiwrappersis a library for building API clients
that work both with regular and async code.FeaturesDRY- support both regular and async code with one implementationFlexible- middleware mechanism to customize request/responseTyped- library is fully typed and it’s relatively easy
to get fully typed wrappersModern- decode JSON with no effort using dataclasses and type annotationsUnified interface- work with different python HTTP client libraries
in the same way. Currently supported:requestsaiohttpInstallationpipinstall'apiwrappers[aiohttp,requests]'Note: extras are mainly needed for the final user of your API clientQuickStartMaking request is rather straightforward:fromdataclassesimportdataclassfromtypingimportListfromapiwrappersimportRequest,fetch,make_driver@dataclassclassRepo:name:strurl="https://api.github.com/users/unmade/repos"request=Request("GET",url)driver=make_driver("requests")fetch(driver,request)# Response(..., status_code=200, ...)fetch(driver,request,model=List[Repo])# [Repo(name='am-date-picker'), ...]driver=make_driver("aiohttp")awaitfetch(driver,request)# Response(..., status_code=200, ...)awaitfetch(driver,request,model=List[Repo])# [Repo(name='am-date-picker'), ...]Writing a Simple API ClientWithapiwrappersyou can bootstrap clients for different API
pretty fast and easily.Here is how a typical API client would look like:from__future__importannotationsfromdataclassesimportdataclassfromtypingimportAwaitable,Generic,List,TypeVar,overloadfromapiwrappersimportAsyncDriver,Driver,Request,Url,fetchT=TypeVar("T",Driver,AsyncDriver)@dataclassclassRepo:id:intname:strclassGitHub(Generic[T]):def__init__(self,host:str,driver:T):self.url=Url(host)self.driver:T=driver@overloaddefget_repos(self:Github[Driver],username:str)->List[Repo]:...@overloaddefget_repos(self:Github[AsyncDriver],username:str)->Awaitable[List[Repo]]:...defget_repos(self,username:str):url=self.url("/users/{username}/repos",username=username)request=Request("GET",url)returnfetch(self.driver,request,model=List[Repo])This is small, but fully typed, API client for one of theapi.github.comendpoints to get all user repos
by username:Here we definedRepodataclass that describes what we want
to get from response and pass it to thefetchfunction.fetchwill then make a request and will cast response to that type.Note how we create URL:url=self.url("/users/{username}/repos",username=username)Sometimes, it’s useful to have an URL template, for example, for logging
or for aggregating metrics, so instead of formatting immediately, we
provide a template and replacement fields.Using the API ClientHere how we can use it:>>>fromapiwrappersimportmake_driver>>>driver=make_driver("requests")>>>github=GitHub("https://api.github.com",driver=driver)>>>github.get_repos("unmade")[Repo(id=47463599,name='am-date-picker'),Repo(id=231653904,name='apiwrappers'),Repo(id=144204778,name='conway'),...]To use it with asyncio all we need to do is provide a proper driver
and don’t forget toawaitmethod call:Use IPython or Python 3.8+ with python -m asyncio
to try this code interactively>>>fromapiwrappersimportmake_driver>>>driver=make_driver("aiohttp")>>>github=GitHub("https://api.github.com",driver=driver)>>>awaitgithub.get_repos("unmade")[Repo(id=47463599,name='am-date-picker'),Repo(id=231653904,name='apiwrappers'),Repo(id=144204778,name='conway'),...]DocumentationDocumentation forapiwrapperscan be found atRead The Docs.Check outExtended Client Example.ContributingContributions are welcome, and they are greatly appreciated! Every
little bit helps, and credit will always be given.Seecontributing guideto learn more.Currently the code and the issues are hosted on GitHub.The project is licensed under MIT.