Source code for zipwire._types
"""Reader protocols for sync and async IO."""
from __future__ import annotations
import typing
if typing.TYPE_CHECKING:
from collections.abc import AsyncIterator, Iterator
[docs]
@typing.runtime_checkable
class SyncReader(typing.Protocol):
"""Protocol for synchronous HTTP range-request readers."""
[docs]
def read_range(
self,
offset: int,
length: int,
) -> tuple[bytes, Headers]:
"""Read *length* bytes starting at absolute byte position *offset*.
Sends ``Range: bytes=<offset>-<offset+length-1>``.
Returns the response body and the HTTP response headers.
"""
...
[docs]
def head(self) -> Headers:
"""Send a HEAD request and return the response headers.
Raises:
RangeRequestUnsupported: If the server does not advertise
``Accept-Ranges: bytes``.
"""
...
[docs]
def stream_range(self, offset: int, length: int) -> Iterator[bytes]:
"""Stream *length* bytes starting at *offset* as chunks."""
...
[docs]
def close(self) -> None:
"""Release any held resources."""
...
[docs]
@typing.runtime_checkable
class AsyncReader(typing.Protocol):
"""Protocol for asynchronous HTTP range-request readers."""
[docs]
async def read_range(
self,
offset: int,
length: int,
) -> tuple[bytes, Headers]:
"""Read *length* bytes starting at absolute byte position *offset*.
Sends ``Range: bytes=<offset>-<offset+length-1>``.
Returns the response body and the HTTP response headers.
"""
...
[docs]
async def head(self) -> Headers:
"""Send a HEAD request and return the response headers.
Raises:
RangeRequestUnsupported: If the server does not advertise
``Accept-Ranges: bytes``.
"""
...
[docs]
def stream_range(self, offset: int, length: int) -> AsyncIterator[bytes]:
"""Stream *length* bytes starting at *offset* as chunks."""
...
[docs]
async def close(self) -> None:
"""Release any held resources."""
...
[docs]
@typing.runtime_checkable
class Writable(typing.Protocol):
"""Protocol for writable file-like objects (io.BytesIO, io.BufferedWriter, etc.)."""
def write(self, data: bytes, /) -> object: ...