---
sidebar:
  label: roboto.storage.http_range_reader
  order: 5
title: roboto.storage.http_range_reader
---
## Module Contents

### HttpRangeReader

```python
class roboto.storage.http_range_reader.HttpRangeReader(
    url: str,
    read_ahead_size: int = _READ_AHEAD_SIZE,
)
```

`from roboto.storage import HttpRangeReader`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L117-L480)

A seekable, read-only file-like object over a file at an HTTP URL, which fetches only the bytes it reads.

It implements the methods of `typing.IO[bytes]` that `mcap.reader` calls, such as `read`, `seek` and `tell`; `as_io_bytes` types it as `typing.IO[bytes]`.

Constructing the reader requests the file's first 8 bytes and its last 128 KiB at the same time, and takes the file's size from the first response. A file of at most 2 MiB is then fetched whole.

Reads come from an in-memory cache that keeps every byte fetched, so reading the same bytes again makes no request. A read the cache cannot serve fetches `read_ahead_size` bytes from its position, or the whole of the file's last 128 KiB when it starts there. A fetch leaves out cached bytes at either end, and extends across a gap of at most 64 KiB to a neighboring cached range. `prefetch_range` fetches a known range ahead of reads, over parallel connections.

Requests share a pool of at most 32 connections to the URL's host, and each request is retried up to 3 times on a network error.

**Parameters**

- **url** (`str`): URL of the file, such as a signed download URL. The server must answer HTTP `Range` requests.
- **read_ahead_size** (`int`): Number of bytes a read fetches from its position when the cache lacks them, unless the read asks for more. Defaults to 8 MiB.

**Raises**

- `OSError`: The server answers either opening request with a status other than 200 or 206, sends no `Content-Range` header with the file's size, or, for a file of at most 2 MiB, returns a last-128-KiB response that stops short of the end of the file. Later reads and prefetches raise `OSError` when a response has an HTTP error status or holds bytes other than the ones requested.
- `urllib3.exceptions.MaxRetryError`: An opening request still fails with a network error after 3 retries. Later reads and prefetches raise it the same way.

**Properties**

- **HttpRangeReader.size** (`int`): Size of the file, in bytes.

#### HttpRangeReader.close()

```python
def close() -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L198-L201)

Release the reader's cached bytes and its HTTP connections.

**Returns**

- `None`

#### HttpRangeReader.prefetch_range()

```python
def prefetch_range(start: int, end: int) -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L203-L249)

Download the bytes from `start` through `end` into the cache, over parallel requests.

Bytes the cache already holds, such as the ones fetched when the reader opened, are not fetched again. The rest are fetched in parallel requests of at most 1/N of the whole range each, where N is the number of whole MiB in the range, from 1 to 32. On Amazon ECS or AWS Lambda, N counts whole 100 MiB instead, since more connections rarely make the transfer faster there.

**Parameters**

- **start** (`int`): Offset of the first byte to fetch.
- **end** (`int`): Offset of the last byte to fetch. The range includes it.

**Raises**

- `OSError`: A response has an HTTP error status or holds bytes other than the ones requested.
- `urllib3.exceptions.MaxRetryError`: A request still fails with a network error after 3 retries.

**Returns**

- `None`

#### HttpRangeReader.read()

```python
def read(size: int = -1) -> bytes
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L251-L281)

**Parameters**

- **size** (`int`)

**Returns**

- `bytes`

#### HttpRangeReader.readable()

```python
def readable() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L283-L284)

**Returns**

- `bool`

#### HttpRangeReader.seek()

```python
def seek(offset: int, whence: int = 0) -> int
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L286-L293)

**Parameters**

- **offset** (`int`)
- **whence** (`int`)

**Returns**

- `int`

#### HttpRangeReader.seekable()

```python
def seekable() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L295-L296)

**Returns**

- `bool`

#### HttpRangeReader.tell()

```python
def tell() -> int
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L298-L299)

**Returns**

- `int`

#### HttpRangeReader.writable()

```python
def writable() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L301-L302)

**Returns**

- `bool`

### as_io_bytes()

```python
def roboto.storage.http_range_reader.as_io_bytes(reader: HttpRangeReader) -> IO[bytes]
```

`from roboto.storage import as_io_bytes`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L483-L493)

Return `reader` unchanged, typed as `typing.IO[bytes]` for type checkers.

`HttpRangeReader` implements only the methods of `typing.IO[bytes]` that `mcap.reader` calls, such as `read`, `seek` and `tell`, so pass the result only to code that calls no others.

**Parameters**

- **reader** (`HttpRangeReader`): Reader to pass where a `typing.IO[bytes]` is expected.

**Returns**

- `IO[bytes]`

### logger

```python
roboto.storage.http_range_reader.logger
```

`from roboto.storage.http_range_reader import logger`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/http_range_reader.py#L20-L20)
