Skip to content

User

toggl_api.UserEndpoint

Bases: TogglEndpoint[Any]

Endpoint for retrieving user data.

Official Documentation

PARAMETER DESCRIPTION
auth

Authentication for the client.

TYPE: BasicAuth

client

Optional client to be passed to be used for requests. Useful when a global client is used and needs to be recycled.

TYPE: Client | None DEFAULT: None

timeout

How long it takes for the client to timeout. Keyword Only. Defaults to 10 seconds.

TYPE: Timeout | int DEFAULT: 10

re_raise

Whether to raise all HTTPStatusError errors and not handle them internally. Keyword Only.

TYPE: bool DEFAULT: False

retries

Max retries to attempt if the server returns a 5xx status_code. Has no effect if re_raise is True. Keyword Only.

TYPE: int DEFAULT: 3

METHOD DESCRIPTION
verify_authentication

Check if user is correctly authenticated with the Toggl API.

get_details

Return details for the current user.

Source code in src/toggl_api/_user.py
 17
 18
 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
 72
 73
 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class UserEndpoint(TogglEndpoint[Any]):
    """Endpoint for retrieving user data.

    [Official Documentation](https://engineering.toggl.com/docs/api/me)

    Params:
        auth: Authentication for the client.
        client: Optional client to be passed to be used for requests. Useful
            when a global client is used and needs to be recycled.
        timeout: How long it takes for the client to timeout. Keyword Only.
            Defaults to 10 seconds.
        re_raise: Whether to raise all HTTPStatusError errors and not handle them
            internally. Keyword Only.
        retries: Max retries to attempt if the server returns a *5xx* status_code.
            Has no effect if re_raise is `True`. Keyword Only.
    """

    def __init__(
        self,
        auth: BasicAuth,
        *,
        client: Client | None = None,
        timeout: Timeout | int = 10,
        re_raise: bool = False,
        retries: int = 3,
    ) -> None:
        super().__init__(
            auth,
            client=client,
            timeout=timeout,
            re_raise=re_raise,
            retries=retries,
        )

    @staticmethod
    def verify_authentication(
        auth: BasicAuth,
        *,
        client: Client | None = None,
    ) -> bool:
        """Check if user is correctly authenticated with the Toggl API.

        [Official Documentation](https://engineering.toggl.com/docs/api/me#get-logged)

        Examples:
            >>> UserEndpoint.verify_authentication(auth)
            True

            >>> auth = generate_authentication()
            >>> UserEndpoint.verify_authentication(auth)
            True

        Args:
            auth: Basic authentication object either created manually or one
                of the provided authentication utilities.
            client: Optional client for making the requests with when using a
                singleton/global client.

        Raises:
            HTTPStatusError: If anything that is error status code that is not
                a FORBIDDEN code.

        Returns:
            True if successfully verified authentication else False.
        """
        client = client or Client()
        try:
            client.get(
                TogglEndpoint.BASE_ENDPOINT + "me/logged",
                auth=auth,
            ).raise_for_status()
        except HTTPStatusError as err:
            log.critical("Failed to verify authentication!")
            log.exception("%s")
            if err.response.status_code != codes.FORBIDDEN:
                raise

            return False

        return True

    def get_details(self) -> dict[str, Any]:
        """Return details for the current user.

        [Official Documentation](https://engineering.toggl.com/docs/api/me#get-me)

        Raises:
            HTTPStatusError: If the request is not a successful status code.

        Returns:
            User details in a raw dictionary.
        """
        return cast(
            "dict[str, Any]",
            cast(
                "Response",
                self.request("me", raw=True),
            ).json(),
        )

verify_authentication staticmethod

verify_authentication(
    auth: BasicAuth, *, client: Client | None = None
) -> bool

Check if user is correctly authenticated with the Toggl API.

Official Documentation

Examples:

>>> UserEndpoint.verify_authentication(auth)
True
>>> auth = generate_authentication()
>>> UserEndpoint.verify_authentication(auth)
True
PARAMETER DESCRIPTION
auth

Basic authentication object either created manually or one of the provided authentication utilities.

TYPE: BasicAuth

client

Optional client for making the requests with when using a singleton/global client.

TYPE: Client | None DEFAULT: None

RAISES DESCRIPTION
HTTPStatusError

If anything that is error status code that is not a FORBIDDEN code.

RETURNS DESCRIPTION
bool

True if successfully verified authentication else False.

Source code in src/toggl_api/_user.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@staticmethod
def verify_authentication(
    auth: BasicAuth,
    *,
    client: Client | None = None,
) -> bool:
    """Check if user is correctly authenticated with the Toggl API.

    [Official Documentation](https://engineering.toggl.com/docs/api/me#get-logged)

    Examples:
        >>> UserEndpoint.verify_authentication(auth)
        True

        >>> auth = generate_authentication()
        >>> UserEndpoint.verify_authentication(auth)
        True

    Args:
        auth: Basic authentication object either created manually or one
            of the provided authentication utilities.
        client: Optional client for making the requests with when using a
            singleton/global client.

    Raises:
        HTTPStatusError: If anything that is error status code that is not
            a FORBIDDEN code.

    Returns:
        True if successfully verified authentication else False.
    """
    client = client or Client()
    try:
        client.get(
            TogglEndpoint.BASE_ENDPOINT + "me/logged",
            auth=auth,
        ).raise_for_status()
    except HTTPStatusError as err:
        log.critical("Failed to verify authentication!")
        log.exception("%s")
        if err.response.status_code != codes.FORBIDDEN:
            raise

        return False

    return True

get_details

get_details() -> dict[str, Any]

Return details for the current user.

Official Documentation

RAISES DESCRIPTION
HTTPStatusError

If the request is not a successful status code.

RETURNS DESCRIPTION
dict[str, Any]

User details in a raw dictionary.

Source code in src/toggl_api/_user.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def get_details(self) -> dict[str, Any]:
    """Return details for the current user.

    [Official Documentation](https://engineering.toggl.com/docs/api/me#get-me)

    Raises:
        HTTPStatusError: If the request is not a successful status code.

    Returns:
        User details in a raw dictionary.
    """
    return cast(
        "dict[str, Any]",
        cast(
            "Response",
            self.request("me", raw=True),
        ).json(),
    )

toggl_api.asyncio.AsyncUserEndpoint

Bases: TogglAsyncEndpoint[Any]

Endpoint for retrieving user data.

The synchronous sibling UserEndpoint has access to static method for verifying authentication.

Official Documentation

PARAMETER DESCRIPTION
auth

Authentication for the client.

TYPE: BasicAuth

client

Optional async client to be passed to be used for requests.

TYPE: AsyncClient | None DEFAULT: None

timeout

How long it takes for the client to timeout. Keyword Only. Defaults to 10 seconds.

TYPE: int DEFAULT: 10

re_raise

Whether to raise all HTTPStatusError errors and not handle them internally. Keyword Only.

TYPE: bool DEFAULT: False

retries

Max retries to attempt if the server returns a 5xx status_code. Has no effect if re_raise is True. Keyword Only.

TYPE: int DEFAULT: 3

METHOD DESCRIPTION
get_details

Return details for the current user.

get_details async

get_details() -> dict[str, Any]

Return details for the current user.

Official Documentation

RAISES DESCRIPTION
HTTPStatusError

If the request is not a successful status code.

RETURNS DESCRIPTION
dict[str, Any]

User details in a dictionary.