我目前正在尝试在 connexion 包中使用 aiohttp 框架,它的效果非常好。 我遇到的一个问题是在端点逻辑内部捕获请求(来自服务器端),因此我可以从请求中读取标头。
该请求在 connexion.decorators.security 中可用,但我如何在端点逻辑中捕获它!
我感谢任何帮助,因为我已经在这个问题上工作了几个小时。
Flask 的“请求”在我的端点逻辑中工作得很好,我认为它有一些事情要做,我切换到“异步定义”。
aiohttp.web.Request.headers 只返回一个对象,而不是真正的请求内容。
这就是我的逻辑构建方式,除了捕获“请求”之外,一切似乎都正常。
import logging
import connexion
import json
import query_segments
import socket
import tokens
import time
import json
import asyncio
import aiohttp
from aiohttp import web
async def endpoint(user_input):
user_token = request.get['Authorization'] #**(something like that)**
return web.Response(text=f"--> {user_input}")
logging.basicConfig(level=getattr(logging, 'INFO', None))
ip_address = socket.gethostbyname(socket.gethostname())
webapp = connexion.AioHttpApp(__name__,host=ip_address, port=4000, debug=False)
webapp.add_api('swagger.yaml', base_path='/cs')
application = webapp.app
if __name__ == '__main__':
webapp.run()
命令的输出:
您可以在add_api
参数中使用pass_context_arg_name
:
webapp.add_api('swagger.yaml', base_path='/cs', pass_context_arg_name='request')
它定义了将添加到端点中的请求参数的名称:
async def endpoint(user_input, request):
user_token = request.get['Authorization'] #**(something like that)**
return web.Response(text=f"--> {user_input}")
完美 - 就是这样。 谢谢!
这是否也适用于x-apikeyInfoFunc
及其其他 auth 函数等价物?
还没有。 拉取请求 #869 是一个允许它的提议。
最有用的评论
您可以在
add_api
参数中使用pass_context_arg_name
:它定义了将添加到端点中的请求参数的名称: