Browse Source

refinements to API

joel
Simon Bowie 1 year ago
parent
commit
94562dc7b1
3 changed files with 49 additions and 19 deletions
  1. +28
    -19
      web/app/api.py
  2. +2
    -0
      web/app/schemas.py
  3. +19
    -0
      web/app/templates/api.html

+ 28
- 19
web/app/api.py View File

# @creation_date: 2023-01-12 # @creation_date: 2023-01-12
# @license: The MIT License <https://opensource.org/licenses/MIT> # @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk> # @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Exposes database data as exportable JSON for an API
# @purpose: Exposes database data as exportable JSON for a RESTful API
# @acknowledgements: # @acknowledgements:
# https://auth0.com/blog/developing-restful-apis-with-python-and-flask/
# https://stackoverflow.com/questions/59721478/serializing-sqlalchemy-with-marshmallow


from flask import Blueprint, render_template, request, flash, redirect, url_for, jsonify from flask import Blueprint, render_template, request, flash, redirect, url_for, jsonify
from flask_login import login_required, current_user from flask_login import login_required, current_user


api = Blueprint('api', __name__) api = Blueprint('api', __name__)


# route for api page
@api.route('/api')
def api_page():
return render_template('api.html')

# route for exporting all users in database # route for exporting all users in database
@api.route('/api/users') @api.route('/api/users')
@login_required @login_required
# return all rows as a JSON array of objects # return all rows as a JSON array of objects
return jsonify(result) return jsonify(result)


def get_resource_json(type):
resources = Resource.query.filter_by(type=type)
if (type=='tool'):
resources_schema = ToolSchema(many=True)
elif (type=='practice'):
resources_schema = PracticeSchema(many=True)
elif (type=='book'):
resources_schema = BookSchema(many=True)
result = resources_schema.dump(resources)
# return all rows as a JSON array of objects
result = jsonify(result)

return result

# route for exporting all tools in database # route for exporting all tools in database
@api.route('/api/tools') @api.route('/api/tools')
def get_tools(): def get_tools():
type = 'tool' type = 'tool'
resources = Resource.query.filter_by(type=type)
resources_schema = ToolSchema(many=True)
result = resources_schema.dump(resources)

# return all rows as a JSON array of objects
return jsonify(result)
output = get_resource_json(type)
return output


# route for exporting all practices in database # route for exporting all practices in database
@api.route('/api/practices') @api.route('/api/practices')
def get_practices(): def get_practices():
type = 'practice' type = 'practice'
resources = Resource.query.filter_by(type=type)
resources_schema = PracticeSchema(many=True)
result = resources_schema.dump(resources)

# return all rows as a JSON array of objects
return jsonify(result)
output = get_resource_json(type)
return output


# route for exporting all books in database # route for exporting all books in database
@api.route('/api/books') @api.route('/api/books')
def get_books(): def get_books():
type = 'book' type = 'book'
resources = Resource.query.filter_by(type=type)
resources_schema = BookSchema(many=True)
result = resources_schema.dump(resources)

# return all rows as a JSON array of objects
return jsonify(result)
output = get_resource_json(type)
return output

+ 2
- 0
web/app/schemas.py View File

# @author: Simon Bowie <ad7588@coventry.ac.uk> # @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Data schemas for API export # @purpose: Data schemas for API export
# @acknowledgements: # @acknowledgements:
# https://marshmallow.readthedocs.io/en/stable/quickstart.html#next-steps
# https://stackoverflow.com/questions/59721478/serializing-sqlalchemy-with-marshmallow


from . import ma from . import ma
from marshmallow import Schema from marshmallow import Schema

+ 19
- 0
web/app/templates/api.html View File

{% block content %}

<a href="/api/users">Users</a> (login required)

<br><br>

<a href="/api/tools">Tools</a>

<br><br>

<a href="/api/practices">Practices</a>

<br><br>

<a href="/api/books">Books</a>

<br><br>

{% endblock %}

Loading…
Cancel
Save