Skip to content

Commit 3378636

Browse files
committed
Allow custom resource constructor args
1 parent b44bc08 commit 3378636

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

flask_rest_jsonapi/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ def route(self, resource, view, *urls, **kwargs):
6969
resource.view = view
7070
url_rule_options = kwargs.get('url_rule_options') or dict()
7171

72-
view_func = resource.as_view(view)
72+
# Allow the customization of the resource class instance
73+
resource_args = kwargs.get('resource_args', [])
74+
resource_kwargs = kwargs.get('resource_kwargs', {})
75+
76+
view_func = resource.as_view(view, *resource_args, **resource_kwargs)
7377

7478
if 'blueprint' in kwargs:
7579
resource.view = '.'.join([kwargs['blueprint'].name, resource.view])

flask_rest_jsonapi/resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __new__(cls, name, bases, d):
5252
class Resource(MethodView):
5353
"""Base resource class"""
5454

55-
def __new__(cls):
55+
def __new__(cls, *args, **kwargs):
5656
"""Constructor of a resource instance"""
5757
if hasattr(cls, '_data_layer'):
5858
cls._data_layer.resource = cls

tests/test_sqlalchemy_data_layer.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer
2020
from flask_rest_jsonapi.data_layers.base import BaseDataLayer
2121
from flask_rest_jsonapi.data_layers.filtering.alchemy import Node
22+
from flask_rest_jsonapi.decorators import check_headers, check_method_requirements, jsonapi_exception_formatter
2223
import flask_rest_jsonapi.decorators
2324
import flask_rest_jsonapi.resource
2425
import flask_rest_jsonapi.schema
@@ -983,6 +984,46 @@ def test_get_list_response(client, register_routes):
983984
assert response.status_code == 200
984985

985986

987+
class TestResourceArgs:
988+
def test_resource_args(self, app):
989+
class TestResource(ResourceDetail):
990+
"""
991+
This fake resource always renders a constructor parameter
992+
"""
993+
def __init__(self, *args, **kwargs):
994+
super().__init__()
995+
self.constant = args[0]
996+
997+
def get(self):
998+
return self.constant
999+
api = Api(app=app)
1000+
api.route(TestResource, 'resource_args', '/resource_args', resource_args=['hello!'])
1001+
api.init_app()
1002+
with app.test_client() as client:
1003+
rv = client.get('/resource_args')
1004+
assert rv.json == 'hello!'
1005+
1006+
def test_resource_kwargs(self, app):
1007+
class TestResource(ResourceDetail):
1008+
"""
1009+
This fake resource always renders a constructor parameter
1010+
"""
1011+
def __init__(self, *args, **kwargs):
1012+
super().__init__()
1013+
self.constant = kwargs.get('constant')
1014+
1015+
def get(self):
1016+
return self.constant
1017+
api = Api(app=app)
1018+
api.route(TestResource, 'resource_kwargs', '/resource_kwargs', resource_kwargs={
1019+
'constant': 'hello!'
1020+
})
1021+
api.init_app()
1022+
with app.test_client() as client:
1023+
rv = client.get('/resource_kwargs')
1024+
assert rv.json == 'hello!'
1025+
1026+
9861027
# test various Accept headers
9871028
def test_single_accept_header(client, register_routes):
9881029
with client:

0 commit comments

Comments
 (0)