Skip to content

Projects

toggl_api.ProjectBody dataclass

Bases: BaseBody

JSON body dataclass for PUT, POST & PATCH requests.

Source code in 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
@dataclass
class ProjectBody(BaseBody):
    """JSON body dataclass for PUT, POST & PATCH requests."""

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

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

    client_id: Optional[int] = field(
        default=None,
        metadata={"endpoints": frozenset(("edit", "add"))},
    )
    client_name: Optional[str] = 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: Optional[str] = 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: Optional[date] = 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: Optional[date] = 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: Optional[date | int] = 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]:
        """Formats 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: Optional[str] = field(default=None) class-attribute instance-attribute

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

active: bool | Literal['both'] = field(default='both') class-attribute instance-attribute

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

is_private: Optional[bool] = field(default=True, metadata={'endpoints': frozenset(('edit', 'add'))}) class-attribute instance-attribute

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

client_name: Optional[str] = field(default=None, metadata={'endpoints': frozenset(('edit', 'add'))}) class-attribute instance-attribute

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

color: Optional[str] = field(default=None, metadata={'endpoints': frozenset(('edit', 'add'))}) class-attribute instance-attribute

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

start_date: Optional[date] = field(default=None, metadata={'endpoints': frozenset(('edit', 'add'))}) class-attribute instance-attribute

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: Optional[date] = field(default=None, metadata={'endpoints': frozenset(('edit', 'add'))}) class-attribute instance-attribute

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: Optional[date | int] = field(default=None, metadata={'endpoints': frozenset(('collect'))}) class-attribute instance-attribute

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'))}) class-attribute instance-attribute

Query for specific projects with assocciated users. API only.

client_ids: list[int] = field(default_factory=list, metadata={'endpoints': frozenset(('collect'))}) class-attribute instance-attribute

Query for specific projects with assocciated clients.

group_ids: list[int] = field(default_factory=list, metadata={'endpoints': frozenset(('collect'))}) class-attribute instance-attribute

Query for specific projects with assocciated groups. API only

statuses: list[TogglProject.Status] = field(default_factory=list, metadata={'endpoints': frozenset(('collect'))}) class-attribute instance-attribute

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

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

Formats the body for JSON requests.

Gets called by the endpoint methods before requesting.

Parameters:

  • endpoint (str) –

    Name of the endpoint for filtering purposes.

  • body (Any, default: {} ) –

    Additional arguments for the body.

Returns:

  • dict[str, Any]

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

Source code in 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
def format(self, endpoint: str, **body: Any) -> dict[str, Any]:
    """Formats 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

Parameters:

  • workspace_id (int | TogglWorkspace) –

    The workspace the projects belong to.

  • auth (BasicAuth) –

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

  • cache (TogglCache[TogglProject]) –

    Cache to push the projects to.

  • timeout (int, default: 10 ) –

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

  • re_raise (bool, default: False ) –

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

  • retries (int, default: 3 ) –

    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 (Final[dict[str, str]]) –

    Default colors that are available for non-premium users.

Source code in toggl_api/project.py
167
168
169
170
171
172
173
174
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
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.
        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.
    """

    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],
        *,
        timeout: int = 10,
        re_raise: bool = False,
        retries: int = 3,
    ) -> None:
        super().__init__(
            0,
            auth,
            cache,
            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]:
        """Creates 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] = []
            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: Optional[ProjectBody] = None,
        *,
        refresh: bool = False,
        sort_pinned: bool = False,
        only_me: bool = False,
        only_templates: bool = False,
    ) -> list[TogglProject]:
        """Returns 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 self.request(
            "",
            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 not refresh:
            return self.cache.find_entry({"id": project_id})

        try:
            response = self.request(
                f"/{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 response or None

    def delete(self, project: TogglProject | int) -> None:
        """Deletes 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"/{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 isinstance(project, int):
            proj = self.cache.find_entry({"id": project})
            if not isinstance(proj, TogglProject):
                return
            project = proj

        self.cache.delete_entries(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 self.request(
            f"/{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.

        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 self.request(
            "",
            method=RequestMethod.POST,
            body=body.format("add", workspace_id=self.workspace_id),
            refresh=True,
        )

    @classmethod
    @_re_kwarg({"color": "name"})
    def get_color(cls, name: str) -> str:
        """Get a color by name. Defaults to gray."""
        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"

    @property
    def model(self) -> type[TogglProject]:
        return 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'} class-attribute instance-attribute

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

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

Returns all cached or remote projects.

Official Documentation

Parameters:

  • body (Optional[ProjectBody], default: None ) –

    Optional body for adding query parameters for filtering projects.

  • refresh (bool, default: False ) –

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

  • sort_pinned (bool, default: False ) –

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

  • only_me (bool, default: False ) –

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

  • only_templates (bool, default: False ) –

    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:

  • list[TogglProject]

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

Source code in toggl_api/project.py
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
def collect(
    self,
    body: Optional[ProjectBody] = None,
    *,
    refresh: bool = False,
    sort_pinned: bool = False,
    only_me: bool = False,
    only_templates: bool = False,
) -> list[TogglProject]:
    """Returns 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 self.request(
        "",
        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(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", ...)

Parameters:

  • project_id (int | TogglProject) –

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

  • refresh (bool, default: False ) –

    Whether to check cache or not.

Raises:

  • HTTPStatusError

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

Returns:

  • TogglProject | None

    A project model or None if nothing was found.

Source code in toggl_api/project.py
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
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 not refresh:
        return self.cache.find_entry({"id": project_id})

    try:
        response = self.request(
            f"/{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 response or None

delete(project: TogglProject | int) -> None

Deletes 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

Parameters:

  • project (TogglProject | int) –

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

Raises:

  • HTTPStatusError

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

Source code in toggl_api/project.py
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
def delete(self, project: TogglProject | int) -> None:
    """Deletes 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"/{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 isinstance(project, int):
        proj = self.cache.find_entry({"id": project})
        if not isinstance(proj, TogglProject):
            return
        project = proj

    self.cache.delete_entries(project)
    self.cache.commit()

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, ...)

Parameters:

  • project (TogglProject | int) –

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

  • body (ProjectBody) –

    The body with the edited attributes.

Raises:

  • HTTPStatusError

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

Returns:

  • TogglProject

    The project model with the provided modifications.

Source code in toggl_api/project.py
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
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 self.request(
        f"/{project}",
        method=RequestMethod.PUT,
        body=body.format("edit", workspace_id=self.workspace_id),
        refresh=True,
    )

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, ...)

Parameters:

  • body (ProjectBody) –

    The body with the new attributes of the project.

Raises:

  • HTTPStatusError

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

Returns:

Source code in toggl_api/project.py
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
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.

    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 self.request(
        "",
        method=RequestMethod.POST,
        body=body.format("add", workspace_id=self.workspace_id),
        refresh=True,
    )

get_color(name: str) -> str classmethod

Get a color by name. Defaults to gray.

Source code in toggl_api/project.py
476
477
478
479
480
@classmethod
@_re_kwarg({"color": "name"})
def get_color(cls, name: str) -> str:
    """Get a color by name. Defaults to gray."""
    return cls.BASIC_COLORS.get(name, "#525266")

get_color_id(color: str) -> int classmethod

Get a color id by name.

Parameters:

  • color (str) –

    Name of the desired color.

Raises:

  • IndexError

    If the color name is not a standard color.

Returns:

  • int

    Index of the provided color name.

Source code in toggl_api/project.py
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
@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(status: TogglProject.Status) -> list[TogglQuery] staticmethod

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

Parameters:

  • status (Status) –

    What is the status you are querying for?

Raises:

  • NotImplementedError

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

Returns:

  • list[TogglQuery]

    A list of query parameters for the desired status.

Source code in toggl_api/project.py
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
@staticmethod
def status_to_query(status: TogglProject.Status) -> list[TogglQuery]:
    """Creates 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)