Skip to content

Commit

Permalink
Added swagger code to API ... not the best way of doing this, but it …
Browse files Browse the repository at this point in the history
…kind of works
  • Loading branch information
iolivergithub committed Oct 26, 2021
1 parent 26378c3 commit 46d5650
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
10 changes: 10 additions & 0 deletions a10rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 45 additions & 14 deletions a10rest/a10rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -25,19 +27,46 @@ 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


@a10rest.route("/elementsByTag", methods=['GET'])
def getelementsbytag():

#Expects a comma separted list of tags
tags = request.args.get('tags')
taglist = tags.split(",")
Expand All @@ -49,20 +78,22 @@ def getelementsbytag():

@a10rest.route("/element/<itemid>", 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:
Expand Down

0 comments on commit 46d5650

Please sign in to comment.