Module scrapfly.transport
API calls written once, run by ScrapflyClient (requests) and AsyncScrapflyClient (httpx).
An operation is a generator: it yields a Call or a Sleep and is sent back the requests.Response (None after a Sleep), so building the request and reading the response are shared, and only the clients' run loops do I/O.
Functions
def ready(value: ~T) ‑> Steps[~T]-
Expand source code
def ready(value: T) -> Steps[T]: """An operation that needs no call.""" return value yieldAn operation that needs no call.
def retry_delay(attempt: int) ‑> float-
Expand source code
def retry_delay(attempt: int) -> float: """Exponential backoff with full jitter, attempt 0 first.""" return random.uniform(0, 2 ** attempt)Exponential backoff with full jitter, attempt 0 first.
def signed(method: Callable, steps: Callable, stream: type) ‑> Callable-
Expand source code
def signed(method: Callable, steps: Callable, stream: type) -> Callable: """Give the client method that runs `steps` its signature: Steps[R] returns R, a Streamed[T] in R reads as stream[T].""" signature = inspect.signature(steps) if get_args(signature.return_annotation): signature = signature.replace(return_annotation=_streams(get_args(signature.return_annotation)[0], stream)) method.__annotations__ = {**steps.__annotations__, 'return': signature.return_annotation} method.__signature__ = signature return methodGive the client method that runs
stepsits signature: Steps[R] returns R, a Streamed[T] in R reads as stream[T]. def without_key(error: BaseException) ‑> BaseException-
Expand source code
def without_key(error: BaseException) -> BaseException: """The error with the API key masked, or the error itself when it does not print one. requests puts the request URL in a connection error message, query and key included. """ message = str(error) if not KEY_IN_URL.search(message): return error masked = KEY_IN_URL.sub('REDACTED', message) if isinstance(error, RequestException): return type(error)(masked, request=error.request, response=error.response) return type(error)(masked)The error with the API key masked, or the error itself when it does not print one.
requests puts the request URL in a connection error message, query and key included.
Classes
class Call (method: str,
url: str,
*,
tries: int = 1,
retry_read_timeout: bool = True,
**options: Any)-
Expand source code
class Call: """One HTTP exchange, as the keyword arguments of requests.request.""" def __init__(self, method: str, url: str, *, tries: int = 1, retry_read_timeout: bool = True, **options: Any): self.method = method self.url = url # A retry on a network error sends the request again: only for calls safe to repeat. self.tries = tries # False: a read timeout is raised at once, the other network errors are retried. self.retry_read_timeout = retry_read_timeout self.options = options @property def stream(self) -> bool: return bool(self.options.get('stream'))One HTTP exchange, as the keyword arguments of requests.request.
Instance variables
prop stream : bool-
Expand source code
@property def stream(self) -> bool: return bool(self.options.get('stream'))
class Sleep (seconds: float)-
Expand source code
@dataclass class Sleep: seconds: floatSleep(seconds: float)
Instance variables
var seconds : float
class StreamDecoder (*args, **kwargs)-
Expand source code
class StreamDecoder(Protocol[T]): """Push decoder of a streamed body: done once it read what it needed.""" done: bool def feed(self, chunk: bytes) -> Iterable[T]: ... def close(self) -> Iterable[T]: ...Push decoder of a streamed body: done once it read what it needed.
Ancestors
- typing.Protocol
- typing.Generic
Class variables
var done : bool
Methods
def close(self) ‑> Iterable[~T]-
Expand source code
def close(self) -> Iterable[T]: ... def feed(self, chunk: bytes) ‑> Iterable[~T]-
Expand source code
def feed(self, chunk: bytes) -> Iterable[T]: ...
class Streamed (response: requests.models.Response,
decoder: StreamDecoder,
chunk_size: int | None = None,
finish: Callable[[Any], Steps[~T]] | None = None)-
Expand source code
@dataclass class Streamed(Generic[T]): """A response read as it arrives, each decoded item finished by an operation of its own.""" response: Response decoder: StreamDecoder chunk_size: Optional[int] = None finish: Optional[Callable[[Any], Steps[T]]] = NoneA response read as it arrives, each decoded item finished by an operation of its own.
Ancestors
- typing.Generic
Instance variables
var chunk_size : int | Nonevar decoder : StreamDecodervar finish : Callable[[Any], Steps[~T]] | Nonevar response : requests.models.Response
class operation (steps: Callable[Concatenate[Any, ~P], Steps[~R]])-
Expand source code
def __get__(self, client: Any, owner: type) -> Any: return selfA method written once as Steps: a plain method of ScrapflyClient, a coroutine of AsyncScrapflyClient.
ScrapflyApi.init_subclass replaces it by the client's method; get only types that method.
Ancestors
- typing.Generic
class streaming (steps: Callable[Concatenate[Any, ~P], Steps[Streamed[~T]]])-
Expand source code
def __get__(self, client: Any, owner: type) -> Any: return selfAn operation whose result arrives as a stream: an iterator of ScrapflyClient, an async iterator of AsyncScrapflyClient.
Ancestors
- typing.Generic