Connexion: Request in aiohttp endpoint

Created on 19 Feb 2019  路  4Comments  路  Source: zalando/connexion

Description

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.

Expected behaviour

Flask's "request" worked perfectly fine inside of my endpoint logic, I assume it has something to do, with me switching to "async def".

Actual behaviour

aiohttp.web.Request.headers just returns an object and not really the content of a request.

Steps to reproduce

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()

Additional info:

Output of the commands:

  • Python version: 3.7
  • connexion = 2.2.0
aiohttp question

Most helpful comment

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}")

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings