Skip to content

Commit

Permalink
feat: add database and rp interfaces and their usecases
Browse files Browse the repository at this point in the history
Signed-off-by: heiliuchao <[email protected]>
  • Loading branch information
heiliuchao committed Mar 26, 2024
1 parent 06409d7 commit acdda60
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions opengemini_client/client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def request(self, method, server_url, url_path, headers=None, body=None, params=
prepared = req.prepare()
resp = self.session.send(prepared)
if not 200 <= resp.status_code < 300:
raise HTTPError(f"HTTP error: {resp.status_code}, Response: {resp.text}")
raise HTTPError(f"Query error_code: {resp.status_code}, error_msg: {resp.text}")
return resp

def exec_http_request_by_index(self, idx, method, url_path, headers=None, body=None) -> requests.Response:
Expand All @@ -140,7 +140,7 @@ def exec_http_request_by_index(self, idx, method, url_path, headers=None, body=N
def ping(self, idx: int):
resp = self.exec_http_request_by_index(idx, 'GET', UrlConst.PING)
if resp.status_code != HTTPStatus.NO_CONTENT:
raise HTTPError(f"ping openGeminiDB status is {resp.status_code}")
raise HTTPError(f"Query error_code: {resp.status_code}, error_msg: {resp.text}")

def query(self, query: Query) -> QueryResult:
server_url = self.get_server_url()
Expand All @@ -149,34 +149,30 @@ def query(self, query: Query) -> QueryResult:
resp = self.request(method='GET', server_url=server_url, url_path=UrlConst.QUERY, params=params)
if resp.status_code == HTTPStatus.OK:
return resolve_query_body(resp)
raise HTTPError(f"Query error: {resp.status_code}, Response: {resp.text}")
raise HTTPError(f"Query error_code: {resp.status_code}, error_msg: {resp.text}")

def _query_post(self, query: Query) -> QueryResult:
server_url = self.get_server_url()
params = {'db': query.database, 'q': query.command, 'rp': query.retention_policy}

resp = self.request(method='POST', server_url=server_url, url_path=UrlConst.QUERY, params=params)
if resp.status_code == HTTPStatus.OK:
return resolve_query_body(resp)
raise HTTPError(f"Query error: {resp.status_code}, Response: {resp.text}")

def create_database(self, dbname):
if len(dbname) == 0:
raise ValueError("empty database name")
query_string = f"CREATE DATABASE {str(dbname).strip()}"
return self._query_post(Query(database=dbname, command=query_string, retention_policy=''))
return resolve_query_body(resp)
raise HTTPError(f"Query error_code: {resp.status_code}, error_msg: {resp.text}")

def create_database_with_rp(self, dbname, rp_config: RpConfig):
def create_database(self, dbname, rp_config=None):
if len(dbname) == 0:
raise ValueError("empty database name")
query_string = f"CREATE DATABASE {str(dbname).strip()} WITH DURATION {rp_config.duration} REPLICATION 1"

if len(rp_config.shard_group_duration) > 0:
query_string += f" SHARD DURATION {rp_config.shard_group_duration.strip()}"
if len(rp_config.index_duration) > 0:
query_string += f" INDEX DURATION {rp_config.index_duration.strip()}"
if len(rp_config.name) > 0:
query_string += f" NAME {rp_config.name.strip()}"
if rp_config is None:
query_string = f"CREATE DATABASE {str(dbname).strip()}"
else:
query_string = f"CREATE DATABASE {str(dbname).strip()} WITH DURATION {rp_config.duration} REPLICATION 1"
if len(rp_config.shard_group_duration) > 0:
query_string += f" SHARD DURATION {rp_config.shard_group_duration.strip()}"
if len(rp_config.index_duration) > 0:
query_string += f" INDEX DURATION {rp_config.index_duration.strip()}"
if len(rp_config.name) > 0:
query_string += f" NAME {rp_config.name.strip()}"
return self._query_post(Query(database=str(dbname).strip(), command=query_string, retention_policy=''))

def drop_database(self, dbname):
Expand Down

0 comments on commit acdda60

Please sign in to comment.