spec-first / connexion

Connexion is a modern Python web framework that makes spec-first and api-first development easy.
https://connexion.readthedocs.io/en/latest/
Apache License 2.0
4.48k stars 762 forks source link

How to pass an argument to an endpoint #1355

Closed Choo57 closed 3 years ago

Choo57 commented 3 years ago

Description

I can see my custom headers with connexion.request.headers['X-MyHeader'] when I place it directly in my app.py (shown below) in the def home() function, but I could not find a way to pass them to my endpoint "users" which is on a separate file and is referenced through the swagger.yml file's operationId: "users.getUsers".

I have been looking for a way to pass the value of connexion.request.headers['X-MyHeader'] to my users.getUsers function but no luck so far. This one seemed close enough https://github.com/zalando/connexion/issues/1210 but looks like it is for aiohttp, while I am currently testing on Flask.

app.py

`from flask import render_template import connexion

class ReverseProxied(object): ''' https://github.com/zalando/connexion/blob/master/examples/openapi3/reverseproxy/app.py '''

def __init__(self, app, script_name=None, scheme=None, server=None):
    self.app = app
    self.script_name = script_name
    self.scheme = scheme
    self.server = server

def __call__(self, environ, start_response):
    script_name = environ.get('HTTP_X_FORWARDED_PATH', '') or self.script_name
    if script_name:
        environ['SCRIPT_NAME'] = "/" + script_name.lstrip("/")
        path_info = environ['PATH_INFO']
        if path_info.startswith(script_name):
            environ['PATH_INFO_OLD'] = path_info
            environ['PATH_INFO'] = path_info[len(script_name):]
    scheme = environ.get('HTTP_X_SCHEME', '') or self.scheme
    if scheme:
        environ['wsgi.url_scheme'] = scheme
    server = environ.get('HTTP_X_FORWARDED_SERVER', '') or self.server
    if server:
        environ['HTTP_HOST'] = server
    return self.app(environ, start_response)

if name == 'main': app = connexion.FlaskApp(name, specification_dir='./swagger/') app.add_api('swagger.yml') flask_app = app.app proxied = ReverseProxied( flask_app.wsgi_app, script_name='/api/' ) flask_app.wsgi_app = proxied

# Create a URL route in our application for "/"
@app.route('/')
def home():
    """
    This function just responds to the browser ULR
    localhost:5000/
    :return:        the rendered template 'home.html'
    """
    print(connexion.request.headers['X-MyHeader'])
    return render_template('home.html')

app.run(host='0.0.0.0', port=5000, debug=True)`

Output of the commands:

RobbeSneyders commented 3 years ago

Hi @Choo57 could you please reformat the issue so it's more readable? And also provide your specification file swagger.yml.