xerberus.session

Documentation for eth_defi.xerberus.session Python module.

HTTP session management for Xerberus public REST API.

Provides session creation with retry logic, paced rate limiting under the public 10 requests / 60 s quota, and dual auth headers (x-api-key + x-user-email).

Authentication requires both an API key and the email registered with that key. Pass them explicitly via api_key / api_email, or set XERBERUS_API_KEY and XERBERUS_API_EMAIL. Do not invent or probe candidate emails; only use operator-supplied values.

For API fetch helpers, see eth_defi.xerberus.api. See README-xerberus.md for the full authentication contract.

Module Attributes

DEFAULT_RETRIES

Default number of retries for API requests

DEFAULT_BACKOFF_FACTOR

Default backoff factor for retries (seconds)

Functions

create_xerberus_session([api_url, api_key, ...])

Create a XerberusSession configured for Xerberus API.

Classes

XerberusSession

A requests.Session subclass for Xerberus API.

DEFAULT_RETRIES: int = 5

Default number of retries for API requests

DEFAULT_BACKOFF_FACTOR: float = 0.5

Default backoff factor for retries (seconds)

class XerberusSession

Bases: requests.sessions.Session

A requests.Session subclass for Xerberus API.

Carries the API URL, API key, and registered email. Sets default headers (x-api-key, x-user-email) and enforces minimum spacing between authenticated requests so the client does not burst under the minute bucket.

Both credentials are required. Prefer passing them explicitly:

create_xerberus_session(api_key=key, api_email=email)

When omitted, values are read from XERBERUS_API_KEY and XERBERUS_API_EMAIL. Agents must not invent email addresses; only use operator-supplied env vars or explicit arguments.

Use create_xerberus_session() to create instances.

Create a session.

Parameters
  • api_url – Xerberus public API base URL.

  • api_key – API key. If None, reads XERBERUS_API_KEY.

  • api_email – Email registered with the key. If None, reads XERBERUS_API_EMAIL. Pass this explicitly when available; never invent a value.

  • min_request_interval – Minimum seconds between authenticated requests.

__init__(api_url='https://api.xerberus.io/public/v1', api_key=None, api_email=None, min_request_interval=7.5)

Create a session.

Parameters
  • api_url (str) – Xerberus public API base URL.

  • api_key (Optional[str]) – API key. If None, reads XERBERUS_API_KEY.

  • api_email (Optional[str]) – Email registered with the key. If None, reads XERBERUS_API_EMAIL. Pass this explicitly when available; never invent a value.

  • min_request_interval (float) – Minimum seconds between authenticated requests.

api_url: str

Xerberus API base URL.

api_key: str

Xerberus API key.

api_email: str

Registered email associated with the API key (sent as x-user-email).

min_request_interval: float

Minimum seconds between authenticated requests.

close()

Closes all adapters and as such the session

delete(url, **kwargs)

Sends a DELETE request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

get(url, **kwargs)

Sends a GET request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

get_adapter(url)

Returns the appropriate connection adapter for the given URL.

Return type

requests.adapters.BaseAdapter

get_redirect_target(resp)

Receives a Response. Returns a redirect URI or None

head(url, **kwargs)

Sends a HEAD request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

merge_environment_settings(url, proxies, stream, verify, cert)

Check the environment and merge it with some settings.

Return type

dict

mount(prefix, adapter)

Registers a connection adapter to a prefix.

Adapters are sorted in descending order by prefix length.

options(url, **kwargs)

Sends a OPTIONS request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

patch(url, data=None, **kwargs)

Sends a PATCH request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

post(url, data=None, json=None, **kwargs)

Sends a POST request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

prepare_request(request)

Constructs a PreparedRequest for transmission and returns it. The PreparedRequest has settings merged from the Request instance and those of the Session.

Parameters

requestRequest instance to prepare with this session’s settings.

Return type

requests.PreparedRequest

put(url, data=None, **kwargs)

Sends a PUT request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

rebuild_auth(prepared_request, response)

When being redirected we may want to strip authentication from the request to avoid leaking credentials. This method intelligently removes and reapplies authentication where possible to avoid credential loss.

rebuild_method(prepared_request, response)

When being redirected we may want to change the method of the request based on certain specs or browser behavior.

rebuild_proxies(prepared_request, proxies)

This method re-evaluates the proxy configuration by considering the environment variables. If we are redirected to a URL covered by NO_PROXY, we strip the proxy configuration. Otherwise, we set missing proxy keys for this URL (in case they were stripped by a previous redirect).

This method also replaces the Proxy-Authorization header where necessary.

Return type

dict

resolve_redirects(resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs)

Receives a Response. Returns a generator of Responses or Requests.

send(request, **kwargs)

Send a given PreparedRequest.

Return type

requests.Response

should_strip_auth(old_url, new_url)

Decide whether Authorization header should be removed when redirecting

headers

A case-insensitive dictionary of headers to be sent on each Request sent from this Session.

auth

Default Authentication tuple or object to attach to Request.

proxies

Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’}) to be used on each Request.

hooks

Event-handling hooks.

params

Dictionary of querystring data to attach to each Request. The dictionary values may be lists for representing multivalued query parameters.

stream

Stream response content default.

verify

SSL Verification default. Defaults to True, requiring requests to verify the TLS certificate at the remote end. If verify is set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Only set this to False for testing.

cert

SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

max_redirects

Maximum number of redirects allowed. If the request exceeds this limit, a TooManyRedirects exception is raised. This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is 30.

trust_env

Trust environment settings for proxy configuration, default authentication and similar.

cookies

A CookieJar containing all currently outstanding cookies set on this session. By default it is a RequestsCookieJar, but may be any other cookielib.CookieJar compatible object.

request(method, url, *args, **kwargs)

Send an HTTP request with paced spacing for authenticated calls.

Healthcheck paths under /healthcheck skip the min-interval sleep so liveness probes stay cheap and outside the paced budget.

Parameters
  • method – HTTP method.

  • url – Request URL.

Returns

Response object from the parent session.

create_xerberus_session(api_url='https://api.xerberus.io/public/v1', api_key=None, api_email=None, retries=5, backoff_factor=0.5, requests_per_minute=8.0, min_request_interval=7.5, pool_maxsize=8, rate_limit_db_path=PosixPath('/home/runner/.tradingstrategy/vaults/xerberus/rate-limit.sqlite'))

Create a XerberusSession configured for Xerberus API.

The session is configured with:

  • Dual auth headers (x-api-key + x-user-email)

  • Minute-bucket rate limiting under the public 10/min quota (default 8/min)

  • Minimum request spacing to avoid burst-then-idle behaviour

  • Retry logic for 429/5xx with Retry-After respect

Prefer passing credentials explicitly when they are already available (e.g. after source .local-test.env loaded operator secrets):

import os

from eth_defi.xerberus.session import create_xerberus_session

session = create_xerberus_session(
    api_key=os.environ["XERBERUS_API_KEY"],
    api_email=os.environ["XERBERUS_API_EMAIL"],
)

Or rely on environment defaults (operator-set only; agents must never invent the email):

session = create_xerberus_session()
Parameters
  • api_url (str) – Xerberus API base URL.

  • api_key (Optional[str]) – API key. If None, reads from XERBERUS_API_KEY. Pass explicitly when known.

  • api_email (Optional[str]) – Email registered with the API key (x-user-email). If None, reads from XERBERUS_API_EMAIL. Pass explicitly when known. Do not guess this value.

  • retries (int) – Maximum retry attempts for failed requests.

  • backoff_factor (float) – Exponential backoff factor.

  • requests_per_minute (float) – Hard minute-bucket cap (default 8, under the 10/min public limit).

  • min_request_interval (float) – Minimum seconds between authenticated requests.

  • pool_maxsize (int) – Connection pool size (serial clients can keep this small).

  • rate_limit_db_path (pathlib.Path) – SQLite path for rate-limit state.

Returns

Configured XerberusSession.

Return type

eth_defi.xerberus.session.XerberusSession