feat: Add script to print OpenAPI JSON

This will allow generating OpenAPI clients in multiple languages
 without relying on manually updating the self-written client.
This commit is contained in:
LoveIsGrief 2021-09-03 14:54:22 +02:00
parent 93c1dff71b
commit d3d967e961
No known key found for this signature in database
GPG Key ID: E96D1EDFA05345EB
5 changed files with 27 additions and 2 deletions

View File

@ -12,4 +12,5 @@ def start_webserver(args: Dict[str, Any]) -> None:
# Initialize configuration
config = Configuration(args, RunMode.WEBSERVER).get_config()
ApiServer(config, standalone=True)
api_server = ApiServer(config, standalone=True)
api_server.start_api()

View File

@ -68,8 +68,10 @@ class ApiServer(RPCHandler):
default_response_class=FTJSONResponse,
)
self.configure_app(self.app, self._config)
# self.start_api()
self.start_api()
def get_open_api_json(self):
return self.app.openapi()
def add_rpc_handler(self, rpc: RPC):
"""

View File

@ -38,6 +38,7 @@ class RPCManager:
logger.info('Enabling rpc.api_server')
from freqtrade.rpc.api_server import ApiServer
apiserver = ApiServer(config)
apiserver.start_api()
apiserver.add_rpc_handler(self._rpc)
self.registered_modules.append(apiserver)

19
scripts/openapi.py Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env python3
import argparse
import json
from freqtrade.rpc.api_server import ApiServer
def main(indent: int):
openapi_dict = ApiServer({"api_server": {}}).get_open_api_json()
print(json.dumps(openapi_dict, indent=indent), flush=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Simply outputs the OpenAPI JSON of the REST API")
parser.add_argument("-i", "--indent", type=int, default=2)
args = parser.parse_args()
main(args.indent)

View File

@ -320,6 +320,7 @@ def test_api_run(default_conf, mocker, caplog):
mocker.patch('freqtrade.rpc.api_server.webserver.UvicornServer', server_mock)
apiserver = ApiServer(default_conf)
apiserver.start_api()
apiserver.add_rpc_handler(RPC(get_patched_freqtradebot(mocker, default_conf)))
assert server_mock.call_count == 1
@ -395,6 +396,7 @@ def test_api_cleanup(default_conf, mocker, caplog):
mocker.patch('freqtrade.rpc.api_server.webserver.UvicornServer', server_mock)
apiserver = ApiServer(default_conf)
apiserver.start_api()
apiserver.add_rpc_handler(RPC(get_patched_freqtradebot(mocker, default_conf)))
apiserver.cleanup()