-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathproject_api.py
123 lines (108 loc) · 3.76 KB
/
project_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#
# Copyright 2022 Logical Clocks AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
from hopsworks import client, constants, project
from hopsworks.client.exceptions import RestAPIError
class ProjectApi:
def _exists(self, name: str):
"""Check if a project exists.
# Arguments
name: Name of the project.
# Returns
`bool`: True if project exists, otherwise False
# Raises
`RestAPIError`: If unable to check the existence of the project
"""
try:
self._get_project(name)
return True
except RestAPIError:
return False
def _get_projects(self):
"""Get all projects accessible by the user.
# Returns
`List[Project]`: List of Project objects
# Raises
`RestAPIError`: If unable to get the projects
"""
_client = client.get_instance()
path_params = [
"project",
]
project_team_json = _client._send_request("GET", path_params)
projects = []
for project_team in project_team_json:
projects.append(self._get_project(project_team["project"]["name"]))
return projects
def _get_project(self, name: str):
"""Get a project.
# Arguments
name: Name of the project.
# Returns
`Project`: The Project object
# Raises
`RestAPIError`: If unable to get the project
"""
_client = client.get_instance()
path_params = [
"project",
"getProjectInfo",
name,
]
project_json = _client._send_request("GET", path_params)
return project.Project.from_response_json(project_json)
def _create_project(
self, name: str, description: str = None, feature_store_topic: str = None
):
"""Create a new project.
# Arguments
name: Name of the project.
description: Description of the project.
feature_store_topic: Feature store topic name.
# Returns
`Project`: The Project object
# Raises
`RestAPIError`: If unable to create the project
"""
_client = client.get_instance()
path_params = ["project"]
query_params = {"projectName": name}
headers = {"content-type": "application/json"}
data = {
"projectName": name,
"services": constants.SERVICES.LIST,
"description": description,
"featureStoreTopic": feature_store_topic,
}
_client._send_request(
"POST",
path_params,
headers=headers,
query_params=query_params,
data=json.dumps(data),
)
# The return of the project creation is not a ProjectDTO, so get the correct object after creation
project = self._get_project(name)
print("Project created successfully, explore it at " + project.get_url())
return project
def get_client(self):
_client = client.get_instance()
path_params = [
"project",
_client._project_id,
"client",
]
return _client._send_request("GET", path_params, stream=True)