Skip to content

Client

A module for keeping track of data related to the API.

This module contains the Client and AuthenticatedClient classes. Eth 2 Key Manager API specification dictates that all sensitive endpoints must be authenticated. All endpoints do require authentication so the AuthenticatedClient is the class you interact with. The Client class is never accessed directly.

Refer to the API specification: https://ethereum.github.io/keymanager-APIs/

AuthenticatedClient

Bases: Client

A Client which has been authenticated for use on secured endpoints.

Attributes:

Name Type Description
token str

The token to be used for authentication.

Examples:

>>> from eth_2_key_manager_api_client.client import AuthenticatedClient
>>> authenticated_client = AuthenticatedClient(base_url="http://localhost:7500", token="test_token")
>>> authenticated_client.base_url
'http://localhost:7500'
>>> authenticated_client.token
'test_token'
>>> authenticated_client.get_headers()['Authorization']
'Bearer test_token'
Source code in eth_2_key_manager_api_client/client.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
@attr.s(auto_attribs=True)
class AuthenticatedClient(Client):
    """A Client which has been authenticated for use on secured endpoints.

    Attributes:
        token: The token to be used for authentication.

    Examples:
        >>> from eth_2_key_manager_api_client.client import AuthenticatedClient
        >>> authenticated_client = AuthenticatedClient(base_url="http://localhost:7500", token="test_token")
        >>> authenticated_client.base_url
        'http://localhost:7500'
        >>> authenticated_client.token
        'test_token'
        >>> authenticated_client.get_headers()['Authorization']
        'Bearer test_token'

    """

    token: str
    prefix: str = "Bearer"
    auth_header_name: str = "Authorization"

    def get_headers(self) -> Dict[str, str]:
        """Get headers to be used in authenticated endpoints"""
        auth_header_value = f"{self.prefix} {self.token}" if self.prefix else self.token
        return {self.auth_header_name: auth_header_value, **self.headers}

get_headers()

Get headers to be used in authenticated endpoints

Source code in eth_2_key_manager_api_client/client.py
 97
 98
 99
100
def get_headers(self) -> Dict[str, str]:
    """Get headers to be used in authenticated endpoints"""
    auth_header_value = f"{self.prefix} {self.token}" if self.prefix else self.token
    return {self.auth_header_name: auth_header_value, **self.headers}

Client

A class for keeping track of data related to the API.

Attributes:

Name Type Description
base_url str

The base URL for the API, all requests are made to a relative path to this URL.

cookies Dict[str, str]

A dictionary of cookies to be sent with every request.

headers Dict[str, str]

A dictionary of headers to be sent with every request.

timeout float

The maximum amount of a time in seconds a request can take. API functions will raise httpx.TimeoutException if this is exceeded.

verify_ssl Union[str, bool, SSLContext]

Whether or not to verify the SSL certificate of the API server. This should be True in production, but can be set to False for testing purposes.

raise_on_unexpected_status bool

Whether or not to raise an errors.UnexpectedStatus if the API returns a status code that was not documented in the source OpenAPI document.

follow_redirects bool

Whether or not to follow redirects. Default value is False.

Examples:

>>> from eth_2_key_manager_api_client.client import Client
>>> client = Client(base_url="http://localhost:7500")
>>> client.base_url
'http://localhost:7500'
Source code in eth_2_key_manager_api_client/client.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@attr.s(auto_attribs=True)
class Client:
    """A class for keeping track of data related to the API.

    Attributes:
        base_url: The base URL for the API, all requests are made to a relative path to this URL.
        cookies: A dictionary of cookies to be sent with every request.
        headers: A dictionary of headers to be sent with every request.
        timeout: The maximum amount of a time in seconds a request can take. API functions will raise
            httpx.TimeoutException if this is exceeded.
        verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production,
            but can be set to False for testing purposes.
        raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
            status code that was not documented in the source OpenAPI document.
        follow_redirects: Whether or not to follow redirects. Default value is False.

    Examples:
        >>> from eth_2_key_manager_api_client.client import Client
        >>> client = Client(base_url="http://localhost:7500")
        >>> client.base_url
        'http://localhost:7500'

    """

    base_url: str
    cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
    headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
    timeout: float = attr.ib(5.0, kw_only=True)
    verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True)
    raise_on_unexpected_status: bool = attr.ib(False, kw_only=True)
    follow_redirects: bool = attr.ib(False, kw_only=True)

    def get_headers(self) -> Dict[str, str]:
        """Get headers to be used in all endpoints"""
        return {**self.headers}

    def with_headers(self, headers: Dict[str, str]) -> "Client":
        """Get a new client matching this one with additional headers"""
        return attr.evolve(self, headers={**self.headers, **headers})

    def get_cookies(self) -> Dict[str, str]:
        return {**self.cookies}

    def with_cookies(self, cookies: Dict[str, str]) -> "Client":
        """Get a new client matching this one with additional cookies"""
        return attr.evolve(self, cookies={**self.cookies, **cookies})

    def get_timeout(self) -> float:
        return self.timeout

    def with_timeout(self, timeout: float) -> "Client":
        """Get a new client matching this one with a new timeout (in seconds)"""
        return attr.evolve(self, timeout=timeout)

get_headers()

Get headers to be used in all endpoints

Source code in eth_2_key_manager_api_client/client.py
51
52
53
def get_headers(self) -> Dict[str, str]:
    """Get headers to be used in all endpoints"""
    return {**self.headers}

with_cookies(cookies)

Get a new client matching this one with additional cookies

Source code in eth_2_key_manager_api_client/client.py
62
63
64
def with_cookies(self, cookies: Dict[str, str]) -> "Client":
    """Get a new client matching this one with additional cookies"""
    return attr.evolve(self, cookies={**self.cookies, **cookies})

with_headers(headers)

Get a new client matching this one with additional headers

Source code in eth_2_key_manager_api_client/client.py
55
56
57
def with_headers(self, headers: Dict[str, str]) -> "Client":
    """Get a new client matching this one with additional headers"""
    return attr.evolve(self, headers={**self.headers, **headers})

with_timeout(timeout)

Get a new client matching this one with a new timeout (in seconds)

Source code in eth_2_key_manager_api_client/client.py
69
70
71
def with_timeout(self, timeout: float) -> "Client":
    """Get a new client matching this one with a new timeout (in seconds)"""
    return attr.evolve(self, timeout=timeout)