Skip to content

Commit

Permalink
Add notification for third party services (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshpme authored Apr 18, 2024
1 parent d9567bb commit 930364e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ FLASK_ENV=development
FLASK_DEBUG=1
FLASK_APP=openreferee_server.app
DATABASE_URI = 'postgresql:///openreferee'
NOTIFY_URL=
NOTIFY_TOKEN=
2 changes: 2 additions & 0 deletions openreferee_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from . import __version__
from .db import db, register_db_cli
from .notify import notify_init

try:
from flask_cors import CORS
Expand All @@ -26,6 +27,7 @@ def create_app():
register_error_handlers(app)
db.init_app(app)
register_db_cli(app)
notify_init(app)
app.register_blueprint(api)
return app

Expand Down
59 changes: 59 additions & 0 deletions openreferee_server/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import json
import logging
import os
import threading

from requests.exceptions import ConnectionError, HTTPError, RequestException, Timeout

from .operations import setup_requests_session


class NotifyService:
def __init__(self, url=None, token=None, logger=None) -> None:
self.url = url
self.token = token
self.logger = logger
self.session = None

def log_error(self, *args):
if self.logger is not None:
self.logger.error(*args)

def send(self, payload):
try:
self.session.post(self.url, data=json.dumps({"payload": payload}))
except HTTPError as e:
self.log_error("Invalid response from notify: %s", str(e))
except ConnectionError as e:
self.log_error("Could not connect to notify server: %s", str(e))
except Timeout as e:
self.log_error("Notify server timed out: %s", str(e))
except RequestException as e:
self.log_error("Failed to send notify payload %s", str(e))

def notify(self, payload):
if not self.url:
return

if self.session is None:
self.session = setup_requests_session(self.token)

threading.Thread(target=self.send, args=(payload,)).start()


def notify_init(app):
app.config.setdefault('NOTIFY_URL', os.environ.get('NOTIFY_URL'))
app.config.setdefault('NOTIFY_TOKEN', os.environ.get('NOTIFY_TOKEN'))

if not app.config['NOTIFY_URL']:
app.logger.warn("Skipping notifications, NOTIFY_URL missing in .env")
return

app.logger.info("Enabling notifications to URL %s", app.config['NOTIFY_URL'])
app.logger.info("Token found: %s", app.config['NOTIFY_TOKEN'] is not None)

app.extensions['notifier'] = NotifyService(
app.config['NOTIFY_URL'],
app.config['NOTIFY_TOKEN'],
app.logger,
)
30 changes: 28 additions & 2 deletions openreferee_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
)


def notify(app, payload):
service = app.extensions.get('notifier')
if service is not None:
service.notify(payload)


def require_event_token(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -246,6 +252,15 @@ def replace_revision_files():
t.daemon = True
t.start()

notify(current_app, {
"event": event.identifier,
"contrib_id": contrib_id,
"action": "create",
"editable_type": editable_type,
"user": user,
"request": request.json
})

replace_revision_files()
return CreateEditableResponseSchema().dump(resp), 201

Expand Down Expand Up @@ -285,6 +300,17 @@ def review_editable(
"A new revision %r was submitted for contribution %r", revision_id, contrib_id
)
resp = {}

notify(current_app, {
"event": event.identifier,
"contrib_id": contrib_id,
"revision_id": revision_id,
"action": action,
"editable_type": editable_type,
"user": user,
"request": request.json
})

if action in {'accept', 'update_accept'}:
resp = process_accepted_revision(event, revision)

Expand Down Expand Up @@ -317,7 +343,7 @@ def remove_editable(event, contrib_id, editable_type):


@api.route(
"/event/<identifier>/editable/<any(paper,slides,poster):editable_type>/<contrib_id>/<revision_id>/actions", # noqa: E501
"/event/<identifier>/editable/<any(paper,slides,poster):editable_type>/<contrib_id>/<revision_id>/actions",
methods=("POST",),
)
@use_kwargs(ServiceActionsRequestSchema(unknown=EXCLUDE), location="json")
Expand Down Expand Up @@ -360,7 +386,7 @@ def get_custom_revision_actions(


@api.route(
"/event/<identifier>/editable/<any(paper,slides,poster):editable_type>/<contrib_id>/<revision_id>/action", # noqa: E501
"/event/<identifier>/editable/<any(paper,slides,poster):editable_type>/<contrib_id>/<revision_id>/action",
methods=("POST",),
)
@use_kwargs(ServiceTriggerActionRequestSchema(unknown=EXCLUDE), location="json")
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from setuptools import setup


setup()

0 comments on commit 930364e

Please sign in to comment.