forked from NiklasRosenstein/python-github-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask.py
More file actions
60 lines (46 loc) · 1.5 KB
/
flask.py
File metadata and controls
60 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Flask binding for handling GitHub webhook events.
Note that you need to install the `flask` module separately.
# Example
```python
from github_bot_api import Event, Webhook
from github_bot_api.flask import create_flask_app
def on_any_event(event: Event) -> bool:
print(event)
return True
webhook = Webhook(secret=None)
webhook.listen('*', on_any_event)
import os; os.environ['FLASK_ENV'] = 'development'
flask_app = create_flask_app(__name__, webhook)
flask_app.run()
```
"""
import flask
import typing as t
from .event import accept_event
from .webhook import Webhook
def create_event_handler(webhook: Webhook) -> t.Callable[[], t.Tuple[t.Text, int, t.Dict[str, str]]]:
"""
Creates an event handler flask view that interprets the received HTTP request as a GitHub application
event and dispatches it via #webhook.dispatch().
"""
def event_handler():
event = accept_event(
t.cast(t.Mapping[str, str], flask.request.headers),
flask.request.get_data(),
webhook.secret)
webhook.dispatch(event)
return '', 202, {}
return event_handler
def create_flask_app(
name: str,
webhook: Webhook,
path: str = '/event-handler',
) -> flask.Flask:
"""
Creates a new #flask.Flask application with a `POST` event handler under the given *path* (defaulting
to `/event-handler`). This is a useful shorthand to attach your #Webhook to an HTTP server.
"""
flask_app = flask.Flask(name)
flask_app.route(path, methods=['POST'])(create_event_handler(webhook))
return flask_app