Skip to content

Commit f76ce95

Browse files
committed
refactor; Implemented API endopints
1 parent 26b5cbc commit f76ce95

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

cognite/client/_api/projects.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
from __future__ import annotations
22

3+
from typing import Sequence, overload
4+
35
from cognite.client._api_client import APIClient
4-
from cognite.client.data_classes import Project, ProjectUpdate, ProjectWrite
6+
from cognite.client.data_classes import Project, ProjectList, ProjectUpdate, ProjectURLNameList, ProjectWrite
57

68

79
class ProjectsAPI(APIClient):
810
_RESOURCE_PATH = "/projects"
911

12+
@overload
1013
def create(self, item: ProjectWrite) -> Project:
14+
...
15+
16+
@overload
17+
def create(self, item: Sequence[ProjectWrite]) -> ProjectList:
18+
...
19+
20+
def create(self, item: ProjectWrite | Sequence[ProjectWrite]) -> Project | ProjectList:
1121
"""`Create a project <https://developer.cognite.com/api#tag/Projects/operation/createProject>`_"""
12-
raise NotImplementedError
22+
return self._create_multiple(item, list_cls=ProjectList, resource_cls=Project, input_resource_cls=ProjectWrite)
1323

1424
def retrieve(self, project: str) -> Project:
1525
"""`Retrieve a project <https://developer.cognite.com/api#tag/Projects/operation/getProject>`_"""
16-
raise NotImplementedError
26+
item = self._get(f"{self._RESOURCE_PATH}/{project}")
27+
return Project._load(item.json(), cognite_client=self._cognite_client)
1728

1829
def update(self, item: ProjectWrite | ProjectUpdate) -> Project:
1930
"""`Update a project <https://developer.cognite.com/api#tag/Projects/operation/updateProject>`_"""
20-
raise NotImplementedError
31+
return self._update_multiple(item, list_cls=ProjectList, resource_cls=Project, update_cls=ProjectUpdate)
2132

22-
def list(self) -> list[str]:
33+
def list(self) -> ProjectURLNameList:
2334
"""`List all projects <https://developer.cognite.com/api#tag/Projects/operation/listProjects>`_"""
24-
raise NotImplementedError
35+
items = self._get(self._RESOURCE_PATH)
36+
return ProjectURLNameList.load(items.json(), cognite_client=self._cognite_client)

cognite/client/data_classes/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,17 @@
151151
LabelDefinitionWrite,
152152
LabelFilter,
153153
)
154-
from cognite.client.data_classes.projects import Claim, OIDCConfiguration, Project, ProjectUpdate, ProjectWrite
154+
from cognite.client.data_classes.projects import (
155+
Claim,
156+
OIDCConfiguration,
157+
Project,
158+
ProjectList,
159+
ProjectUpdate,
160+
ProjectURLName,
161+
ProjectURLNameList,
162+
ProjectWrite,
163+
ProjectWriteList,
164+
)
155165
from cognite.client.data_classes.raw import (
156166
Database,
157167
DatabaseList,
@@ -561,6 +571,10 @@
561571
"Project",
562572
"ProjectUpdate",
563573
"ProjectWrite",
574+
"ProjectURLName",
575+
"ProjectURLNameList",
576+
"ProjectWriteList",
577+
"ProjectList",
564578
"OIDCConfiguration",
565579
"Claim",
566580
]

cognite/client/data_classes/projects.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
CogniteObjectUpdate,
99
CognitePrimitiveUpdate,
1010
CogniteResource,
11+
CogniteResourceList,
1112
CogniteUpdate,
1213
PropertySpec,
14+
WriteableCogniteResource,
1315
)
1416
from cognite.client.data_classes.user_profiles import UserProfilesConfiguration
1517

@@ -110,7 +112,24 @@ def dump(self, camel_case: bool = True) -> dict[str, Any]:
110112
return output
111113

112114

113-
class ProjectCore(CogniteResource, ABC):
115+
class ProjectURLName(CogniteResource):
116+
"""A project URL name is a unique identifier for a project.
117+
118+
Args:
119+
url_name (str): The URL name of the project. This is used as part of the request path in API calls.
120+
Valid URL names contain between 3 and 32 characters, and may only contain English letters, digits and hyphens,
121+
must contain at least one letter and may not start or end with a hyphen.
122+
"""
123+
124+
def __init__(self, url_name: str) -> None:
125+
self.url_name = url_name
126+
127+
@classmethod
128+
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> ProjectURLName:
129+
return cls(url_name=resource["urlName"])
130+
131+
132+
class ProjectCore(WriteableCogniteResource["ProjectWrite"], ABC):
114133
"""Projects are used to isolate data in CDF rom each other. All objects in CDF belong to a single project,
115134
and objects in different projects are isolated from each other.
116135
@@ -168,6 +187,10 @@ def __init__(
168187
# is required for the Read format but not the Write format.
169188
self.user_profiles_configuration = user_profiles_configuration
170189

190+
def as_write(self) -> ProjectWrite:
191+
"""Returns this instance which is a Project Write"""
192+
return self
193+
171194
@classmethod
172195
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> ProjectWrite:
173196
return cls(
@@ -230,6 +253,10 @@ def __init__(
230253
# is required for the Read format but not the Write format.
231254
self.user_profiles_configuration = user_profiles_configuration
232255

256+
def as_write(self) -> ProjectWrite:
257+
"""Returns this instance which is a Project Write"""
258+
raise NotImplementedError("Project cannot be used as a ProjectWrite")
259+
233260
@classmethod
234261
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> Project:
235262
return cls(
@@ -301,3 +328,15 @@ def _get_update_properties(cls) -> list[PropertySpec]:
301328
PropertySpec("oidc_configuration", is_nullable=True),
302329
PropertySpec("user_profiles_configuration", is_nullable=False),
303330
]
331+
332+
333+
class ProjectURLNameList(CogniteResourceList[ProjectURLName]):
334+
_RESOURCE = ProjectURLName
335+
336+
337+
class ProjectList(CogniteResourceList[Project]):
338+
_RESOURCE = Project
339+
340+
341+
class ProjectWriteList(CogniteResourceList[ProjectWrite]):
342+
_RESOURCE = ProjectWrite
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from cognite.client.data_classes import ProjectURLName, ProjectURLNameList
6+
7+
8+
@pytest.fixture(scope="module")
9+
def available_projects(cognite_client) -> ProjectURLNameList:
10+
if projects := cognite_client.iam.projects.list():
11+
return projects
12+
pytest.skip("Can't test projects without any projects available", allow_module_level=True)
13+
14+
15+
@pytest.mark.skip(reason="Lack access to projects to perform operations")
16+
class TestProjects:
17+
def test_list_projects(self, available_projects: ProjectURLNameList) -> None:
18+
assert len(available_projects) >= 1, "Expected at least one project"
19+
assert isinstance(available_projects, ProjectURLNameList)
20+
assert isinstance(available_projects[0], ProjectURLName)

0 commit comments

Comments
 (0)