zytedata/python-zyte-api

Simplify extract syntax

Gallaecio opened this issue · 1 comments

What about simplifying

import asyncio
from base64 import b64decode

from zyte_api.aio.client import AsyncClient

async def main():
    client = AsyncClient()
    api_response = await client.request_raw(
        {
            'url': 'https://toscrape.com',
            'httpResponseBody': True,
        }
    )
    http_response_body: bytes = b64decode(
        api_response['httpResponseBody']
    )

asyncio.run(main())

as

import asyncio

from zyte_api.aio.client import AsyncClient

async def main():
    client = AsyncClient()
    api_response = await client.extract(
        url='https://toscrape.com',
        http_response_body=True,
    )
    http_response_body: bytes = api_response.http_response_body

asyncio.run(main())

+1 on this.

I like this idea as this interface gives us type annotations to statically validate the parameters for the call. It also removes the effort of decoding the base64 body.