Skip to content

Commit

Permalink
Connexion request user (#435)
Browse files Browse the repository at this point in the history
* Add back connexion.request

* Refactor code to lifecycle module

* Test user from connexion.request instance
  • Loading branch information
rafaelcaricio authored and hjacobs committed Apr 7, 2017
1 parent a95b986 commit 63abcad
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 52 deletions.
7 changes: 5 additions & 2 deletions connexion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
try:
from .apis.flask_api import FlaskApi
from .apps.flask_app import FlaskApp
from flask import request # NOQA
except ImportError as e: # pragma: no cover
import sys
import six
Expand All @@ -17,8 +18,10 @@
def _required_lib(exec_info, *args, **kwargs):
six.reraise(*exec_info)

FlaskApi = functools.partial(_required_lib, sys.exc_info())
FlaskApp = functools.partial(_required_lib, sys.exc_info())
_flask_not_installed_error = functools.partial(_required_lib, sys.exc_info())

FlaskApi = _flask_not_installed_error
FlaskApp = _flask_not_installed_error

App = FlaskApp
Api = FlaskApi
Expand Down
3 changes: 1 addition & 2 deletions connexion/apis/flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from connexion.apis.abstract import AbstractAPI
from connexion.decorators.produces import BaseSerializer, NoContent
from connexion.handlers import AuthErrorHandler
from connexion.request import ConnexionRequest
from connexion.response import ConnexionResponse
from connexion.lifecycle import ConnexionRequest, ConnexionResponse
from connexion.utils import is_json_mimetype

logger = logging.getLogger('connexion.apis.flask_api')
Expand Down
5 changes: 1 addition & 4 deletions connexion/apps/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ def common_error_handler(exception):

response = problem(title=exception.name, detail=exception.description,
status=exception.code)
kwargs = {attr_name: getattr(response, attr_name) for attr_name in response._fields}
response = type(response)(**kwargs)

response = FlaskApi.get_response(response)
return response
return FlaskApi.get_response(response)

def add_api(self, specification, base_path=None, arguments=None,
auth_all_paths=None, swagger_json=None, swagger_ui=None,
Expand Down
2 changes: 1 addition & 1 deletion connexion/decorators/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import inflection
import six

from ..request import ConnexionRequest # NOQA
from ..lifecycle import ConnexionRequest # NOQA
from ..utils import all_json, boolean, is_null, is_nullable

try:
Expand Down
37 changes: 37 additions & 0 deletions connexion/lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

class ConnexionRequest(object):
def __init__(self,
url,
method,
path_params=None,
query=None,
headers=None,
form=None,
body=None,
json=None,
files=None,
context=None):
self.url = url
self.method = method
self.path_params = path_params or {}
self.query = query or {}
self.headers = headers or {}
self.form = form or {}
self.body = body
self.json = json
self.files = files
self.context = context or {}


class ConnexionResponse(object):
def __init__(self,
status_code=200,
mimetype=None,
content_type=None,
body=None,
headers=None):
self.status_code = status_code
self.mimetype = mimetype
self.content_type = content_type
self.body = body
self.headers = headers or {}
4 changes: 2 additions & 2 deletions connexion/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get_mimetype(self):
def _request_begin_lifecycle_decorator(self):
"""
Transforms the result of the operation handler in a internal
representation (connexion.request.ConnexionRequest) to be
representation (connexion.lifecycle.ConnexionRequest) to be
used by internal Connexion decorators.
:rtype: types.FunctionType
Expand All @@ -116,7 +116,7 @@ def _request_end_lifecycle_decorator(self):
"""
Guarantees that instead of the internal representation of the
operation handler response
(connexion.request.ConnexionRequest) a framework specific
(connexion.lifecycle.ConnexionRequest) a framework specific
object is returned.
:rtype: types.FunctionType
"""
Expand Down
2 changes: 1 addition & 1 deletion connexion/problem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .response import ConnexionResponse
from .lifecycle import ConnexionResponse


def problem(status, title, detail, type=None, instance=None, headers=None, ext=None):
Expand Down
23 changes: 0 additions & 23 deletions connexion/request.py

This file was deleted.

17 changes: 0 additions & 17 deletions connexion/response.py

This file was deleted.

4 changes: 4 additions & 0 deletions tests/api/test_secure_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def test_security(oauth_requests, secure_endpoint_app):
get_bye_from_flask = app_client.get('/v1.0/byesecure-from-flask', headers=headers) # type: flask.Response
assert get_bye_from_flask.data == b'Goodbye test-user (Secure!)'

headers = {"Authorization": "Bearer 100"}
get_bye_from_connexion = app_client.get('/v1.0/byesecure-from-connexion', headers=headers) # type: flask.Response
assert get_bye_from_connexion.data == b'Goodbye test-user (Secure!)'


def test_checking_that_client_token_has_all_necessary_scopes(
oauth_requests, secure_endpoint_app):
Expand Down
5 changes: 5 additions & 0 deletions tests/fakeapi/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import flask
from flask import jsonify, redirect

import connexion
from connexion import NoContent, ProblemException, problem


Expand Down Expand Up @@ -75,6 +76,10 @@ def get_bye_secure_from_flask():
return 'Goodbye {user} (Secure!)'.format(user=flask.request.user)


def get_bye_secure_from_connexion():
return 'Goodbye {user} (Secure!)'.format(user=connexion.request.user)


def get_bye_secure_ignoring_context(name):
return 'Goodbye {name} (Secure!)'.format(name=name)

Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/secure_endpoint/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ paths:
schema:
type: string

/byesecure-from-connexion:
get:
summary: Generate goodbye
description: ""
operationId: fakeapi.hello.get_bye_secure_from_connexion
security:
- oauth:
- myscope
produces:
- text/plain
responses:
200:
description: goodbye response
schema:
type: string

/byesecure-ignoring-context/{name}:
get:
summary: Generate goodbye
Expand Down

0 comments on commit 63abcad

Please sign in to comment.