Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加fastapi支持 #690

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions werobot/contrib/fastApiView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
import asyncio
from fastapi import Request,Response
from fastapi.responses import HTMLResponse
import html

def make_view(robot):
"""
为一个 BaseRoBot 生成 fastapi view。
用法:
app = FastAPI()
app.add_route('/werobot', make_view(robot), ['post'])
:param robot:
:return:
"""
def werobot_view(request: Request):
timestamp = request.query_params.get('timestamp')
nonce = request.query_params.get('nonce')
signature = request.query_params.get('signature')
if not robot.check_signature(
timestamp,
nonce,
signature,
):
return HTMLResponse(robot.make_error_page(html.escape(request.url.hostname)), 403)
message = robot.parse_message(
asyncio.run(request.body()),
timestamp=timestamp,
nonce=nonce,
msg_signature=request.query_params.get('msg_signature', '')
)
response = Response(robot.get_encrypted_reply(message))
response.headers['content_type'] = 'application/xml'
return response

return werobot_view