Skip to content

Add count and find-one methods #6

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

Merged
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ table_rows = client.table_row_list(project, table_name, params={'offset': 100})
table_rows = client.table_row_list(project, table_name, InFilter("name", "sam"))
table_rows = client.table_row_list(project, table_name, filter_obj=EqFilter("Id", 100))

# Filter and count rows
count = client.table_count(project, table_name, filter_obj=EqFilter("Id", 100))

# Find one row
table_row = client.table_find_one(project, table_name, filter_obj=EqFilter("Id", 100), params={"sort": "-created_at"})

# Retrieve a single row
row_id = 10
row = client.table_row_detail(project, table_name, row_id)
Expand Down
16 changes: 16 additions & 0 deletions nocodb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ def get_table_uri(self, project: NocoDBProject, table: str) -> str:
)
)

def get_table_count_uri(self, project: NocoDBProject, table: str) -> str:
return "/".join(
(
self.get_table_uri(project, table),
'count'
)
)

def get_table_find_one_uri(self, project: NocoDBProject, table: str) -> str:
return "/".join(
(
self.get_table_uri(project, table),
'find-one'
)
)

def get_row_detail_uri(
self, project: NocoDBProject, table: str, row_id: int
):
Expand Down
76 changes: 50 additions & 26 deletions nocodb/infra/requests_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,75 @@ def __init__(self, auth_token: AuthToken, base_uri: str):
self.__session.headers.update({"Content-Type": "application/json"})
self.__api_info = NocoDBAPI(base_uri)

def _request(self, method, url, *args, **kwargs):
response = self.__session.request(method, url, *args, **kwargs)
response.raise_for_status()
return response

def table_row_list(
self,
project: NocoDBProject,
table: str,
filter_obj: Optional[WhereFilter] = None,
params: Optional[dict] = None,
) -> dict:

response = self.__session.get(
return self._request(
"GET",
self.__api_info.get_table_uri(project, table),
params=get_query_params(filter_obj, params),
)
return response.json()
).json()

def table_row_create(
self, project: NocoDBProject, table: str, body: dict
) -> dict:
return self.__session.post(
self.__api_info.get_table_uri(project, table), json=body
def table_row_create(self, project: NocoDBProject, table: str, body: dict) -> dict:
return self._request(
"POST", self.__api_info.get_table_uri(project, table), json=body
).json()

def table_row_detail(
self, project: NocoDBProject, table: str, row_id: int
) -> dict:
return self.__session.get(
def table_row_detail(self, project: NocoDBProject, table: str, row_id: int) -> dict:
return self._request(
"GET",
self.__api_info.get_row_detail_uri(project, table, row_id),
).json()

def table_row_update(
self, project: NocoDBProject, table: str, row_id: int, body: dict
) -> dict:
return self.__session.patch(
return self._request(
"PATCH",
self.__api_info.get_row_detail_uri(project, table, row_id),
json=body,
).json()

def table_row_delete(
self, project: NocoDBProject, table: str, row_id: int
) -> int:
return self.__session.delete(
def table_row_delete(self, project: NocoDBProject, table: str, row_id: int) -> int:
return self._request(
"DELETE",
self.__api_info.get_row_detail_uri(project, table, row_id),
).json()

def table_count(
self,
project: NocoDBProject,
table: str,
filter_obj: Optional[WhereFilter] = None,
) -> dict:
return self._request(
"GET",
self.__api_info.get_table_count_uri(project, table),
params=get_query_params(filter_obj),
).json()

def table_find_one(
self,
project: NocoDBProject,
table: str,
filter_obj: Optional[WhereFilter] = None,
params: Optional[dict] = None,
) -> dict:
return self._request(
"GET",
self.__api_info.get_table_find_one_uri(project, table),
params=get_query_params(filter_obj, params),
).json()

def table_row_nested_relations_list(
self,
project: NocoDBProject,
Expand All @@ -71,16 +97,14 @@ def table_row_nested_relations_list(
row_id: int,
column_name: str,
) -> dict:
return self.__session.get(
return self._request(
"GET",
self.__api_info.get_nested_relations_rows_list_uri(
project, table, relation_type, row_id, column_name
)
),
).json()

def project_create(
self,
body
):
return self.__session.post(
self.__api_info.get_project_uri(), json=body
def project_create(self, body):
return self._request(
"POST", self.__api_info.get_project_uri(), json=body
).json()
2 changes: 1 addition & 1 deletion nocodb/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def get_query_params(filter_obj, params) -> dict:
def get_query_params(filter_obj, params=None) -> dict:
query_params = params or {}
if filter_obj:
query_params["where"] = filter_obj.get_where()
Expand Down