---
sidebar:
  label: roboto.http.response
  order: 7
title: roboto.http.response
---
## Module Contents

### BatchResponse

```python
class roboto.http.response.BatchResponse(/, **data: Any)
```

`from roboto.http import BatchResponse`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L129-L194)

Bases: `pydantic.BaseModel`, `Generic[Model]`

Response to a batch request: the outcome of each request element, in the order the request sent them.

Roboto applies or refuses each element on its own, so a batch can come back partly applied. `succeeded` and `failed` split the outcomes for a caller that does not need to know which request element produced which; `responses` traces each outcome back to the request element at the same position.

**Parameters**

- **data** (`Any`)

**Attributes**

- **BatchResponse.responses** (`list[BatchResponseElement[Model]]`): Outcome of each request element, at the same position as that element in the request.

**Properties**

- **BatchResponse.failed** (`list[roboto.exceptions.RobotoDomainException]`): Exception Roboto refused each refused element with, in request order.

- **BatchResponse.succeeded** (`list[Model]`): Result of each applied element, in request order.

  Elements applied with no content to return appear in neither `succeeded` nor `failed`.

#### BatchResponse.map_data()

```python
def map_data(
    transform: collections.abc.Callable[[Model], MappedModel],
) -> BatchResponse[MappedModel]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L153-L174)

Return a batch whose results are this batch's results passed through `transform`.

`Device.create_sessions` turns each `SessionRecord` in Roboto's response into a `Session` through it, so most code never calls it. `transform` runs only on elements that hold a result; a refused element keeps its exception, and every element keeps its position.

**Parameters**

- **transform** (`collections.abc.Callable[[Model], MappedModel]`): Function that builds the new result from an element's result.

**Returns**

- `BatchResponse[MappedModel]`: A new batch with one element per element of this one, in the same order. This batch is unchanged.

#### BatchResponse.single()

```python
def single() -> Model
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L176-L194)

Return the result of the only element of a one-element batch.

`Device.create_session` returns the result of its one-element batch through it, so most code never calls it. A refusal raises its exception instead of coming back inside a batch.

**Raises**

- [`RobotoDomainException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoDomainException): Roboto refused the element. The exception raised is the one Roboto reported.
- [`RobotoInternalException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoInternalException): The batch does not hold exactly one element, or its element holds neither a result nor an exception.

**Returns**

- `Model`

### BatchResponseElement

```python
class roboto.http.response.BatchResponseElement(/, **data: Any)
```

`from roboto.http import BatchResponseElement`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L42-L126)

Bases: `pydantic.BaseModel`, `Generic[Model]`

Outcome of one element of a batch request: its result if Roboto applied it, or its exception if refused.

An element never holds both. One that holds neither was applied with no content to return, as a single request answered with HTTP 204 would be.

**Parameters**

- **data** (`Any`)

**Attributes**

- **BatchResponseElement.data** (`Model | None`) = `None`: Result of the element, or `None` if Roboto refused it or applied it with no content to return.
- **BatchResponseElement.error** (`roboto.exceptions.RobotoDomainException | None`) = `None`: Exception Roboto refused the element with, or `None` if Roboto applied it.
- **BatchResponseElement.model_config**: Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

#### BatchResponseElement.serialize_error()

```python
def serialize_error(
    value: Optional[roboto.exceptions.RobotoDomainException],
    info: pydantic.SerializationInfo,
) -> Optional[dict[str, Any]]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L117-L126)

Serialize `error` as the envelope [`to_dict()`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoDomainException.to_dict) produces.

Pydantic calls this whenever an element is dumped, and `validate_error` reads the envelope back.

**Parameters**

- **value** (`Optional[roboto.exceptions.RobotoDomainException]`)
- **info** (`pydantic.SerializationInfo`)

**Returns**

- `Optional[dict[str, Any]]`

#### BatchResponseElement.validate_error()

```python
def validate_error(value: Any) -> Optional[roboto.exceptions.RobotoDomainException]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L61-L114)

Build the exception an element was refused with, from whichever form the refusal arrived in.

Pydantic calls this whenever an element is built or parsed. It accepts three forms:

- An exception object, as an element copied from another batch carries.
- The envelope [`to_dict()`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoDomainException.to_dict) produces.
- That envelope as JSON text, the form [`RobotoDomainException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoDomainException) declares to pydantic.

Only `None` yields `None`. Any other value is a refusal, whatever its shape: an envelope whose error code names no exception class in this SDK version, one missing its code or message, JSON that is not an envelope, and text that is not JSON all become a [`RobotoUnrecognizedErrorException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoUnrecognizedErrorException). It carries whatever code and message could be read, and the value's own text as its message when no message could be. A refused element therefore always appears in `BatchResponse.failed`, so a caller that checks `failed` cannot mistake a malformed refusal for success.

`plain` mode replaces the validation the `error` annotation would apply, which reads JSON text and would reject the exception object this returns.

**Parameters**

- **value** (`Any`)

**Returns**

- `Optional[roboto.exceptions.RobotoDomainException]`

### DEFAULT_RESPONSE_JSONPATH

```python
roboto.http.response.DEFAULT_RESPONSE_JSONPATH = ('data',)
```

`from roboto.http.response import DEFAULT_RESPONSE_JSONPATH`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L38-L38)

Path to the result within a Roboto response body: its top-level `data` field.

### HttpResponse

```python
class roboto.http.response.HttpResponse(response: urllib.response.addinfourl)
```

`from roboto.http.response import HttpResponse`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L373-L484)

Response to an HTTP request, with methods that parse its body.

Returned by the request methods of [`RobotoClient`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient) and [`HttpClient`](/docs/reference/python-sdk/roboto/http/http_client#roboto.http.http_client.HttpClient). Each `to_*` method reads the body and closes the response, so call at most one of them per response. The methods that build records raise `pydantic.ValidationError` when the body does not match the record type.

**Parameters**

- **response** (`urllib.response.addinfourl`): Response `urllib` returned, with its body not yet read.

**Properties**

- **HttpResponse.headers** (`dict[str, str] | None`): Headers of the response, keyed by name. Of several headers with one name, the last is kept.

  Return type: `Optional[dict[str, str]]`

- **HttpResponse.readable_response** (`urllib.response.addinfourl`): Response `urllib` returned, for reading the body directly.

- **HttpResponse.status** (`http.HTTPStatus`): HTTP status of the response. Reading it raises `RuntimeError` if the response has no status code.

#### HttpResponse.to_dict()

```python
def to_dict(json_path: Optional[collections.abc.Sequence[str]] = None) -> Any
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L454-L471)

Parse the body as JSON and return it, or the value at `json_path` in it.

The value can be any JSON value, not only an object.

**Parameters**

- **json_path** (`Optional[collections.abc.Sequence[str]]`): Keys leading from the top of the body to the value, outermost first. `None` returns the whole body.

**Returns**

- `Any`: The value at `json_path`, or `None` if the path does not lead to a value.

#### HttpResponse.to_int()

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

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L478-L484)

Return the body parsed as an integer.

**Raises**

- `ValueError`: The body is not an integer.

**Returns**

- `int`

#### HttpResponse.to_paginated_list()

```python
def to_paginated_list(record_type: Type[PydanticModel]) -> PaginatedList[PydanticModel]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L408-L420)

Parse the page in the body's `data` field, validating each of its items as `record_type`.

The page's `total_count` is left `None`, even when the body reports one.

**Parameters**

- **record_type** (`Type[PydanticModel]`): Pydantic model to validate each item as.

**Returns**

- `PaginatedList[PydanticModel]`

#### HttpResponse.to_record()

```python
def to_record(
    record_type: Type[PydanticModel],
    json_path: Optional[collections.abc.Sequence[str]] = DEFAULT_RESPONSE_JSONPATH,
) -> PydanticModel
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L422-L434)

Parse the value at `json_path` in the body as one `record_type`.

**Parameters**

- **record_type** (`Type[PydanticModel]`): Pydantic model to validate the value as.
- **json_path** (`Optional[collections.abc.Sequence[str]]`): Keys leading from the top of the body to the value, outermost first. Defaults to the body's `data` field. `None` parses the whole body.

**Returns**

- `PydanticModel`

#### HttpResponse.to_record_list()

```python
def to_record_list(
    record_type: Type[PydanticModel],
    json_path: Optional[collections.abc.Sequence[str]] = DEFAULT_RESPONSE_JSONPATH,
) -> list[PydanticModel]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L436-L448)

Parse the list at `json_path` in the body, validating each of its items as `record_type`.

**Parameters**

- **record_type** (`Type[PydanticModel]`): Pydantic model to validate each item as.
- **json_path** (`Optional[collections.abc.Sequence[str]]`): Keys leading from the top of the body to the list, outermost first. Defaults to the body's `data` field. `None` parses the whole body.

**Returns**

- `list[PydanticModel]`

#### HttpResponse.to_string()

```python
def to_string()
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L473-L476)

Return the body decoded as UTF-8 text.

#### HttpResponse.to_string_list()

```python
def to_string_list() -> list[str]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L450-L452)

Return the items of the list in the body's `data` field, each converted to a string.

**Returns**

- `list[str]`

### InvalidPaginationTokenError

```python
exception roboto.http.response.InvalidPaginationTokenError
```

`from roboto.http import InvalidPaginationTokenError`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L255-L262)

Bases: `ValueError`

The pagination token does not parse.

`PaginationToken.from_token` raises it. Pagination tokens are opaque to callers, so one that does not decode or names an unsupported scheme, such as a fabricated or truncated token, is an error in the caller's input rather than a failure in Roboto. Subclasses `ValueError`, so code that catches `ValueError` around `from_token` catches it too.

### MappedModel

```python
roboto.http.response.MappedModel
```

`from roboto.http.response import MappedModel`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L32-L32)

Type of the results a `BatchResponse` that `BatchResponse.map_data` returns holds.

### Model

```python
roboto.http.response.Model
```

`from roboto.http.response import Model`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L29-L29)

Type of the values a `BatchResponse`, `BatchResponseElement`, `PaginatedList` or `StreamedList` holds.

### PaginatedList

```python
class roboto.http.response.PaginatedList(/, **data: Any)
```

`from roboto.http import PaginatedList`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L197-L207)

Bases: `pydantic.BaseModel`, `Generic[Model]`

One page of a listing: the items on it, and a token that fetches the next page if there is one.

**Parameters**

- **data** (`Any`)

**Attributes**

- **PaginatedList.items** (`list[Model]`): Items on this page.
- **PaginatedList.next_token** (`str | None`) = `None`: Token that fetches the next page, or `None` if this is the last page. Pass it back unchanged.
- **PaginatedList.total_count** (`int | None`) = `None`: Number of items across every page, or `None` if the listing does not report it.

### PaginationToken

```python
class roboto.http.response.PaginationToken(
    scheme: PaginationTokenScheme,
    encoding: PaginationTokenEncoding,
    data: Any,
)
```

`from roboto.http import PaginationToken`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L265-L370)

Pagination token whose contents callers treat as opaque, with a version that lets its format change.

Roboto uses it to build and parse the page tokens some of its listings return; callers pass those tokens back unchanged and have no reason to build one. `to_token` writes the scheme, the encoding and the data as `<scheme>:<encoding>:<data>`, in URL-safe Base64 without `=` padding.

**Parameters**

- **scheme** (`PaginationTokenScheme`): Version of the token format.
- **encoding** (`PaginationTokenEncoding`): How `data` is written into the token.
- **data** (`Any`): Data the token carries: a string for `PaginationTokenEncoding.Raw`, or any JSON-serializable value for `Json`.

**Properties**

- **PaginationToken.data** (`Any`): Data the token carries, already deserialized when the encoding is `Json`.

#### PaginationToken.decode()

```python
@staticmethod
def decode(data: str) -> str
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L294-L298)

Return the text that URL-safe Base64 `data` encodes, restoring any trailing `=` padding it lacks.

**Parameters**

- **data** (`str`)

**Returns**

- `str`

#### PaginationToken.empty()

```python
@staticmethod
def empty() -> PaginationToken
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L284-L286)

Return a token that carries no data.

**Returns**

- `PaginationToken`

#### PaginationToken.encode()

```python
@staticmethod
def encode(data: str) -> str
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L289-L291)

Return `data` in URL-safe Base64, with the trailing `=` padding removed.

**Parameters**

- **data** (`str`)

**Returns**

- `str`

#### PaginationToken.from_token()

```python
@classmethod
def from_token(token: Optional[str]) -> PaginationToken
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L301-L330)

Parse a token string that `to_token` produced.

**Parameters**

- **token** (`Optional[str]`): Token string to parse. `None` returns a token that carries no data.

**Raises**

- [`InvalidPaginationTokenError`](/docs/reference/python-sdk/roboto/http/response#roboto.http.response.InvalidPaginationTokenError): `token` does not decode, does not name the `v1` scheme and a known encoding, or carries JSON data that does not parse.

**Returns**

- `PaginationToken`

#### PaginationToken.json_token()

```python
@classmethod
def json_token(data: Any) -> PaginationToken
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L333-L343)

Build a token that carries `data` serialized as JSON.

**Parameters**

- **data** (`Any`): JSON-serializable data to carry.

**Returns**

- `PaginationToken`

#### PaginationToken.to_token()

```python
def to_token() -> str
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L367-L370)

Render the token as the string `from_token` parses.

**Returns**

- `str`

### PaginationTokenEncoding

```python
class roboto.http.response.PaginationTokenEncoding(*args, **kwds)
```

`from roboto.http import PaginationTokenEncoding`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L233-L243)

Bases: `enum.Enum`

Encoding of the data a `PaginationToken` carries.

Roboto uses it when it builds and parses page tokens.

**Attributes**

- **PaginationTokenEncoding.Json** = `'json'`: Data serialized as JSON.
- **PaginationTokenEncoding.Raw** = `'raw'`: Data stored as a string, unchanged.

### PaginationTokenScheme

```python
class roboto.http.response.PaginationTokenScheme(*args, **kwds)
```

`from roboto.http import PaginationTokenScheme`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L246-L252)

Bases: `enum.Enum`

Version of the `PaginationToken` format.

Roboto uses it when it builds and parses page tokens.

**Attributes**

- **PaginationTokenScheme.V1** = `'v1'`

### PydanticModel

```python
roboto.http.response.PydanticModel
```

`from roboto.http.response import PydanticModel`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L35-L35)

Type of the pydantic model an `HttpResponse` method parses the body as.

### StreamedList

```python
class roboto.http.response.StreamedList(/, **data: Any)
```

`from roboto.http import StreamedList`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L210-L230)

Bases: `pydantic.BaseModel`, `Generic[Model]`

One read from a stream that may still be growing, such as the logs of an invocation that is running.

A `PaginatedList` pages through a fixed set of items. A stream can gain items between reads and may never end, so a read that finds nothing more does not mean the stream is complete.

**Parameters**

- **data** (`Any`)

**Attributes**

- **StreamedList.has_next** (`bool`): Whether more items are known to be available now, readable from `last_read`.

  `False` does not mean the stream has ended: more items may be written later.

- **StreamedList.items** (`list[Model]`): Items read from the stream.

- **StreamedList.last_read** (`str | None`): Token that marks the position after the last item read, to pass back to read the items that follow it.

  `None` if Roboto returned no position to resume from.

### logger

```python
roboto.http.response.logger
```

`from roboto.http.response import logger`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/http/response.py#L27-L27)
