Skip to content

Commit 254998b

Browse files
authored
Add support for table management and column management (#7)
* use urljoin instead of string concatenation, added gitignore * added table management endpoints * added table column endpoints * fixed a name clash with the get_table_uri method
1 parent 3dd40a3 commit 254998b

File tree

4 files changed

+190
-21
lines changed

4 files changed

+190
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

nocodb/api.py

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
from enum import Enum
2+
from urllib.parse import urljoin
23
from .nocodb import NocoDBProject
34

45

56
class NocoDBAPIUris(Enum):
6-
V1_DB_DATA_PREFIX = "api/v1/db/data"
7-
V1_DB_META_PREFIX = "api/v1/db/meta"
7+
V1_DB_DATA_PREFIX = "api/v1/db/data/"
8+
V1_DB_META_PREFIX = "api/v1/db/meta/"
89

910

1011
class NocoDBAPI:
1112
def __init__(self, base_uri: str):
12-
self.__base_data_uri = (
13-
f"{base_uri}/{NocoDBAPIUris.V1_DB_DATA_PREFIX.value}"
14-
)
15-
self.__base_meta_uri = (
16-
f"{base_uri}/{NocoDBAPIUris.V1_DB_META_PREFIX.value}"
17-
)
13+
self.__base_data_uri = urljoin(base_uri + "/", NocoDBAPIUris.V1_DB_DATA_PREFIX.value)
14+
self.__base_meta_uri = urljoin(base_uri + "/", NocoDBAPIUris.V1_DB_META_PREFIX.value)
1815

1916
def get_table_uri(self, project: NocoDBProject, table: str) -> str:
20-
return "/".join(
17+
return urljoin(self.__base_data_uri, "/".join(
2118
(
22-
self.__base_data_uri,
2319
project.org_name,
2420
project.project_name,
2521
table,
2622
)
27-
)
23+
))
2824

2925
def get_table_count_uri(self, project: NocoDBProject, table: str) -> str:
3026
return "/".join(
@@ -45,15 +41,14 @@ def get_table_find_one_uri(self, project: NocoDBProject, table: str) -> str:
4541
def get_row_detail_uri(
4642
self, project: NocoDBProject, table: str, row_id: int
4743
):
48-
return "/".join(
44+
return urljoin(self.__base_data_uri, "/".join(
4945
(
50-
self.__base_data_uri,
5146
project.org_name,
5247
project.project_name,
5348
table,
5449
str(row_id),
5550
)
56-
)
51+
))
5752

5853
def get_nested_relations_rows_list_uri(
5954
self,
@@ -63,24 +58,57 @@ def get_nested_relations_rows_list_uri(
6358
row_id: int,
6459
column_name: str,
6560
) -> str:
66-
return "/".join(
61+
return urljoin(self.__base_data_uri, "/".join(
6762
(
68-
self.__base_data_uri,
6963
project.org_name,
7064
project.project_name,
7165
table,
7266
str(row_id),
7367
relation_type,
7468
column_name,
7569
)
76-
)
70+
))
7771

7872
def get_project_uri(
7973
self,
8074
) -> str:
81-
return "/".join(
75+
return urljoin(self.__base_meta_uri, "projects")
76+
77+
def get_project_tables_uri(
78+
self, project: NocoDBProject,
79+
) -> str:
80+
return urljoin(self.__base_meta_uri, "/".join(
8281
(
83-
self.__base_meta_uri,
84-
"projects"
82+
"projects",
83+
project.project_name,
84+
"tables"
8585
)
86-
)
86+
))
87+
88+
def get_table_meta_uri(
89+
self, tableId: str, operation: str = None,
90+
) -> str:
91+
additional_path = []
92+
if operation is not None:
93+
additional_path.append(operation)
94+
95+
return urljoin(self.__base_meta_uri, "/".join(
96+
[
97+
"tables",
98+
tableId,
99+
] + additional_path
100+
))
101+
102+
def get_column_uri(
103+
self, columnId: str, operation: str = None,
104+
) -> str:
105+
additional_path = []
106+
if operation is not None:
107+
additional_path.append(operation)
108+
109+
return urljoin(self.__base_meta_uri, "/".join(
110+
[
111+
"columns",
112+
columnId,
113+
] + additional_path
114+
))

nocodb/infra/requests_client.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,81 @@ def project_create(self, body):
122122
return self._request(
123123
"POST", self.__api_info.get_project_uri(), json=body
124124
).json()
125+
126+
def table_create(
127+
self, project: NocoDBProject, body: dict
128+
) -> dict:
129+
return self.__session.post(
130+
url=self.__api_info.get_project_tables_uri(project),
131+
json=body,
132+
).json()
133+
134+
def table_list(
135+
self,
136+
project: NocoDBProject,
137+
params: Optional[dict] = None,
138+
) -> dict:
139+
return self.__session.get(
140+
url=self.__api_info.get_project_tables_uri(project),
141+
params=params,
142+
).json()
143+
144+
def table_read(
145+
self, tableId: str,
146+
) -> dict:
147+
return self.__session.get(
148+
url=self.__api_info.get_table_meta_uri(tableId)
149+
).json()
150+
151+
def table_update(
152+
self, tableId: str, body: dict
153+
):
154+
return self.__session.patch(
155+
url=self.__api_info.get_table_meta_uri(tableId),
156+
json=body,
157+
).json()
158+
159+
def table_delete(
160+
self, tableId: str,
161+
) -> dict:
162+
return self.__session.delete(
163+
url=self.__api_info.get_table_meta_uri(tableId)
164+
).json()
165+
166+
def table_reorder(
167+
self, tableId: str, order: int
168+
) -> dict:
169+
return self.__session.post(
170+
url=self.__api_info.get_table_meta_uri(tableId, "reorder"),
171+
json={ "order": order }
172+
).json()
173+
174+
def table_column_create(
175+
self, tableId: str, body: dict,
176+
) -> dict:
177+
return self.__session.post(
178+
url=self.__api_info.get_table_meta_uri(tableId, "columns"),
179+
json=body,
180+
).json()
181+
182+
def table_column_update(
183+
self, columnId: str, body: dict,
184+
) -> dict:
185+
return self.__session.patch(
186+
url=self.__api_info.get_column_uri(columnId),
187+
json=body,
188+
).json()
189+
190+
def table_column_delete(
191+
self, columnId: str,
192+
) -> dict:
193+
return self.__session.delete(
194+
url=self.__api_info.get_column_uri(columnId)
195+
).json()
196+
197+
def table_column_set_primary(
198+
self, columnId: str,
199+
) -> bool:
200+
return self.__session.post(
201+
url=self.__api_info.get_column_uri(columnId, "primary"),
202+
).json()

nocodb/nocodb.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,65 @@ def table_row_nested_relations_list(
134134
column_name: str,
135135
) -> dict:
136136
pass
137+
138+
@abstractmethod
139+
def table_create(
140+
self, project: NocoDBProject, body: dict
141+
) -> dict:
142+
pass
143+
144+
@abstractmethod
145+
def table_list(
146+
self,
147+
project: NocoDBProject,
148+
params: Optional[dict] = None,
149+
) -> dict:
150+
pass
151+
152+
@abstractmethod
153+
def table_read(
154+
self, tableId: str,
155+
) -> dict:
156+
pass
157+
158+
@abstractmethod
159+
def table_update(
160+
self, tableId: str, body: dict,
161+
) -> bool:
162+
pass
163+
164+
@abstractmethod
165+
def table_delete(
166+
self, tableId: str,
167+
) -> dict:
168+
pass
169+
170+
@abstractmethod
171+
def table_reorder(
172+
self, tableId: str, order: int,
173+
) -> dict:
174+
pass
175+
176+
@abstractmethod
177+
def table_column_create(
178+
self, tableId: str, body: dict,
179+
) -> dict:
180+
pass
181+
182+
@abstractmethod
183+
def table_column_update(
184+
self, columnId: str, body: dict,
185+
) -> dict:
186+
pass
187+
188+
@abstractmethod
189+
def table_column_delete(
190+
self, columnId: str,
191+
) -> dict:
192+
pass
193+
194+
@abstractmethod
195+
def table_column_set_primary(
196+
self, columnId: str,
197+
) -> dict:
198+
pass

0 commit comments

Comments
 (0)