I am currently trying to use the aiohttp framework inside of the connexion package, which works really good. The one problem i've got is to catch a request (from server side) inside of the endpoint logic, so I can read the header from the request.
The request is available in connexion.decorators.security, but how can i catch it in the endpoint logic!
I appreciate any help, as i've been working on this issue for hours now.
Flask's "request" worked perfectly fine inside of my endpoint logic, I assume it has something to do, with me switching to "async def".
aiohttp.web.Request.headers just returns an object and not really the content of a request.
This is how my logic is build and everything seems to work fine, except the catching of the "request".
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()
Output of the commands:
You can use pass_context_arg_name in add_api arguments:
webapp.add_api('swagger.yaml', base_path='/cs', pass_context_arg_name='request')
It defines the name of a request argument that will be added in your endpoints:
async def endpoint(user_input, request):
user_token = request.get['Authorization'] #**(something like that)**
return web.Response(text=f"--> {user_input}")
Perfect - that's it. Thanks!
Does this also work for x-apikeyInfoFunc and its other auth function equivalents?
Not yet. Pull request #869 is a proposal to allow it.
Most helpful comment
You can use
pass_context_arg_nameinadd_apiarguments:It defines the name of a request argument that will be added in your endpoints: