Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Propose a minimal specialization for extract #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions zyte_api/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import asyncio
import time
from base64 import b64decode
from collections.abc import Mapping
from functools import partial
from typing import Optional, Iterator, List
from typing import Awaitable, Iterator, List, Optional

import aiohttp
from aiohttp import TCPConnector
Expand All @@ -16,7 +18,7 @@
from ..apikey import get_apikey
from ..constants import API_URL, API_TIMEOUT
from ..stats import AggStats, ResponseStats
from ..utils import user_agent
from ..utils import _to_lower_camel_case, user_agent


# 120 seconds is probably too long, but we are concerned about the case with
Expand All @@ -43,6 +45,38 @@ def _post_func(session):
return session.post


class ExtractResult(Mapping):
BurnzZ marked this conversation as resolved.
Show resolved Hide resolved
"""Result of a call to AsyncClient.extract.

It can be used as a dictionary to access the raw API response.

It also provides some helper properties for easier access to some of its
underlying data.
"""

def __init__(self, api_response: dict):
self._api_response = api_response

def __getitem__(self, key):
return self._api_response[key]

def __iter__(self):
yield from self._api_response

def __len__(self):
return len(self._api_response)

@property
def http_response_body(self): -> bytes:
if hasattr(self, "_http_response_body"):
return self._http_response_body
base64_body = self._api_response.get("httpResponseBody", None)
if base64_body is None:
raise ValueError("API response has no httpResponseBody key.")
self._http_response_body = b64decode(base64_body)
return self._http_response_body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could later add http_response_text, which would return str, handling decoding.



class AsyncClient:
def __init__(self, *,
api_key=None,
Expand Down Expand Up @@ -148,3 +182,44 @@ async def _request(query):
session=session)

return asyncio.as_completed([_request(query) for query in queries])

@staticmethod
def _build_extract_query(raw_query):
return {
_to_lower_camel_case(k): v
for k, v in raw_query.items()
}

async def extract(
self,
url: str,
*,
session: Optional[aiohttp.ClientSession] = None,
handle_retries: bool = True,
retrying: Optional[AsyncRetrying] = None,
**kwargs,
) -> Awaitable[ExtractResult]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current features, compared to request_raw:

  • Pass top-level arguments instead of a dictionary.
  • May pass url as a positional argument.
  • Allows using Pythonic snake case for top-level query parameters.

Some potential features:

  • Allow a headers parameter, which automatically fills the corresponding API parameters.
  • Allow an outputs parameter, supporting a flag-like way of defining which outputs to enable.

The point of using **kwargs instead of mapping all parameters is forward-compatibility.

"""…"""
query = self._build_extract_query({**kwargs, 'url'=url})
response = await self.request_raw(
query=query,
endpoint='extract',
session=session,
handle_retries=handle_retries,
retrying=retrying,
)
return ExtractResult(response)

def extract_in_parallel(
self,
queries: List[dict],
*,
session: Optional[aiohttp.ClientSession] = None,
) -> Iterator[asyncio.Future]:
"""…"""
queries = [self._build_extract_query(query) for query in queries]
return self.request_parallel_as_completed(
queries,
endpoint='extract',
session=session,
)
8 changes: 7 additions & 1 deletion zyte_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# -*- coding: utf-8 -*-
from .__version__ import __version__


def _to_lower_camel_case(snake_case_string):
"""Convert from snake case (foo_bar) to lower-case-initial camel case
(fooBar)."""
prefix, *rest = snake_case_string.split('_')
return prefix + ''.join(part.title() for part in rest)


def user_agent(library):
return 'python-zyte-api/{} {}/{}'.format(
__version__,
Expand Down