Skip to content

Commit 44bb4c2

Browse files
feat: Add flask-restx support (#48)
1 parent 717a12b commit 44bb4c2

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ matrix:
1313
install:
1414
# werkzeug 1.0 breaks some parts of Flask ecosystem (see https://github.com/pallets/flask/pull/3488),
1515
# so let's use older versions at least for now.
16-
- pip install coveralls flake8 nose flask flask_restful flask_restplus injector flask_sqlalchemy eventlet typing "werkzeug<1.0"
16+
- pip install coveralls flake8 nose flask flask_restful flask_restplus flask_restx injector flask_sqlalchemy eventlet typing "werkzeug<1.0"
1717
# mypy can't be installed on pypy
1818
- if [[ "${TRAVIS_PYTHON_VERSION}" != "pypy"* ]] ; then pip install mypy typed_ast ; fi
1919
- if [[ "${TRAVIS_PYTHON_VERSION}" != "pypy"* && "${TRAVIS_PYTHON_VERSION}" != "3.5"* ]] ; then pip install black ; fi

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Flask-Injector lets you inject dependencies into:
4040
* Jinja environment globals (functions in `app.jinja_env.globals`)
4141
* Flask-RESTFul Resource constructors
4242
* Flask-RestPlus Resource constructors
43+
* Flask-RESTX Resource constructors
4344

4445
Flask-Injector supports defining types using function annotations (Python 3),
4546
see below.

flask_injector/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
from flask_restplus.utils import unpack as flask_response_unpack # noqa
2525
except ImportError:
2626
flask_restplus = None
27+
#
28+
try:
29+
import flask_restx
30+
from flask_restx.utils import unpack as flask_response_unpack # noqa
31+
except ImportError:
32+
flask_restx = None
2733

2834
from injector import Binder, Injector, inject
2935
from flask import Config, Request
@@ -113,7 +119,11 @@ def wrap_class_based_view(fun: Callable, injector: Injector) -> Callable:
113119
class_args = fun_closure.get('class_args')
114120
assert not class_args, 'Class args are not supported, use kwargs instead'
115121

116-
if flask_restful_api and flask_restplus and isinstance(flask_restful_api, flask_restplus.Api):
122+
if (
123+
flask_restful_api
124+
and (flask_restplus and isinstance(flask_restful_api, flask_restplus.Api))
125+
or (flask_restx and isinstance(flask_restful_api, flask_restx.Api))
126+
):
117127
# This is flask_restplus' add_resource implementation:
118128
#
119129
# def add_resource(self, resource, *urls, **kwargs):

flask_injector/tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import flask_restful
88
import flask_restplus
9+
import flask_restx
910
from eventlet import greenthread
1011
from injector import __version__ as injector_version, CallableProvider, inject, Scope
1112
from flask import Blueprint, Flask
@@ -335,6 +336,30 @@ def get(self):
335336
eq_(data, {'int': 0})
336337

337338

339+
def test_flask_restx_integration_works():
340+
app = Flask(__name__)
341+
api = flask_restx.Api(app)
342+
343+
class HelloWorld(flask_restx.Resource):
344+
@inject
345+
def __init__(self, *args, int: int, **kwargs):
346+
self._int = int
347+
super().__init__(*args, **kwargs)
348+
349+
@api.doc()
350+
def get(self):
351+
return {'int': self._int}
352+
353+
api.add_resource(HelloWorld, '/hello')
354+
355+
FlaskInjector(app=app)
356+
357+
client = app.test_client()
358+
response = client.get('/hello')
359+
data = json.loads(response.data.decode('utf-8'))
360+
eq_(data, {'int': 0})
361+
362+
338363
def test_request_scope_not_started_before_any_request_made_regression():
339364
# Version 0.6.1 (patch cacaef6 specifially) broke backwards compatibility in
340365
# a relatively subtle way. The code used to support RequestScope even in

0 commit comments

Comments
 (0)