Skip to content

Instantly share code, notes, and snippets.

@Morabaraba
Created September 25, 2018 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Morabaraba/29102a86ee182ae4d1d6d61b3c6526a9 to your computer and use it in GitHub Desktop.
Save Morabaraba/29102a86ee182ae4d1d6d61b3c6526a9 to your computer and use it in GitHub Desktop.
modified `hello.py` [connexion](https://github.com/zalando/connexion/) example to include `Accept` header validation decorator.
#!/usr/bin/env python3
import connexion
import flask
from decorator import decorator
@decorator
def requires_header(f: callable, *args, **kwargs):
accept = flask.request.headers.get('Accept')
print(accept)
if not accept or accept != 'application/xml':
return '400', 400
return f(*args, **kwargs)
def post_greeting(name: str) -> str:
return 'Hello {name}'.format(name=name)
@requires_header
def get_user() -> str:
return 'got user'
if __name__ == '__main__':
app = connexion.FlaskApp(__name__, port=8080, specification_dir='swagger/')
app.add_api('helloworld-api.yaml', arguments={'title': 'Hello World Example'} ) #, validate_responses=True)# strict_validation=True)
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment