init
This commit is contained in:
1
views/apis/__init__.py
Normal file
1
views/apis/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__all__ = ['bark_service', 'health']
|
66
views/apis/bark_service.py
Normal file
66
views/apis/bark_service.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from flask import request
|
||||
from flask_restful import Resource
|
||||
from utils.error_handler import error_handler
|
||||
from app.app import app
|
||||
from app.api import api
|
||||
from munch import Munch
|
||||
import requests
|
||||
|
||||
class BarkResource(Resource):
|
||||
def post(self):
|
||||
try:
|
||||
data = Munch.fromDict(request.get_json(silent=True))
|
||||
app.logger.debug(data)
|
||||
config = Munch.fromDict(app.config)
|
||||
content = {
|
||||
'title': config.title.firing if data.status == 'firing' else config.title.resolved if data.status == 'resolved' else config.title.default,
|
||||
'category': 'category'
|
||||
}
|
||||
|
||||
# TODO: alertmanager message to bark content.
|
||||
'''
|
||||
{
|
||||
"version": "4",
|
||||
"groupKey": <string>, // key identifying the group of alerts (e.g. to deduplicate)
|
||||
"truncatedAlerts": <int>, // how many alerts have been truncated due to "max_alerts"
|
||||
"status": "<resolved|firing>",
|
||||
"receiver": <string>,
|
||||
"groupLabels": <object>,
|
||||
"commonLabels": <object>,
|
||||
"commonAnnotations": <object>,
|
||||
"externalURL": <string>, // backlink to the Alertmanager.
|
||||
"alerts": [
|
||||
{
|
||||
"status": "<resolved|firing>",
|
||||
"labels": <object>,
|
||||
"annotations": <object>,
|
||||
"startsAt": "<rfc3339>",
|
||||
"endsAt": "<rfc3339>",
|
||||
"generatorURL": <string>, // identifies the entity that caused the alert
|
||||
"fingerprint": <string> // fingerprint to identify the alert
|
||||
},
|
||||
...
|
||||
]
|
||||
}
|
||||
'''
|
||||
content.body = app.jinja_environment.get_template(config.template_file).render(data)
|
||||
app.logger.debug(content.body)
|
||||
for to in config.to:
|
||||
content['device_key'] = to
|
||||
response = requests.post(
|
||||
url = config.bark_api,
|
||||
headers = {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
},
|
||||
data=content
|
||||
)
|
||||
app.logger.info(response)
|
||||
return {
|
||||
'success': True,
|
||||
'message': '',
|
||||
'data': {},
|
||||
}
|
||||
except Exception as e:
|
||||
return error_handler(e)
|
||||
|
||||
api.add_resource(BarkResource, '/api/send')
|
12
views/apis/health.py
Normal file
12
views/apis/health.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from flask_restful import Resource
|
||||
from app.api import api
|
||||
|
||||
class HealthResource(Resource):
|
||||
def get(self):
|
||||
return {
|
||||
'success': True,
|
||||
'message': '',
|
||||
'data': {},
|
||||
}
|
||||
|
||||
api.add_resource(HealthResource, '/health')
|
Reference in New Issue
Block a user