Add more examples

This commit is contained in:
Patrick Jentsch 2021-09-14 16:17:26 +02:00
parent 523faaea05
commit 8a69d6364a
2 changed files with 35 additions and 5 deletions

View File

@ -1,5 +1,6 @@
from flask_restx import Namespace, Resource from flask_restx import Namespace, Resource
from .auth import token_auth from .auth import token_auth
from ..jobs import tasks
from ..models import Job from ..models import Job
@ -7,12 +8,41 @@ ns = Namespace('jobs', description='Job operations')
@ns.route('/') @ns.route('/')
class JobList(Resource): class API_Jobs(Resource):
'''Shows a list of all jobs, and lets you POST to add new job''' '''Shows a list of all jobs and lets you POST to add new job'''
@ns.doc(security='apiKey') @ns.doc(security='apiKey')
@token_auth.login_required @token_auth.login_required
def get(self): def get(self):
'''List all jobs''' '''List all jobs'''
# TODO: Implement the correct get_jobs functionality
jobs = Job.query.all() jobs = Job.query.all()
return [job.to_dict(include_relationships=False) for job in jobs] return [job.to_dict(include_relationships=False) for job in jobs]
@ns.doc(security='apiKey')
@token_auth.login_required
def post(self):
'''Create a new job'''
# TODO: Implement this
pass
@ns.route('/<int:id>')
class API_Job(Resource):
'''Show a single job and lets you delete it'''
@ns.doc(security='apiKey')
@token_auth.login_required
def get(self, id):
'''Get a job by id'''
job = Job.query.get_or_404(id)
return job.to_dict(include_relationships=False)
@ns.doc(security='apiKey')
@token_auth.login_required
def delete(self, id):
'''Delete a job by id'''
job = Job.query.get_or_404(id)
# We use this imported task because it will run in the background
tasks.delete_job(job.id)
return '', 204

View File

@ -7,13 +7,13 @@ ns = Namespace('tokens', description='Token operations')
@ns.route('/') @ns.route('/')
class Token(Resource): class API_Tokens(Resource):
'''Get or revoke a user authentication token''' '''Get or revoke a user authentication token'''
@ns.doc(security='basicAuth') @ns.doc(security='basicAuth')
@basic_auth.login_required @basic_auth.login_required
def post(self): def post(self):
'''Get user token''' '''Get a user token'''
token = basic_auth.current_user().get_token() token = basic_auth.current_user().get_token()
db.session.commit() db.session.commit()
return {'token': 'Bearer ' + token} return {'token': 'Bearer ' + token}
@ -21,7 +21,7 @@ class Token(Resource):
@ns.doc(security='apiKey') @ns.doc(security='apiKey')
@token_auth.login_required @token_auth.login_required
def delete(self): def delete(self):
'''Revoke user token''' '''Revoke a user token'''
token_auth.current_user().revoke_token() token_auth.current_user().revoke_token()
db.session.commit() db.session.commit()
return '', 204 return '', 204