I'm building a wrapper for an API. For now, it works. However, it is synchronous, using the requests module for HTTP calls.
I would like to implement a way to asynchronously call these methods while not having to use a different name or lib version. I immediately thought about overloading, but seeing as overloading in python is a bit different than other languages, it doesn't really look possible.
Essentially, I want to build a class that would look like this (idea-wise, I know that it doesn't work in Python):
class Foo:
def foo(self):
# Requests code...
print("foo sync")
async def foo(self):
# aiohttp code...
print("foo async")
And use it in this way:
f = Foo()
f.foo()
await f.foo()
Output:
>> "foo sync"
>> "foo async"
Essentially, in this code, the async function would just completely override the previous one, which isn't really helpful.
From some googling, it doesn't exactly look possible, however, Python always manages to surprise me.
Thanks in advance :D