---
sidebar:
  label: roboto.http.http_client
  order: 2
title: roboto.http.http_client
---
## Module Contents

### HttpClient

```python
class roboto.http.http_client.HttpClient(
    base_headers: Optional[dict[str, str]] = None,
    default_endpoint: Optional[str] = None,
    default_auth: Optional[roboto.http.request.HttpRequestDecorator] = None,
    requester: Optional[roboto.http.requester.RobotoRequester] = None,
    extra_headers_provider: Optional[Callable[[], dict[str, str]]] = None,
    default_timeout: Optional[float] = None,
    options: Optional[roboto.http.options.HttpClientOptions] = None,
)
```

`from roboto.http import HttpClient`

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

HTTP client that sends each request with the same headers, authentication, timeout and retry rules.

[`RobotoClient`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient) sends the SDK's requests to Roboto through one of these, and converts the errors it raises into Roboto exceptions (see [`HttpError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.HttpError)).

For each request, the client:

1. Passes the request through `default_auth`, when set.
2. Sends the base headers, then the headers `extra_headers_provider` returns, then the request's own, each replacing any earlier header of the same name.
3. Sends the request. When an attempt raises, attempts remain, and the retry predicate in `options` accepts the exception, it waits the request's `retry_wait` and tries again. Otherwise the method raises.

A request body (`data`) of bytes or a string is sent as given, a pydantic model as JSON holding the fields that were set, and any other value as the output of `json.dumps`. A pydantic model or a dictionary also sets `Content-Type: application/json`.

**Parameters**

- **base_headers** (`Optional[dict[str, str]]`): Headers to send with every request. `None` sends none.
- **default_endpoint** (`Optional[str]`): Base URL that `url` appends paths to. `None` leaves the client without one, and `url` then raises `ValueError`.
- **default_auth** (`Optional[roboto.http.request.HttpRequestDecorator]`): Function applied to every request before it is sent, such as [`BearerTokenDecorator`](/docs/reference/python-sdk/roboto/http/request_decorators#roboto.http.request_decorators.BearerTokenDecorator), which adds an access token. `None` sends requests as given.
- **requester** (`Optional[roboto.http.requester.RobotoRequester]`): Tool sending the requests, reported in every request's `X-Roboto-Requester` header, as `set_requester` sets it. `None` sends no such header.
- **extra_headers_provider** (`Optional[Callable[[], dict[str, str]]]`): Function called before every request, whose headers are added to the base headers. They stay there, so a header it stops returning keeps its last value. `None` adds none.
- **default_timeout** (`Optional[float]`): Timeout in seconds for requests that pass no `timeout`. It limits each wait to connect or to receive data, not the request as a whole. `None` waits indefinitely.
- **options** (`Optional[roboto.http.options.HttpClientOptions]`): Logging and retry behavior for every request. `None` uses the defaults of [`HttpClientOptions`](/docs/reference/python-sdk/roboto/http/options#roboto.http.options.HttpClientOptions).

**Properties**

- **HttpClient.auth_decorator** (`roboto.http.request.HttpRequestDecorator | None`): Function the client applies to every request before sending it, or `None` if it sends them as given.

  Return type: `Optional[roboto.http.request.HttpRequestDecorator]`

#### HttpClient.delete()

```python
def delete(
    url: str,
    data: Any = None,
    headers: Optional[dict] = None,
    idempotent: bool = True,
    retry_wait: Optional[roboto.http.request.RetryWaitFn] = None,
    timeout: roboto.env.Timeout = NotSet,
) -> roboto.http.response.HttpResponse
```

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

Send a DELETE request to `url` and return its response.

**Parameters**

- **url** (`str`): Full URL to send the request to. The `url` method builds one from the client's `default_endpoint`.
- **data** (`Any`): Body of the request, encoded as `HttpClient` describes. `None` sends no body.
- **headers** (`Optional[dict]`): Headers for this request only. Each replaces a client header of the same name.
- **idempotent** (`bool`): Whether sending the request more than once has the same effect as sending it once. The default retry predicate retries a timeout, a failed connection, or a 408, 500 or 504 status only for an idempotent request.
- **retry_wait** (`Optional[roboto.http.request.RetryWaitFn]`): Function that returns how many milliseconds to wait before the next attempt. `None` waits a random time up to a limit that starts at 1 second and doubles after each attempt, to at most 30 seconds.
- **timeout** (`roboto.env.Timeout`): Timeout in seconds for each wait to connect or to receive data. `None` waits indefinitely. Left unset, the client's `default_timeout` applies.

**Raises**

- [`ClientError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ClientError): The last attempt's response has a 4xx status.
- [`ServerError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ServerError): The last attempt's response has a status of 500 or above.
- [`HttpError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.HttpError): The last attempt's response has any other error status.
- `ConnectionRefusedError`: The server refused the connection on the last attempt.
- `urllib.error.URLError`: The last attempt could not be sent for another reason, such as a host name that does not resolve.
- `TimeoutError`: The server sent no response within `timeout` on the last attempt.

**Returns**

- `roboto.http.response.HttpResponse`

#### HttpClient.get()

```python
def get(
    url: str,
    headers: Optional[dict] = None,
    retry_wait: Optional[roboto.http.request.RetryWaitFn] = None,
    idempotent: bool = True,
    timeout: roboto.env.Timeout = NotSet,
) -> roboto.http.response.HttpResponse
```

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

Send a GET request to `url` and return its response.

**Parameters**

- **url** (`str`): Full URL to send the request to. The `url` method builds one from the client's `default_endpoint`.
- **headers** (`Optional[dict]`): Headers for this request only. Each replaces a client header of the same name.
- **retry_wait** (`Optional[roboto.http.request.RetryWaitFn]`): Function that returns how many milliseconds to wait before the next attempt. `None` waits a random time up to a limit that starts at 1 second and doubles after each attempt, to at most 30 seconds.
- **idempotent** (`bool`): Whether sending the request more than once has the same effect as sending it once. The default retry predicate retries a timeout, a failed connection, or a 408, 500 or 504 status only for an idempotent request.
- **timeout** (`roboto.env.Timeout`): Timeout in seconds for each wait to connect or to receive data. `None` waits indefinitely. Left unset, the client's `default_timeout` applies.

**Raises**

- [`ClientError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ClientError): The last attempt's response has a 4xx status.
- [`ServerError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ServerError): The last attempt's response has a status of 500 or above.
- [`HttpError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.HttpError): The last attempt's response has any other error status.
- `ConnectionRefusedError`: The server refused the connection on the last attempt.
- `urllib.error.URLError`: The last attempt could not be sent for another reason, such as a host name that does not resolve.
- `TimeoutError`: The server sent no response within `timeout` on the last attempt.

**Returns**

- `roboto.http.response.HttpResponse`

#### HttpClient.patch()

```python
def patch(
    url: str,
    data: Any = None,
    headers: Optional[dict] = None,
    idempotent: bool = True,
    retry_wait: Optional[roboto.http.request.RetryWaitFn] = None,
    timeout: roboto.env.Timeout = NotSet,
) -> roboto.http.response.HttpResponse
```

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

Send a PATCH request to `url` and return its response.

**Parameters**

- **url** (`str`): Full URL to send the request to. The `url` method builds one from the client's `default_endpoint`.
- **data** (`Any`): Body of the request, encoded as `HttpClient` describes. `None` sends no body.
- **headers** (`Optional[dict]`): Headers for this request only. Each replaces a client header of the same name.
- **idempotent** (`bool`): Whether sending the request more than once has the same effect as sending it once. The default retry predicate retries a timeout, a failed connection, or a 408, 500 or 504 status only for an idempotent request.
- **retry_wait** (`Optional[roboto.http.request.RetryWaitFn]`): Function that returns how many milliseconds to wait before the next attempt. `None` waits a random time up to a limit that starts at 1 second and doubles after each attempt, to at most 30 seconds.
- **timeout** (`roboto.env.Timeout`): Timeout in seconds for each wait to connect or to receive data. `None` waits indefinitely. Left unset, the client's `default_timeout` applies.

**Raises**

- [`ClientError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ClientError): The last attempt's response has a 4xx status.
- [`ServerError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ServerError): The last attempt's response has a status of 500 or above.
- [`HttpError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.HttpError): The last attempt's response has any other error status.
- `ConnectionRefusedError`: The server refused the connection on the last attempt.
- `urllib.error.URLError`: The last attempt could not be sent for another reason, such as a host name that does not resolve.
- `TimeoutError`: The server sent no response within `timeout` on the last attempt.

**Returns**

- `roboto.http.response.HttpResponse`

#### HttpClient.post()

```python
def post(
    url: str,
    data: Any = None,
    headers: Optional[dict] = None,
    idempotent: bool = False,
    retry_wait: Optional[roboto.http.request.RetryWaitFn] = None,
    timeout: roboto.env.Timeout = NotSet,
) -> roboto.http.response.HttpResponse
```

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

Send a POST request to `url` and return its response.

**Parameters**

- **url** (`str`): Full URL to send the request to. The `url` method builds one from the client's `default_endpoint`.
- **data** (`Any`): Body of the request, encoded as `HttpClient` describes. `None` sends no body.
- **headers** (`Optional[dict]`): Headers for this request only. Each replaces a client header of the same name.
- **idempotent** (`bool`): Whether sending the request more than once has the same effect as sending it once. The default retry predicate retries a timeout, a failed connection, or a 408, 500 or 504 status only for an idempotent request.
- **retry_wait** (`Optional[roboto.http.request.RetryWaitFn]`): Function that returns how many milliseconds to wait before the next attempt. `None` waits a random time up to a limit that starts at 1 second and doubles after each attempt, to at most 30 seconds.
- **timeout** (`roboto.env.Timeout`): Timeout in seconds for each wait to connect or to receive data. `None` waits indefinitely. Left unset, the client's `default_timeout` applies.

**Raises**

- [`ClientError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ClientError): The last attempt's response has a 4xx status.
- [`ServerError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ServerError): The last attempt's response has a status of 500 or above.
- [`HttpError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.HttpError): The last attempt's response has any other error status.
- `ConnectionRefusedError`: The server refused the connection on the last attempt.
- `urllib.error.URLError`: The last attempt could not be sent for another reason, such as a host name that does not resolve.
- `TimeoutError`: The server sent no response within `timeout` on the last attempt.

**Returns**

- `roboto.http.response.HttpResponse`

#### HttpClient.put()

```python
def put(
    url: str,
    data: Any = None,
    headers: Optional[dict] = None,
    idempotent: bool = True,
    retry_wait: Optional[roboto.http.request.RetryWaitFn] = None,
    timeout: roboto.env.Timeout = NotSet,
) -> roboto.http.response.HttpResponse
```

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

Send a PUT request to `url` and return its response.

**Parameters**

- **url** (`str`): Full URL to send the request to. The `url` method builds one from the client's `default_endpoint`.
- **data** (`Any`): Body of the request, encoded as `HttpClient` describes. `None` sends no body.
- **headers** (`Optional[dict]`): Headers for this request only. Each replaces a client header of the same name.
- **idempotent** (`bool`): Whether sending the request more than once has the same effect as sending it once. The default retry predicate retries a timeout, a failed connection, or a 408, 500 or 504 status only for an idempotent request.
- **retry_wait** (`Optional[roboto.http.request.RetryWaitFn]`): Function that returns how many milliseconds to wait before the next attempt. `None` waits a random time up to a limit that starts at 1 second and doubles after each attempt, to at most 30 seconds.
- **timeout** (`roboto.env.Timeout`): Timeout in seconds for each wait to connect or to receive data. `None` waits indefinitely. Left unset, the client's `default_timeout` applies.

**Raises**

- [`ClientError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ClientError): The last attempt's response has a 4xx status.
- [`ServerError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.ServerError): The last attempt's response has a status of 500 or above.
- [`HttpError`](/docs/reference/python-sdk/roboto/exceptions/http#roboto.exceptions.http.HttpError): The last attempt's response has any other error status.
- `ConnectionRefusedError`: The server refused the connection on the last attempt.
- `urllib.error.URLError`: The last attempt could not be sent for another reason, such as a host name that does not resolve.
- `TimeoutError`: The server sent no response within `timeout` on the last attempt.

**Returns**

- `roboto.http.response.HttpResponse`

#### HttpClient.set_requester()

```python
def set_requester(requester: roboto.http.requester.RobotoRequester)
```

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

Set the tool reported in the `X-Roboto-Requester` header of every later request.

**Parameters**

- **requester** (`roboto.http.requester.RobotoRequester`): Tool, version and platform sending the requests. Fields left `None` are left out of the header.

#### HttpClient.url()

```python
def url(path: str) -> str
```

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

Return the URL of `path` under the client's `default_endpoint`.

**Parameters**

- **path** (`str`): Path to append to the endpoint, without a leading slash.

**Raises**

- `ValueError`: The client has no `default_endpoint`.

**Returns**

- `str`

### is_expected_to_be_transient()

```python
def roboto.http.http_client.is_expected_to_be_transient(
    request: roboto.http.request.HttpRequest,
    exc: BaseException,
) -> bool
```

`from roboto.http import is_expected_to_be_transient`

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

Return whether a failed attempt at a request is expected to succeed if tried again.

`HttpClient` retries with this predicate unless [`predicate`](/docs/reference/python-sdk/roboto/http/options#roboto.http.options.HttpRetryOptions.predicate) names another. A predicate of your own can call it to keep these rules and add to them. It retries:

- A host name that fails to resolve, for any request, since the request never reached the server.
- A 429, 502 or 503 status, for any request.
- A 408, 500 or 504 status, a connection that fails or closes, or a timeout, only when `request.idempotent` is `True`, since the server may have received and acted on the request.

Nothing else is retried.

**Parameters**

- **request** (`roboto.http.request.HttpRequest`): Request whose attempt failed.
- **exc** (`BaseException`): Exception the failed attempt raised.

**Returns**

- `bool`

### logger

```python
roboto.http.http_client.logger
```

`from roboto.http.http_client import logger`

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