Writing Python API wrappers using aiohttp

820 views
Skip to first unread message

Usman Ehtesham

unread,
Jan 11, 2017, 1:55:57 PM1/11/17
to aio-libs
Hello,

I want to learn aiohttp (and asyncio) and i am trying to look for examples of Python API wrappers written for Restful APIs using aiohttp. Usually, such API wrappers are written using the requests library. I was thinking of writing an API wrapper using aiohttp and if I can find some good projects to refer to, that would really help. I found a couple of projects online but they were not very useful.

Would really appreciate any help from this group. 

Usman

Nickolai Novik

unread,
Jan 11, 2017, 2:29:37 PM1/11/17
to Usman Ehtesham, aio-libs

Hi,
There are bunch of asyncio REST API wrappers in the wild most of them very similar to requests versions, you just put async/await in proper places and use aiohttp client instead requests  one.

Here are few projects you may check

https://github.com/paultag/aiodocker  -- wrapper around docker HTTP API
https://github.com/aio-libs/aioes -- more complicated wrapper around Elasticsearch HTTP API



--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+u...@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/aio-libs.
To view this discussion on the web visit https://groups.google.com/d/msgid/aio-libs/658c6434-1cf9-46f0-8f67-b1e976fcb874%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Usman Ehtesham

unread,
Jan 11, 2017, 2:38:37 PM1/11/17
to aio-libs, uehte...@gmail.com
Hello Nickolai,

Thank you for your reply and the examples. From what I see, if I write a Python API wrapper using aiohttp, then users of the API wrapper will have to have knowledge of aiohttp right? As in they will have to something similar like in the example in the README here: https://github.com/aio-libs/aioes/blob/master/README.rst

Is there a way where we can encapsulate late this such that user can use the library like a synchronous library and internally its doing stuff asynchronously? One example that I found online was this:

https://github.com/ramalho/platespinning/blob/master/countries/flags2_asyncio.py#L109 (look at function download_many)

Thank you again :)

Usman

Nickolai Novik

unread,
Jan 11, 2017, 2:57:03 PM1/11/17
to Usman Ehtesham, aio-libs
Having both async and sync API is tricky business, personally I do not like that approach with *loop.run_until_complete()* and would just create 2 separate wrapper with common helpers. There is also other way, checkout https://github.com/cablehead/python-consul project, it have requests/tornado/asyncio API support:

aiohttp version:
c = consul.aio.Consul()     
response = yield from c.kv.put(b'foo', b'bar')

vs requests version:
c = consul.Consul()
response = c.kv.put('foo', 'bar')

User just creates client type he wants, and use common handlers.



Hussain

unread,
Jan 14, 2017, 7:21:13 AM1/14/17
to aio-...@googlegroups.com
Hi,

@Usman:

It is not worth it to make it both sync and async . Most external
software already has regular wrappers (literally most common OS software
has a Python wrapper).

Building up a suite of reliable async-wrappers for asyncio/Python will
actually make Python-async more approachable for situations where async
makes a lot of sense (lots of common web functionality, especially
things like DB-calls, make more sense under async).

I do like the format of calling the libs: aio{name} , as that makes it
easy to find/discover on github/pypi .

If you need some advice for which type of wrappers you might be
interested in writing, go see which popular sync wrappers exist on
github/pypi and if they don't have an async wrapper (but an async
wrapper makes *sense*) then it could be worth writing one for that
specific software.

Regards,

Reply all
Reply to author
Forward
0 new messages