-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ build/ | |
.eggs/ | ||
dist/ | ||
*.egg-info/ | ||
htmlcov/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
import logging | ||
from queue import Queue | ||
|
||
import cherrypy | ||
import isodate | ||
from cloudevents.exceptions import GenericException as CloudEventException | ||
from cloudevents.http import from_http | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_RABE_CLOUD_EVENTS_SUBS = ( | ||
"ch.rabe.api.events.track.v1.trackStarted", | ||
"ch.rabe.api.events.track.v1.trackFinished", | ||
) | ||
|
||
|
||
class ApiServer: | ||
"""The API server.""" | ||
|
||
def __init__(self, event_queue: Queue): | ||
self.event_queue = event_queue | ||
|
||
@cherrypy.expose | ||
@cherrypy.tools.json_in( | ||
content_type=("application/json", "application/cloudevents+json") | ||
) | ||
def webhook(self): | ||
"""Receive a CloudEvent and put it into the event queue.""" | ||
try: | ||
event = from_http( | ||
cherrypy.request.headers, json.dumps(cherrypy.request.json) | ||
) | ||
except CloudEventException as error: | ||
cherrypy.response.status = f"400 {error}" | ||
return | ||
|
||
logger.info("Received event: %s", event) | ||
|
||
if event["time"]: | ||
event["time"] = isodate.parse_datetime(event["time"]) | ||
if event["type"] in _RABE_CLOUD_EVENTS_SUBS: | ||
self.event_queue.put(event) | ||
|
||
cherrypy.response.status = "204 Event Received" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ isodate==0.6.1 | |
pylast==4.4.0 | ||
lxml==4.7.1 | ||
requests==2.26.0 | ||
cherrypy==18.6.1 | ||
cloudevents==1.2.0 |