Skip to content
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

refactor: optimize json unmarshall #28

Merged
merged 1 commit into from
Mar 15, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/py_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
- main
jobs:
py_lint:
name: line lint
name: py lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
13 changes: 11 additions & 2 deletions opengemini_client/client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from requests import HTTPError

from opengemini_client.client import Client
from opengemini_client.models import Config, BatchPoints, Query, QueryResult
from opengemini_client.models import Config, BatchPoints, Query, QueryResult, Series, SeriesResult
from opengemini_client.url_const import UrlConst
from opengemini_client.utils import AtomicInt

Expand Down Expand Up @@ -125,7 +125,16 @@ 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 QueryResult(resp.json())
json_data = resp.json()
results = []

for result in json_data.get('results', []):
series_list = [Series(name=series['name']) for series in result.get('series', [])]
series_result = SeriesResult(series=series_list)
results.append(series_result)

return QueryResult(results=results)

raise HTTPError(f"Query error: {resp.status_code}, Response: {resp.text}")

def write_batch_points(self, database: str, batch_points: BatchPoints):
Expand Down
14 changes: 7 additions & 7 deletions opengemini_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ class Series:
values: List[List[Any]] = field(default_factory=list)


@dataclass
class SeriesResult:
series: List[Series] = field(default_factory=list)
error: str = ""


@dataclass
class Query:
database: str
command: str
retention_policy: str


@dataclass
class SeriesResult:
series: List[Series] = field(default_factory=list)
error: str = None


@dataclass
class QueryResult:
results: List[SeriesResult] = field(default_factory=list)
error: str = ""
error: str = None
9 changes: 7 additions & 2 deletions opengemini_client/query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ def test_query_databases_success(self):
)
with client_impl.OpenGeminiDBClient(cfg) as cli:
qr = cli.query(query=models.Query('', 'show databases', ''))
result = qr.results['results'][0]
self.assertEqual(result['statement_id'], 0)
self.assertEqual(qr.error, None)
results = qr.results
self.assertEqual(len(results), 1)
result = results[0]
self.assertEqual(result.error, None)
series = result.series
self.assertEqual(len(series), 1)