From 46d5650d8c17252abcf67cf0af0d834d4dce650f Mon Sep 17 00:00:00 2001 From: Ian Date: Tue, 26 Oct 2021 21:09:13 +0300 Subject: [PATCH] Added swagger code to API ... not the best way of doing this, but it kind of works --- a10rest/README.md | 10 ++++++++ a10rest/a10rest.py | 59 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/a10rest/README.md b/a10rest/README.md index 68747aa2..d710586b 100644 --- a/a10rest/README.md +++ b/a10rest/README.md @@ -17,6 +17,16 @@ This is the full form for a local, untrusted pypi repository with a proxy thrown pip3 install --index-url x.x.x.x/simple a10 -v --trusted-host x.x.x.x --proxy=y.y.y.y ``` +### Swagger + +The python3 package flask-swagger is used to generate a JSON file that can be used by Swagger. + +```bash +pip3 install flask-swagger +``` + +The document is exposed on the `/spec` endpoint when running. + ## Prerequisites A mosquitto broker and mongo database must be running as referred to in the a10.conf file. diff --git a/a10rest/a10rest.py b/a10rest/a10rest.py index 1b843068..8ea896f4 100644 --- a/a10rest/a10rest.py +++ b/a10rest/a10rest.py @@ -7,7 +7,9 @@ from a10.asvr import elements, policies, attestation, claims, expectedvalues, results from a10.structures import constants -from flask import Flask, request, send_from_directory +from flask import Flask, request, send_from_directory, jsonify + +from flask_swagger import swagger print(sys.path) @@ -25,12 +27,38 @@ def hello(): return "Hello from A10REST" + + +# +# Swagger - documentation for OpenAPI +# + +@a10rest.route("/spec") +def spec(): + swag = swagger(a10rest) + swag['info']['version'] = "1.0" + swag['info']['description'] = "The A10 REST API Server" + swag['info']['title'] = "A10REST" + swag['title']= "A10REST" + return jsonify(swag) + + + # # ELEMENTS # @a10rest.route("/elements", methods=['GET']) def getelements(): - + """ + Gets a list of all elements + --- + get: + responses: + - 200: + content: + application/json: + schema: list itemid + """ es = [ x['itemid'] for x in elements.getElements() ] print("ES is ",es) return str(es), 200 @@ -38,6 +66,7 @@ def getelements(): @a10rest.route("/elementsByTag", methods=['GET']) def getelementsbytag(): + #Expects a comma separted list of tags tags = request.args.get('tags') taglist = tags.split(",") @@ -49,20 +78,22 @@ def getelementsbytag(): @a10rest.route("/element/", methods=['GET']) def getelement(itemid): - + """ + Gets the details of a specific element + --- + get: + parameters: + - in: itemid + schema: Item ID + responses: + - 404 + - 200: + content: + application/json: + schema: Element + """ elem = elements.getElement(itemid) - #this was a modification by victor - no idea :-) - #OK.It was for some more advanced searching of the database, but not sure - #if it works anymore...commented out for safety reasons - # - #if itemid is not None: - # - # print("itemid", itemid) - # elem = elements.getElement(itemid) - #else: - # elem = elements.getElementByParams(request.args) - if elem.rc() != constants.SUCCESS: return elem.msg(), 404 else: