reductstore/reduct-py

Reduce duplications in code

Closed this issue · 2 comments

Each API method in the client repeats the same code:

async with aiohttp.ClientSession() as session:
    async with session.get(f"{self.url}/list") as response:
        if response.ok:
            buckets = BucketList.parse_raw(await response.text())
            return buckets
        raise ReductError(response.status, await response.read())

It can be wrapped into a helper method.

i started doing this, but i'm not sure it's such a good idea. did you mean on every type of HTTP request of just for the get methods?

For all the HTTP methods, which we use. I have in mind something like this:

async def request(method: str, url: str, **kwargs) -> bytes:
  async with aiohttp.ClientSession() as session:
    async with session.request(method, url, **kwargs) as response:
        if response.ok:
            return await response.read()
        raise ReductError(response.status, await response.text()) 


class Client:
  async def list() -> BucketList:
    return BucketList.parse_raw(await request("GET", f"{self.url}/list"))

If you have only one method for all the HTTP requests, it is easier to add the token authentication.