Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Project Endpoints #1390

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cognite/client/_api/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing_extensions import TypeAlias

from cognite.client._api.projects import ProjectsAPI
from cognite.client._api.user_profiles import UserProfilesAPI
from cognite.client._api_client import APIClient
from cognite.client._constants import DEFAULT_LIMIT_READ
Expand Down Expand Up @@ -89,6 +90,7 @@ def _convert_capability_to_tuples(capabilities: ComparableCapability, project: s
class IAMAPI(APIClient):
def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: CogniteClient) -> None:
super().__init__(config, api_version, cognite_client)
self.projects = ProjectsAPI(config, api_version, cognite_client)
self.groups = GroupsAPI(config, api_version, cognite_client)
self.security_categories = SecurityCategoriesAPI(config, api_version, cognite_client)
self.sessions = SessionsAPI(config, api_version, cognite_client)
Expand Down
99 changes: 99 additions & 0 deletions cognite/client/_api/projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from __future__ import annotations

from typing import Sequence, overload
from urllib.parse import quote

from cognite.client._api_client import APIClient
from cognite.client.data_classes import Project, ProjectList, ProjectUpdate, ProjectURLNameList, ProjectWrite


class ProjectsAPI(APIClient):
_RESOURCE_PATH = "/projects"

@overload
def create(self, item: ProjectWrite) -> Project:
...

@overload
def create(self, item: Sequence[ProjectWrite]) -> ProjectList:
...

def create(self, item: ProjectWrite | Sequence[ProjectWrite]) -> Project | ProjectList:
"""`Create a project <https://developer.cognite.com/api#tag/Projects/operation/createProject>`_

Args:
item (ProjectWrite | Sequence[ProjectWrite]): Project(s) to create

Returns:
Project | ProjectList: Created project(s)

Examples:
Create a new project with the name "my project"

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import ProjectWrite
>>> c = CogniteClient()
>>> project = ProjectWrite(name="my project", url_name="my-project", parent_project_url_name="root")
>>> res = c.iam.projects.create(project)
"""
return self._create_multiple(item, list_cls=ProjectList, resource_cls=Project, input_resource_cls=ProjectWrite)

def retrieve(self, project: str) -> Project:
"""`Retrieve a project <https://developer.cognite.com/api#tag/Projects/operation/getProject>`_

Args:
project (str): Project to retrieve

Returns:
Project: The requested project

Examples:

Retrieve the project with the name "publicdata"

>>> from cognite.client import CogniteClient
>>> c = CogniteClient()
>>> res = c.iam.projects.retrieve("publicdata")
"""
item = self._get(f"{self._RESOURCE_PATH}/{quote(project, '')}")
return Project._load(item.json(), cognite_client=self._cognite_client)

def update(self, item: ProjectUpdate) -> Project:
"""`Update a project <https://developer.cognite.com/api#tag/Projects/operation/updateProject>`_

Args:
item (ProjectUpdate): Project to update

Returns:
Project: Updated project

Examples:

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import ProjectUpdate
>>> c = CogniteClient()
>>> my_update = ProjectUpdate("my_project").name.set("new name").oidc_configuration.modify.skew_ms.set(100)
>>> res = c.iam.projects.update(my_update)
"""
project = item._project
response = self._post(
url_path=f"{self._RESOURCE_PATH}/{quote(project, '')}/update", json=item.dump(camel_case=True)
)
return Project._load(response.json(), cognite_client=self._cognite_client)

def list(self) -> ProjectURLNameList:
"""`List all projects <https://developer.cognite.com/api#tag/Projects/operation/listProjects>`_

Returns:
ProjectURLNameList: List of project URL names

Examples:

List all projects

>>> from cognite.client import CogniteClient
>>> c = CogniteClient()
>>> res = c.iam.projects.list()
"""
items = self._get(self._RESOURCE_PATH)
return ProjectURLNameList.load(items.json(), cognite_client=self._cognite_client)
23 changes: 22 additions & 1 deletion cognite/client/data_classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@
LabelDefinitionWrite,
LabelFilter,
)
from cognite.client.data_classes.projects import (
Claim,
OIDCConfiguration,
Project,
ProjectList,
ProjectUpdate,
ProjectURLName,
ProjectURLNameList,
ProjectWrite,
ProjectWriteList,
)
from cognite.client.data_classes.raw import (
Database,
DatabaseList,
Expand Down Expand Up @@ -276,7 +287,7 @@
TransformationSchemaColumn,
TransformationSchemaColumnList,
)
from cognite.client.data_classes.user_profiles import UserProfile, UserProfileList
from cognite.client.data_classes.user_profiles import UserProfile, UserProfileList, UserProfilesConfiguration
from cognite.client.data_classes.workflows import (
CancelExecution,
CDFTaskOutput,
Expand Down Expand Up @@ -531,6 +542,7 @@
"CoordinateReferenceSystem",
"UserProfile",
"UserProfileList",
"UserProfilesConfiguration",
"CancelExecution",
"WorkflowUpsert",
"WorkflowExecution",
Expand All @@ -557,4 +569,13 @@
"WorkflowTask",
"WorkflowUpsertList",
"WorkflowVersionUpsertList",
"Project",
"ProjectUpdate",
"ProjectWrite",
"ProjectURLName",
"ProjectURLNameList",
"ProjectWriteList",
"ProjectList",
"OIDCConfiguration",
"Claim",
]
Loading