Skip to content

Commit

Permalink
api: add support for v2
Browse files Browse the repository at this point in the history
Scylla supports swagger v2 api for config and metrics-config.
Currently this api-client bases its per-api metadata on version 1.2,
so translate the v2 json metadata to the existing structures
so the v2 apis can be represented along the existing ones.

Fixes scylladb#47

Signed-off-by: Benny Halevy <[email protected]>
  • Loading branch information
bhalevy committed Nov 28, 2024
1 parent 67e26f5 commit baf0c5a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
33 changes: 32 additions & 1 deletion scylla_api_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
from argparse import ArgumentParser
from pprint import PrettyPrinter
import json

from .rest.scylla_rest_client import ScyllaRestClient

Expand Down Expand Up @@ -387,7 +388,7 @@ def load(self):
return
for module_def in top_json["apis"]:
# FIXME: handle service down, errors
module_json = self.client.get_raw_api_json(f"{module_def['path']}/")
module_json = self.client.get_raw_api_json(f"/api-doc{module_def['path']}/")
module_path = module_def['path'].strip(' /')
module = ScyllaApiModule(module_path, module_def['description'])
for command_json in module_json["apis"]:
Expand All @@ -401,3 +402,33 @@ def load(self):
command.load_json(command_json)
module.add_command(command)
self.add_module(module)
for module_def in [json.loads('{ "path": "/v2", "description": "V2 API"}')]:
module_json = self.client.get_raw_api_json(f"{module_def['path']}")["paths"]
module_path = module_def['path'].strip(' /')
module = ScyllaApiModule(module_path, module_def['description'])
for command_path in module_json:
operations = []
for op, v2_meta in module_json[command_path].items():
if op.upper() not in ["GET", "POST", "DELETE"]:
continue
operation = { "method": op }
kw_trans = { "description": "summary", "produces": "produces", "parameters": "parameters"}
for v2_kw, v1_kw in kw_trans.items():
if v2_kw in v2_meta:
operation[v1_kw] = v2_meta[v2_kw]
operations.append(operation)
if "produces" in v2_meta:
operation["produces"] = v2_meta["produces"]
command_path = command_path.strip(' /')
if command_path.startswith(module_path):
command_path = command_path[len(module_path)+1:]
json_str = f'{{"path": "{command_path}", "operations": {json.dumps(operations)} }}'
command_json = json.loads(json_str)
log.debug(f"{module_path} {command_path}: {command_json}")
command = ScyllaApiCommand(module_name=module_path,
command_name=command_path,
host=self._host,
port=self._port)
command.load_json(command_json)
module.add_command(command)
self.add_module(module)
4 changes: 2 additions & 2 deletions scylla_api_client/rest/scylla_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class ScyllaRestClient(RestClient):
def __init__(self, host: str = "localhost", port: str = "10000"):
super().__init__(host=host, port=port)

def get_raw_api_json(self, resource_path: str = ""):
if api := self.get(f"/api-doc{resource_path}"):
def get_raw_api_json(self, resource_path: str = "/api-doc"):
if api := self.get(resource_path):
return api.json()
return None

Expand Down

0 comments on commit baf0c5a

Please sign in to comment.