Skip to content

Projects

toggl_api.ProjectBody dataclass

Bases: BaseBody

JSON body dataclass for PUT, POST & PATCH requests.

METHOD DESCRIPTION
format

Format the body for JSON requests.

ATTRIBUTE DESCRIPTION
name

Name of the project. Defaults to None. Will be required if its a POST request.

TYPE: str | None

active

Whether the project is archived or active.

TYPE: bool | Literal['both']

is_private

Whether the project is private or not. Defaults to True.

TYPE: bool | None

client_name

Client name if client_id is not set. Defaults to None. If client_id is

TYPE: str | None

color

Color of the project. Refer to BASIC_COLORS

TYPE: str | None

start_date

Date to set the start of a project. If not set or start date is after

TYPE: date | None

end_date

Date to set the end of the project. If not set or start date is after

TYPE: date | None

since

Timestamp for querying for projects with the 'collect' endpoint.

TYPE: date | int | None

user_ids

Query for specific projects with assocciated users. API only.

TYPE: list[int]

client_ids

Query for specific projects with assocciated clients.

TYPE: list[int]

group_ids

Query for specific projects with assocciated groups. API only

TYPE: list[int]

statuses

Query for specific statuses when using the collect endpoint.

TYPE: list[Status]

Source code in src/toggl_api/_project.py
 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@dataclass
class ProjectBody(BaseBody):
    """JSON body dataclass for PUT, POST & PATCH requests."""

    name: str | None = field(default=None)
    """Name of the project. Defaults to None. Will be required if its a POST request."""

    active: bool | Literal["both"] = field(default=True)
    """Whether the project is archived or active.
    The literal 'both' is used for querying."""
    is_private: bool | None = field(
        default=True,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    """Whether the project is private or not. Defaults to True."""

    client_id: int | None = field(
        default=None,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    client_name: str | None = field(
        default=None,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    """Client name if client_id is not set. Defaults to None. If client_id is
    set the client_name will be ignored."""

    color: str | None = field(
        default=None,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    """Color of the project. Refer to [BASIC_COLORS][toggl_api.ProjectEndpoint.BASIC_COLORS]
    for accepted colors for non-premium users."""

    start_date: date | None = field(
        default=None,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    """Date to set the start of a project. If not set or start date is after
    the end date the end date will be ignored."""

    end_date: date | None = field(
        default=None,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    """Date to set the end of the project. If not set or start date is after
    the end date the end date will be ignored."""

    since: date | int | None = field(
        default=None,
        metadata={"endpoints": frozenset(("collect",))},
    )
    """Timestamp for querying for projects with the 'collect' endpoint.
    Retrieve projects created/modified/deleted since this date using UNIX timestamp.
    *If using local cache deleted projects are not present.*
    """

    user_ids: list[int] = field(
        default_factory=list,
        metadata={"endpoints": frozenset(("collect",))},
    )
    """Query for specific projects with assocciated users. API only."""

    client_ids: list[int] = field(
        default_factory=list,
        metadata={"endpoints": frozenset(("collect",))},
    )
    """Query for specific projects with assocciated clients."""

    group_ids: list[int] = field(
        default_factory=list,
        metadata={"endpoints": frozenset(("collect",))},
    )
    """Query for specific projects with assocciated groups. API only"""

    statuses: list[TogglProject.Status] = field(
        default_factory=list,
        metadata={"endpoints": frozenset(("collect",))},
    )
    """Query for specific statuses when using the collect endpoint.
    Deleted status only works with the remote API.
    """

    def _format_collect(self, body: dict[str, Any]) -> None:
        if self.since:
            body["since"] = get_timestamp(self.since)
        if self.user_ids:
            body["user_ids"] = self.user_ids
        if self.client_ids:
            body["client_ids"] = self.client_ids
        if self.group_ids:
            body["group_ids"] = self.group_ids
        if self.statuses:
            body["statuses"] = [s.name.lower() for s in self.statuses]

    def format(self, endpoint: str, **body: Any) -> dict[str, Any]:
        """Format the body for JSON requests.

        Gets called by the endpoint methods before requesting.

        Args:
            endpoint: Name of the endpoint for filtering purposes.
            body: Additional arguments for the body.

        Returns:
            dict[str, Any]: JSON compatible formatted body.
        """
        body.update(
            {
                "active": self.active,
                "is_private": self.is_private,
            },
        )
        if self.name:
            body["name"] = self.name
        if self.client_id:
            body["client_id"] = self.client_id
        elif self.client_name:
            body["client_name"] = self.client_name

        if self.color:
            color = ProjectEndpoint.get_color(self.color) if self.color in ProjectEndpoint.BASIC_COLORS else self.color
            body["color"] = color

        if self.start_date and self._verify_endpoint_parameter(
            "start_date",
            endpoint,
        ):
            body["start_date"] = format_iso(self.start_date)
        if self.end_date and self._verify_endpoint_parameter(
            "end_date",
            endpoint,
        ):
            if self.start_date and self.end_date < self.start_date:
                log.warning(
                    "End date is before the start date. Ignoring end date...",
                )
            else:
                body["end_date"] = format_iso(self.end_date)

        if endpoint == "collect":
            self._format_collect(body)

        return body

name class-attribute instance-attribute

name: str | None = field(default=None)

Name of the project. Defaults to None. Will be required if its a POST request.

active class-attribute instance-attribute

active: bool | Literal['both'] = field(default=True)

Whether the project is archived or active. The literal 'both' is used for querying.

is_private class-attribute instance-attribute

is_private: bool | None = field(
    default=True, metadata={"endpoints": frozenset(("edit", "add"))}
)

Whether the project is private or not. Defaults to True.

client_name class-attribute instance-attribute

client_name: str | None = field(
    default=None, metadata={"endpoints": frozenset(("edit", "add"))}
)

Client name if client_id is not set. Defaults to None. If client_id is set the client_name will be ignored.

color class-attribute instance-attribute

color: str | None = field(
    default=None, metadata={"endpoints": frozenset(("edit", "add"))}
)

Color of the project. Refer to BASIC_COLORS for accepted colors for non-premium users.

start_date class-attribute instance-attribute

start_date: date | None = field(
    default=None, metadata={"endpoints": frozenset(("edit", "add"))}
)

Date to set the start of a project. If not set or start date is after the end date the end date will be ignored.

end_date class-attribute instance-attribute

end_date: date | None = field(
    default=None, metadata={"endpoints": frozenset(("edit", "add"))}
)

Date to set the end of the project. If not set or start date is after the end date the end date will be ignored.

since class-attribute instance-attribute

since: date | int | None = field(
    default=None, metadata={"endpoints": frozenset(("collect",))}
)

Timestamp for querying for projects with the 'collect' endpoint. Retrieve projects created/modified/deleted since this date using UNIX timestamp. If using local cache deleted projects are not present.

user_ids class-attribute instance-attribute

user_ids: list[int] = field(
    default_factory=list, metadata={"endpoints": frozenset(("collect",))}
)

Query for specific projects with assocciated users. API only.

client_ids class-attribute instance-attribute

client_ids: list[int] = field(
    default_factory=list, metadata={"endpoints": frozenset(("collect",))}
)

Query for specific projects with assocciated clients.

group_ids class-attribute instance-attribute

group_ids: list[int] = field(
    default_factory=list, metadata={"endpoints": frozenset(("collect",))}
)

Query for specific projects with assocciated groups. API only

statuses class-attribute instance-attribute

statuses: list[Status] = field(
    default_factory=list, metadata={"endpoints": frozenset(("collect",))}
)

Query for specific statuses when using the collect endpoint. Deleted status only works with the remote API.

format

format(endpoint: str, **body: Any) -> dict[str, Any]

Format the body for JSON requests.

Gets called by the endpoint methods before requesting.

PARAMETER DESCRIPTION
endpoint

Name of the endpoint for filtering purposes.

TYPE: str

body

Additional arguments for the body.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
dict[str, Any]

dict[str, Any]: JSON compatible formatted body.

Source code in src/toggl_api/_project.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def format(self, endpoint: str, **body: Any) -> dict[str, Any]:
    """Format the body for JSON requests.

    Gets called by the endpoint methods before requesting.

    Args:
        endpoint: Name of the endpoint for filtering purposes.
        body: Additional arguments for the body.

    Returns:
        dict[str, Any]: JSON compatible formatted body.
    """
    body.update(
        {
            "active": self.active,
            "is_private": self.is_private,
        },
    )
    if self.name:
        body["name"] = self.name
    if self.client_id:
        body["client_id"] = self.client_id
    elif self.client_name:
        body["client_name"] = self.client_name

    if self.color:
        color = ProjectEndpoint.get_color(self.color) if self.color in ProjectEndpoint.BASIC_COLORS else self.color
        body["color"] = color

    if self.start_date and self._verify_endpoint_parameter(
        "start_date",
        endpoint,
    ):
        body["start_date"] = format_iso(self.start_date)
    if self.end_date and self._verify_endpoint_parameter(
        "end_date",
        endpoint,
    ):
        if self.start_date and self.end_date < self.start_date:
            log.warning(
                "End date is before the start date. Ignoring end date...",
            )
        else:
            body["end_date"] = format_iso(self.end_date)

    if endpoint == "collect":
        self._format_collect(body)

    return body

toggl_api.ProjectEndpoint

Bases: TogglCachedEndpoint[TogglProject]

Specific endpoints for retrieving and modifying projects.

Official Documentation

Examples:

>>> from toggl_api.utility import get_authentication, retrieve_workspace_id
>>> from toggl_api import JSONCache
>>> project_endpoint = ProjectEndpoint(retrieve_workspace_id(), get_authentication(), JSONCache(...))
>>> project_endpoint.get(213141424)
TogglProject(213141424, "Amaryllis", ...)
>>> project_endpoint.delete(213141424)
None
PARAMETER DESCRIPTION
workspace_id

The workspace the projects belong to.

TYPE: int | TogglWorkspace

auth

Basic authentication with an api token or username/password combo.

TYPE: BasicAuth

cache

Cache to push the projects to.

TYPE: TogglCache[TogglProject] | None DEFAULT: None

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

ATTRIBUTE DESCRIPTION
BASIC_COLORS

Default colors that are available for non-premium users.

TYPE: Final[dict[str, str]]

METHOD DESCRIPTION
collect

Return all cached or remote projects.

get

Request a project based on its id.

delete

Delete a project based on its id.

edit

Edit a project based on its id with the parameters provided in the body.

add

Create a new project based on the parameters provided in the body.

get_color

Get a color by name. Defaults to gray.

get_color_id

Get a color id by name.

status_to_query

Create a list of queries depending on the desired project status.

Source code in src/toggl_api/_project.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
class ProjectEndpoint(TogglCachedEndpoint[TogglProject]):
    """Specific endpoints for retrieving and modifying projects.

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

    Examples:
        >>> from toggl_api.utility import get_authentication, retrieve_workspace_id
        >>> from toggl_api import JSONCache
        >>> project_endpoint = ProjectEndpoint(retrieve_workspace_id(), get_authentication(), JSONCache(...))
        >>> project_endpoint.get(213141424)
        TogglProject(213141424, "Amaryllis", ...)

        >>> project_endpoint.delete(213141424)
        None

    Params:
        workspace_id: The workspace the projects belong to.
        auth: Basic authentication with an api token or username/password combo.
        cache: Cache to push the projects to.
        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.

    Attributes:
        BASIC_COLORS: Default colors that are available for non-premium users.
    """

    MODEL = TogglProject

    BASIC_COLORS: Final[dict[str, str]] = {
        "blue": "#0b83d9",
        "violet": "#9e5bd9",
        "pink": "#d94182",
        "orange": "#e36a00",
        "gold": "#bf7000",
        "green": "#2da608",
        "teal": "#06a893",
        "beige": "#c9806b",
        "dark-blue": "#465bb3",
        "purple": "#990099",
        "yellow": "#c7af14",
        "dark-green": "#566614",
        "red": "#d92b2b",
        "gray": "#525266",
    }
    """Basic colors available for projects in order of the API index."""

    def __init__(
        self,
        workspace_id: int | TogglWorkspace,
        auth: BasicAuth,
        cache: TogglCache[TogglProject] | None = None,
        *,
        client: Client | None = None,
        timeout: Timeout | int = 10,
        re_raise: bool = False,
        retries: int = 3,
    ) -> None:
        super().__init__(
            auth,
            cache,
            client=client,
            timeout=timeout,
            re_raise=re_raise,
            retries=retries,
        )
        self.workspace_id = workspace_id if isinstance(workspace_id, int) else workspace_id.id

    @staticmethod
    def status_to_query(status: TogglProject.Status) -> list[TogglQuery[Any]]:
        """Create a list of queries depending on the desired project status.

        Args:
            status: What is the status you are querying for?

        Raises:
            NotImplementedError: Active & Deleted Statuses are currently not
                supported for local querying.

        Returns:
            A list of query parameters for the desired status.
        """
        if status == TogglProject.Status.ARCHIVED:
            return [TogglQuery("active", value=False)]

        now = datetime.now(timezone.utc)
        if status == TogglProject.Status.UPCOMING:
            return [TogglQuery("start_date", now, Comparison.LESS_THEN)]

        if status == TogglProject.Status.ENDED:
            return [TogglQuery("end_date", now, Comparison.GREATER_THEN)]

        msg = f"{status} status is not supported by local cache queries!"
        raise NotImplementedError(msg)

    def _collect_cache(self, body: ProjectBody | None) -> list[TogglProject]:
        if body:
            queries: list[TogglQuery[Any]] = []
            if isinstance(body.active, bool):
                queries.append(
                    TogglQuery("active", body.active, Comparison.EQUAL),
                )
            if body.since:
                queries.append(
                    TogglQuery(
                        "timestamp",
                        body.since,
                        Comparison.GREATER_THEN_OR_EQUAL,
                    ),
                )
            if body.client_ids:
                queries.append(TogglQuery("client", body.client_ids))
            if body.statuses:
                for status in body.statuses:
                    queries += self.status_to_query(status)

            return list(self.query(*queries))

        return list(self.load_cache())

    def collect(
        self,
        body: ProjectBody | None = None,
        *,
        refresh: bool = False,
        sort_pinned: bool = False,
        only_me: bool = False,
        only_templates: bool = False,
    ) -> list[TogglProject]:
        """Return all cached or remote projects.

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

        Args:
            body: Optional body for adding query parameters for filtering projects.
            refresh: Whether to fetch from the remote API if true else using
                the local cache.
            sort_pinned: Whether to put pinned projects ontop of the results.
                Only works with the remote API at the moment.
            only_me: Only retrieve projects that are assigned to the current
                user assocciated with the authentication. API specific.
            only_templates: Retrieve template projects. API specific.

        Raises:
            HTTPStatusError: If any response that is not '200' code is returned.
            NotImplementedError: Deleted or Active status are used with a 'False'
                refresh argument.

        Returns:
            A list of projects or an empty list if None are found.
        """
        if not refresh:
            return self._collect_cache(body)

        return cast(
            "list[TogglProject]",
            self.request(
                self.endpoint,
                body=body.format(
                    "collect",
                    workspace_id=self.workspace_id,
                    sort_pinned=sort_pinned,
                    only_me=only_me,
                    only_templates=only_templates,
                )
                if body
                else {
                    "sort_pinned": sort_pinned,
                    "only_me": only_me,
                    "only_templates": only_templates,
                },
                refresh=refresh,
            ),
        )

    def get(
        self,
        project_id: int | TogglProject,
        *,
        refresh: bool = False,
    ) -> TogglProject | None:
        """Request a project based on its id.

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

        Examples:
            >>> project_endpoint.get(213141424)
            TogglProject(213141424, "Amaryllis", ...)

        Args:
            project_id: TogglProject to retrieve. Either a model with the correct id or integer.
            refresh: Whether to check cache or not.

        Raises:
            HTTPStatusError: If any status code that is not '200' or a '404' is returned.

        Returns:
            A project model or None if nothing was found.
        """
        if isinstance(project_id, TogglProject):
            project_id = project_id.id

        if self.cache and not refresh:
            return self.cache.find({"id": project_id})

        try:
            response = self.request(
                f"{self.endpoint}/{project_id}",
                refresh=refresh,
            )
        except HTTPStatusError as err:
            if not self.re_raise and err.response.status_code == codes.NOT_FOUND:
                log.warning("Project with id %s was not found!", project_id)
                return None
            raise

        return cast("TogglProject", response) or None

    def delete(self, project: TogglProject | int) -> None:
        """Delete a project based on its id.

        This endpoint always hits the external API in order to keep projects consistent.

        [Official Documentation](https://engineering.toggl.com/docs/api/projects#delete-workspaceproject)

        Examples:
            >>> project_endpoint.delete(213141424)
            None

        Args:
            project: TogglProject to delete. Either an existing model or the integer id.

        Raises:
            HTTPStatusError: For anything that's not a '200' or '404' status code.
        """
        project_id = project if isinstance(project, int) else project.id
        try:
            self.request(
                f"{self.endpoint}/{project_id}",
                method=RequestMethod.DELETE,
                refresh=True,
            )
        except HTTPStatusError as err:
            if self.re_raise or err.response.status_code != codes.NOT_FOUND:
                raise
            log.warning(
                "Project with id %s was either already deleted or did not exist in the first place!",
                project_id,
            )
        if self.cache is None:
            return
        if isinstance(project, int):
            proj = self.cache.find({"id": project})
            if not isinstance(proj, TogglProject):
                return
            project = proj

        self.cache.delete(project)
        self.cache.commit()

    def edit(
        self,
        project: TogglProject | int,
        body: ProjectBody,
    ) -> TogglProject:
        """Edit a project based on its id with the parameters provided in the body.

        This endpoint always hit the external API in order to keep projects consistent.

        [Official Documentation](https://engineering.toggl.com/docs/api/projects#put-workspaceproject)

        Examples:
            >>> body = ProjectBody(name="Amaryllis")
            >>> project_endpoint.add(213141424, body)
            TogglProject(213141424, "Amaryllis", client=87695895, ...)

        Args:
            project: The existing project to edit. Either the model or the integer id.
            body: The body with the edited attributes.

        Raises:
            HTTPStatusError: For anything that's not a 'ok' status code.

        Returns:
            The project model with the provided modifications.
        """
        if isinstance(project, TogglProject):
            project = project.id

        return cast(
            "TogglProject",
            self.request(
                f"{self.endpoint}/{project}",
                method=RequestMethod.PUT,
                body=body.format("edit", workspace_id=self.workspace_id),
                refresh=True,
            ),
        )

    def add(self, body: ProjectBody) -> TogglProject:
        """Create a new project based on the parameters provided in the body.

        This endpoint always hit the external API in order to keep projects consistent.

        [Official Documentation](https://engineering.toggl.com/docs/api/projects#post-workspaceprojects)

        Examples:
            >>> body = ProjectBody(name="Zinnia", client_id=87695895)
            >>> project_endpoint.add(body)
            TogglProject(213141424, "Zinnia", client=87695895, ...)

        Args:
            body: The body with the new attributes of the project.

        Raises:
            HTTPStatusError: For anything that's not a 'ok' status code.
            NamingError: If the new project name is invalid.

        Returns:
            The newly created project.
        """
        if body.name is None:
            msg = "Name must be set in order to create a project!"
            raise NamingError(msg)

        return cast(
            "TogglProject",
            self.request(
                self.endpoint,
                method=RequestMethod.POST,
                body=body.format("add", workspace_id=self.workspace_id),
                refresh=True,
            ),
        )

    @classmethod
    def get_color(cls, name: str) -> str:
        """Get a color by name. Defaults to gray.

        Args:
            name: The name of the color.

        Returns:
            Color in a hexcode.
        """
        return cls.BASIC_COLORS.get(name, "#525266")

    @classmethod
    def get_color_id(cls, color: str) -> int:
        """Get a color id by name.

        Args:
            color: Name of the desired color.

        Raises:
            IndexError: If the color name is not a standard color.

        Returns:
            Index of the provided color name.
        """
        colors = list(cls.BASIC_COLORS.values())
        return colors.index(color)

    @property
    def endpoint(self) -> str:
        return f"workspaces/{self.workspace_id}/projects"

BASIC_COLORS class-attribute instance-attribute

BASIC_COLORS: Final[dict[str, str]] = {
    "blue": "#0b83d9",
    "violet": "#9e5bd9",
    "pink": "#d94182",
    "orange": "#e36a00",
    "gold": "#bf7000",
    "green": "#2da608",
    "teal": "#06a893",
    "beige": "#c9806b",
    "dark-blue": "#465bb3",
    "purple": "#990099",
    "yellow": "#c7af14",
    "dark-green": "#566614",
    "red": "#d92b2b",
    "gray": "#525266",
}

Basic colors available for projects in order of the API index.

collect

collect(
    body: ProjectBody | None = None,
    *,
    refresh: bool = False,
    sort_pinned: bool = False,
    only_me: bool = False,
    only_templates: bool = False,
) -> list[TogglProject]

Return all cached or remote projects.

Official Documentation

PARAMETER DESCRIPTION
body

Optional body for adding query parameters for filtering projects.

TYPE: ProjectBody | None DEFAULT: None

refresh

Whether to fetch from the remote API if true else using the local cache.

TYPE: bool DEFAULT: False

sort_pinned

Whether to put pinned projects ontop of the results. Only works with the remote API at the moment.

TYPE: bool DEFAULT: False

only_me

Only retrieve projects that are assigned to the current user assocciated with the authentication. API specific.

TYPE: bool DEFAULT: False

only_templates

Retrieve template projects. API specific.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
HTTPStatusError

If any response that is not '200' code is returned.

NotImplementedError

Deleted or Active status are used with a 'False' refresh argument.

RETURNS DESCRIPTION
list[TogglProject]

A list of projects or an empty list if None are found.

Source code in src/toggl_api/_project.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
def collect(
    self,
    body: ProjectBody | None = None,
    *,
    refresh: bool = False,
    sort_pinned: bool = False,
    only_me: bool = False,
    only_templates: bool = False,
) -> list[TogglProject]:
    """Return all cached or remote projects.

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

    Args:
        body: Optional body for adding query parameters for filtering projects.
        refresh: Whether to fetch from the remote API if true else using
            the local cache.
        sort_pinned: Whether to put pinned projects ontop of the results.
            Only works with the remote API at the moment.
        only_me: Only retrieve projects that are assigned to the current
            user assocciated with the authentication. API specific.
        only_templates: Retrieve template projects. API specific.

    Raises:
        HTTPStatusError: If any response that is not '200' code is returned.
        NotImplementedError: Deleted or Active status are used with a 'False'
            refresh argument.

    Returns:
        A list of projects or an empty list if None are found.
    """
    if not refresh:
        return self._collect_cache(body)

    return cast(
        "list[TogglProject]",
        self.request(
            self.endpoint,
            body=body.format(
                "collect",
                workspace_id=self.workspace_id,
                sort_pinned=sort_pinned,
                only_me=only_me,
                only_templates=only_templates,
            )
            if body
            else {
                "sort_pinned": sort_pinned,
                "only_me": only_me,
                "only_templates": only_templates,
            },
            refresh=refresh,
        ),
    )

get

get(
    project_id: int | TogglProject, *, refresh: bool = False
) -> TogglProject | None

Request a project based on its id.

Official Documentation

Examples:

>>> project_endpoint.get(213141424)
TogglProject(213141424, "Amaryllis", ...)
PARAMETER DESCRIPTION
project_id

TogglProject to retrieve. Either a model with the correct id or integer.

TYPE: int | TogglProject

refresh

Whether to check cache or not.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
HTTPStatusError

If any status code that is not '200' or a '404' is returned.

RETURNS DESCRIPTION
TogglProject | None

A project model or None if nothing was found.

Source code in src/toggl_api/_project.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
def get(
    self,
    project_id: int | TogglProject,
    *,
    refresh: bool = False,
) -> TogglProject | None:
    """Request a project based on its id.

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

    Examples:
        >>> project_endpoint.get(213141424)
        TogglProject(213141424, "Amaryllis", ...)

    Args:
        project_id: TogglProject to retrieve. Either a model with the correct id or integer.
        refresh: Whether to check cache or not.

    Raises:
        HTTPStatusError: If any status code that is not '200' or a '404' is returned.

    Returns:
        A project model or None if nothing was found.
    """
    if isinstance(project_id, TogglProject):
        project_id = project_id.id

    if self.cache and not refresh:
        return self.cache.find({"id": project_id})

    try:
        response = self.request(
            f"{self.endpoint}/{project_id}",
            refresh=refresh,
        )
    except HTTPStatusError as err:
        if not self.re_raise and err.response.status_code == codes.NOT_FOUND:
            log.warning("Project with id %s was not found!", project_id)
            return None
        raise

    return cast("TogglProject", response) or None

delete

delete(project: TogglProject | int) -> None

Delete a project based on its id.

This endpoint always hits the external API in order to keep projects consistent.

Official Documentation

Examples:

>>> project_endpoint.delete(213141424)
None
PARAMETER DESCRIPTION
project

TogglProject to delete. Either an existing model or the integer id.

TYPE: TogglProject | int

RAISES DESCRIPTION
HTTPStatusError

For anything that's not a '200' or '404' status code.

Source code in src/toggl_api/_project.py
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def delete(self, project: TogglProject | int) -> None:
    """Delete a project based on its id.

    This endpoint always hits the external API in order to keep projects consistent.

    [Official Documentation](https://engineering.toggl.com/docs/api/projects#delete-workspaceproject)

    Examples:
        >>> project_endpoint.delete(213141424)
        None

    Args:
        project: TogglProject to delete. Either an existing model or the integer id.

    Raises:
        HTTPStatusError: For anything that's not a '200' or '404' status code.
    """
    project_id = project if isinstance(project, int) else project.id
    try:
        self.request(
            f"{self.endpoint}/{project_id}",
            method=RequestMethod.DELETE,
            refresh=True,
        )
    except HTTPStatusError as err:
        if self.re_raise or err.response.status_code != codes.NOT_FOUND:
            raise
        log.warning(
            "Project with id %s was either already deleted or did not exist in the first place!",
            project_id,
        )
    if self.cache is None:
        return
    if isinstance(project, int):
        proj = self.cache.find({"id": project})
        if not isinstance(proj, TogglProject):
            return
        project = proj

    self.cache.delete(project)
    self.cache.commit()

edit

edit(project: TogglProject | int, body: ProjectBody) -> TogglProject

Edit a project based on its id with the parameters provided in the body.

This endpoint always hit the external API in order to keep projects consistent.

Official Documentation

Examples:

>>> body = ProjectBody(name="Amaryllis")
>>> project_endpoint.add(213141424, body)
TogglProject(213141424, "Amaryllis", client=87695895, ...)
PARAMETER DESCRIPTION
project

The existing project to edit. Either the model or the integer id.

TYPE: TogglProject | int

body

The body with the edited attributes.

TYPE: ProjectBody

RAISES DESCRIPTION
HTTPStatusError

For anything that's not a 'ok' status code.

RETURNS DESCRIPTION
TogglProject

The project model with the provided modifications.

Source code in src/toggl_api/_project.py
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
def edit(
    self,
    project: TogglProject | int,
    body: ProjectBody,
) -> TogglProject:
    """Edit a project based on its id with the parameters provided in the body.

    This endpoint always hit the external API in order to keep projects consistent.

    [Official Documentation](https://engineering.toggl.com/docs/api/projects#put-workspaceproject)

    Examples:
        >>> body = ProjectBody(name="Amaryllis")
        >>> project_endpoint.add(213141424, body)
        TogglProject(213141424, "Amaryllis", client=87695895, ...)

    Args:
        project: The existing project to edit. Either the model or the integer id.
        body: The body with the edited attributes.

    Raises:
        HTTPStatusError: For anything that's not a 'ok' status code.

    Returns:
        The project model with the provided modifications.
    """
    if isinstance(project, TogglProject):
        project = project.id

    return cast(
        "TogglProject",
        self.request(
            f"{self.endpoint}/{project}",
            method=RequestMethod.PUT,
            body=body.format("edit", workspace_id=self.workspace_id),
            refresh=True,
        ),
    )

add

add(body: ProjectBody) -> TogglProject

Create a new project based on the parameters provided in the body.

This endpoint always hit the external API in order to keep projects consistent.

Official Documentation

Examples:

>>> body = ProjectBody(name="Zinnia", client_id=87695895)
>>> project_endpoint.add(body)
TogglProject(213141424, "Zinnia", client=87695895, ...)
PARAMETER DESCRIPTION
body

The body with the new attributes of the project.

TYPE: ProjectBody

RAISES DESCRIPTION
HTTPStatusError

For anything that's not a 'ok' status code.

NamingError

If the new project name is invalid.

RETURNS DESCRIPTION
TogglProject

The newly created project.

Source code in src/toggl_api/_project.py
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
def add(self, body: ProjectBody) -> TogglProject:
    """Create a new project based on the parameters provided in the body.

    This endpoint always hit the external API in order to keep projects consistent.

    [Official Documentation](https://engineering.toggl.com/docs/api/projects#post-workspaceprojects)

    Examples:
        >>> body = ProjectBody(name="Zinnia", client_id=87695895)
        >>> project_endpoint.add(body)
        TogglProject(213141424, "Zinnia", client=87695895, ...)

    Args:
        body: The body with the new attributes of the project.

    Raises:
        HTTPStatusError: For anything that's not a 'ok' status code.
        NamingError: If the new project name is invalid.

    Returns:
        The newly created project.
    """
    if body.name is None:
        msg = "Name must be set in order to create a project!"
        raise NamingError(msg)

    return cast(
        "TogglProject",
        self.request(
            self.endpoint,
            method=RequestMethod.POST,
            body=body.format("add", workspace_id=self.workspace_id),
            refresh=True,
        ),
    )

get_color classmethod

get_color(name: str) -> str

Get a color by name. Defaults to gray.

PARAMETER DESCRIPTION
name

The name of the color.

TYPE: str

RETURNS DESCRIPTION
str

Color in a hexcode.

Source code in src/toggl_api/_project.py
515
516
517
518
519
520
521
522
523
524
525
@classmethod
def get_color(cls, name: str) -> str:
    """Get a color by name. Defaults to gray.

    Args:
        name: The name of the color.

    Returns:
        Color in a hexcode.
    """
    return cls.BASIC_COLORS.get(name, "#525266")

get_color_id classmethod

get_color_id(color: str) -> int

Get a color id by name.

PARAMETER DESCRIPTION
color

Name of the desired color.

TYPE: str

RAISES DESCRIPTION
IndexError

If the color name is not a standard color.

RETURNS DESCRIPTION
int

Index of the provided color name.

Source code in src/toggl_api/_project.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
@classmethod
def get_color_id(cls, color: str) -> int:
    """Get a color id by name.

    Args:
        color: Name of the desired color.

    Raises:
        IndexError: If the color name is not a standard color.

    Returns:
        Index of the provided color name.
    """
    colors = list(cls.BASIC_COLORS.values())
    return colors.index(color)

status_to_query staticmethod

status_to_query(status: Status) -> list[TogglQuery[Any]]

Create a list of queries depending on the desired project status.

PARAMETER DESCRIPTION
status

What is the status you are querying for?

TYPE: Status

RAISES DESCRIPTION
NotImplementedError

Active & Deleted Statuses are currently not supported for local querying.

RETURNS DESCRIPTION
list[TogglQuery[Any]]

A list of query parameters for the desired status.

Source code in src/toggl_api/_project.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
@staticmethod
def status_to_query(status: TogglProject.Status) -> list[TogglQuery[Any]]:
    """Create a list of queries depending on the desired project status.

    Args:
        status: What is the status you are querying for?

    Raises:
        NotImplementedError: Active & Deleted Statuses are currently not
            supported for local querying.

    Returns:
        A list of query parameters for the desired status.
    """
    if status == TogglProject.Status.ARCHIVED:
        return [TogglQuery("active", value=False)]

    now = datetime.now(timezone.utc)
    if status == TogglProject.Status.UPCOMING:
        return [TogglQuery("start_date", now, Comparison.LESS_THEN)]

    if status == TogglProject.Status.ENDED:
        return [TogglQuery("end_date", now, Comparison.GREATER_THEN)]

    msg = f"{status} status is not supported by local cache queries!"
    raise NotImplementedError(msg)

toggl_api.asyncio.AsyncProjectEndpoint

Bases: TogglAsyncCachedEndpoint[TogglProject]

Specific endpoints for retrieving and modifying projects.

Official Documentation

Examples:

>>> from toggl_api.utility import get_authentication, retrieve_workspace_id
>>> from toggl_api.asyncio import AsyncSqliteCache, ProjectEndpoint
>>> project_endpoint = ProjectEndpoint(retrieve_workspace_id(), get_authentication(), AsyncSqliteCache(...))
>>> await project_endpoint.get(213141424)
TogglProject(213141424, "Amaryllis", ...)
>>> await project_endpoint.delete(213141424)
None
PARAMETER DESCRIPTION
workspace_id

The workspace the projects belong to.

TYPE: int | TogglWorkspace

auth

Basic authentication with an api token or username/password combo.

TYPE: BasicAuth

cache

Cache to push the projects to.

TYPE: AsyncSqliteCache[TogglProject] | None DEFAULT: None

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

ATTRIBUTE DESCRIPTION
BASIC_COLORS

Default colors that are available for non-premium users.

TYPE: Final[dict[str, str]]

METHOD DESCRIPTION
status_to_query

Create a list of queries depending on the desired project status.

collect

Return all cached or remote projects.

get

Request a project based on its id.

delete

Delete a project based on its id.

edit

Edit a project based on its id with the parameters provided in the body.

add

Create a new project based on the parameters provided in the body.

get_color

Get a color by name. Defaults to gray.

get_color_id

Get a color id by name.

BASIC_COLORS class-attribute instance-attribute

BASIC_COLORS: Final[dict[str, str]] = {
    "blue": "#0b83d9",
    "violet": "#9e5bd9",
    "pink": "#d94182",
    "orange": "#e36a00",
    "gold": "#bf7000",
    "green": "#2da608",
    "teal": "#06a893",
    "beige": "#c9806b",
    "dark-blue": "#465bb3",
    "purple": "#990099",
    "yellow": "#c7af14",
    "dark-green": "#566614",
    "red": "#d92b2b",
    "gray": "#525266",
}

Basic colors available for projects in order of the API index.

status_to_query staticmethod

status_to_query(status: Status, statement: Select[Any]) -> Select[Any]

Create a list of queries depending on the desired project status.

PARAMETER DESCRIPTION
status

What is the status you are querying for?

TYPE: Status

statement

Base statement to add filters onto.

TYPE: Select[Any]

RAISES DESCRIPTION
NotImplementedError

Active & Deleted Statuses are currently not supported for local querying.

RETURNS DESCRIPTION
Select[Any]

A list of query parameters for the desired status.

collect async

collect(
    body: ProjectBody | None = None,
    *,
    refresh: bool = False,
    sort_pinned: bool = False,
    only_me: bool = False,
    only_templates: bool = False,
) -> list[TogglProject]

Return all cached or remote projects.

Official Documentation

PARAMETER DESCRIPTION
body

Optional body for adding query parameters for filtering projects.

TYPE: ProjectBody | None DEFAULT: None

refresh

Whether to fetch from the remote API if true else using the local cache.

TYPE: bool DEFAULT: False

sort_pinned

Whether to put pinned projects ontop of the results. Only works with the remote API at the moment.

TYPE: bool DEFAULT: False

only_me

Only retrieve projects that are assigned to the current user assocciated with the authentication. API specific.

TYPE: bool DEFAULT: False

only_templates

Retrieve template projects. API specific.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
HTTPStatusError

If any response that is not '200' code is returned.

NotImplementedError

Deleted or Active status are used with a 'False' refresh argument.

RETURNS DESCRIPTION
list[TogglProject]

A list of projects or an empty list if None are found.

get async

get(
    project_id: int | TogglProject, *, refresh: bool = False
) -> TogglProject | None

Request a project based on its id.

Official Documentation

Examples:

>>> await project_endpoint.get(213141424)
TogglProject(213141424, "Amaryllis", ...)
PARAMETER DESCRIPTION
project_id

TogglProject to retrieve. Either a model with the correct id or integer.

TYPE: int | TogglProject

refresh

Whether to check cache or not.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
HTTPStatusError

If any status code that is not '200' or a '404' is returned.

RETURNS DESCRIPTION
TogglProject | None

A project model or None if nothing was found.

delete async

delete(project: TogglProject | int) -> None

Delete a project based on its id.

This endpoint always hits the external API in order to keep projects consistent.

Official Documentation

Examples:

>>> project_endpoint.delete(213141424)
None
PARAMETER DESCRIPTION
project

TogglProject to delete. Either an existing model or the integer id.

TYPE: TogglProject | int

RAISES DESCRIPTION
HTTPStatusError

For anything that's not a '200' or '404' status code.

edit async

edit(project: TogglProject | int, body: ProjectBody) -> TogglProject

Edit a project based on its id with the parameters provided in the body.

This endpoint always hit the external API in order to keep projects consistent.

Official Documentation

Examples:

>>> body = ProjectBody(name="Amaryllis")
>>> project_endpoint.add(213141424, body)
TogglProject(213141424, "Amaryllis", client=87695895, ...)
PARAMETER DESCRIPTION
project

The existing project to edit. Either the model or the integer id.

TYPE: TogglProject | int

body

The body with the edited attributes.

TYPE: ProjectBody

RAISES DESCRIPTION
HTTPStatusError

For anything that's not a 'ok' status code.

RETURNS DESCRIPTION
TogglProject

The project model with the provided modifications.

add async

add(body: ProjectBody) -> TogglProject

Create a new project based on the parameters provided in the body.

This endpoint always hit the external API in order to keep projects consistent.

Official Documentation

Examples:

>>> body = ProjectBody(name="Zinnia", client_id=87695895)
>>> project_endpoint.add(body)
TogglProject(213141424, "Zinnia", client=87695895, ...)
PARAMETER DESCRIPTION
body

The body with the new attributes of the project.

TYPE: ProjectBody

RAISES DESCRIPTION
HTTPStatusError

For anything that's not a 'ok' status code.

NamingError

If the project name is invalid.

RETURNS DESCRIPTION
TogglProject

The newly created project.

get_color classmethod

get_color(name: str) -> str

Get a color by name. Defaults to gray.

PARAMETER DESCRIPTION
name

The name of the color.

TYPE: str

RETURNS DESCRIPTION
str

Color in a hexcode.

get_color_id classmethod

get_color_id(color: str) -> int

Get a color id by name.

PARAMETER DESCRIPTION
color

Name of the desired color.

TYPE: str

RAISES DESCRIPTION
IndexError

If the color name is not a standard color.

RETURNS DESCRIPTION
int

Index of the provided color name.