Skip to content

api

detdevlib.etl.api

APIClient

A client for interacting with a RESTful API.

This class handles sessions, authentication, and robust error handling. Wraps around requests.Session.

Attributes:

Name Type Description
base_url

The base URL for the API endpoint.

timeout

The timeout in seconds for requests.

session

The requests.Session object.

__init__

__init__(
    base_url: str,
    headers: Optional[Dict[str, str]] = None,
    timeout: int = 30,
)

Initializes the API Client.

Parameters:

Name Type Description Default
base_url str

The base URL for the API endpoint (e.g., "https://api.example.com").

required
headers Optional[Dict[str, str]]

A dictionary of headers to include in all requests.

None
timeout int

The timeout in seconds for requests.

30

set_auth_token

set_auth_token(token: str, scheme: str = 'Token')

Sets an Authorization token header.

Parameters:

Name Type Description Default
token str

The authentication token.

required
scheme str

The authentication scheme (e.g., "Bearer", "Token").

'Token'

request

request(method: str, endpoint: str, **kwargs) -> Any

Constructs and sends an API request.

Parameters:

Name Type Description Default
method str

The HTTP method (GET, POST, PUT, DELETE).

required
endpoint str

The API endpoint path.

required
**kwargs

Additional keyword arguments for the requests session.

{}

Returns:

Type Description
Any

The JSON response body as a python object, or None if it cant be decoded.

Raises:

Type Description
HTTPError

For 4xx/5xx responses.

RequestException

For other request-related errors.

close

close()

Closes the underlying requests session.