Initial commit
This commit is contained in:
4
birb_scheduler/utils/__init__.py
Normal file
4
birb_scheduler/utils/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
from .require_decorators import json_required
|
||||
from .error_handlers import register_all_error_handlers
|
||||
from .healthchecks import register_health_checks
|
20
birb_scheduler/utils/error_handlers.py
Normal file
20
birb_scheduler/utils/error_handlers.py
Normal file
@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
def get_standard_error_handler(code: int):
|
||||
def error_handler(err):
|
||||
return jsonify({"msg": err.description, "status": code}), code
|
||||
|
||||
return error_handler
|
||||
|
||||
|
||||
def register_all_error_handlers(app):
|
||||
"""
|
||||
function to register all handlers
|
||||
"""
|
||||
|
||||
error_codes_to_override = [404, 403, 401, 405, 400, 409, 422]
|
||||
|
||||
for code in error_codes_to_override:
|
||||
app.register_error_handler(code, get_standard_error_handler(code))
|
13
birb_scheduler/utils/healthchecks.py
Normal file
13
birb_scheduler/utils/healthchecks.py
Normal file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
from healthcheck import HealthCheck
|
||||
from flask import Flask
|
||||
|
||||
|
||||
def health_database_status():
|
||||
return True, f'db is ok'
|
||||
|
||||
|
||||
def register_health_checks(app: Flask):
|
||||
health = HealthCheck()
|
||||
health.add_check(health_database_status)
|
||||
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())
|
16
birb_scheduler/utils/require_decorators.py
Normal file
16
birb_scheduler/utils/require_decorators.py
Normal file
@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
from flask import request, abort
|
||||
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def json_required(f):
|
||||
@wraps(f)
|
||||
def call(*args, **kwargs):
|
||||
|
||||
if request.is_json:
|
||||
return f(*args, **kwargs)
|
||||
else:
|
||||
abort(400, "JSON required")
|
||||
|
||||
return call
|
Reference in New Issue
Block a user