Skip to content

Commit

Permalink
feat: Add fields to status ping payload and make status ping route au…
Browse files Browse the repository at this point in the history
…thenticated
  • Loading branch information
Emmo00 committed Dec 25, 2023
1 parent d1df67e commit ac5fa0a
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions flask_status/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
from types import FunctionType
from typing import Any, Dict, Callable
from flask import Flask, jsonify


Expand All @@ -7,14 +8,57 @@ def __init__(
self,
app: Flask | None = None,
url: str = "/api/status",
message: Any = {"status": "OK"},
message: str | Dict[str, str] = {"status": "OK"},
authenticated: bool = False,
authenticator: Callable[[Callable], Callable] = None,
):
self.status_ping_url = url
self.status_message = message
if app is not None:
self.init_app(app)
self.init_app(app, url, message, authenticated, authenticator)

def init_app(
self,
app: Flask,
url: str = "/api/status",
message: str | Dict[str, str] = {"status": "OK"},
authenticated: bool = False,
authenticator: Callable[[Callable], Callable] = None,
) -> None:
if type(url) is not str:
raise TypeError("Status Ping URL must be a string")
if not url.startswith("/"):
url = "/" + url
self.status_ping_url = url

if type(message) is str:
self.status_message = {"message": message}
elif type(message) is dict:
self.status_message = message
else:
raise TypeError("Status Ping message must be a string or dictionary")

if authenticated == True:
if type(authenticator) is not FunctionType:
raise TypeError(
"Status Ping authenticator must be a decorator function"
)

@app.route(self.status_ping_url, methods=["GET"])
@authenticator
def status_ping():
return jsonify(self.status_message), 200

return

def init_app(self, app: Flask):
@app.route(self.status_ping_url, methods=["GET"])
def status_ping():
return jsonify(self.status_message), 200

def add_field(self, key: str, value: Any) -> None:
if type(key) is not str:
raise TypeError("Status Ping field key must be a string")
if type(value) is str:
self.status_message[key] = value
elif type(value) is FunctionType:
self.status_message[key] = value()
else:
raise TypeError("Status Ping value must be a string or function")

0 comments on commit ac5fa0a

Please sign in to comment.