Python flask_restx.Resource() Examples

The following are 22 code examples of flask_restx.Resource(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_restx , or try the search function .
Example #1
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_partial_param_for_schema_loading(app, client):  # noqa
    class TestSchema(Schema):
        foo = fields.Str(required=True)
        bar = fields.Str(required=True)

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(schema=TestSchema(), api=api, partial=True)
        def post(self):
            return "success"

    with client as cl:
        resp = cl.post("/test", json={"foo": 'foo', "bar": "bar"})
        assert resp.status_code == 200
        resp = cl.post("/test", json={"foo": 'foo'})
        assert resp.status_code == 200 
Example #2
Source File: marshmallow_example.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_app(env=None):
    from flask_restx import Api, Namespace, Resource

    app = Flask(__name__)
    api = Api(app)

    @app.route("/simple/make_a_widget", methods=["POST"])
    @accepts(dict(name="some_arg", type=str), schema=WidgetSchema)
    @responds(schema=WidgetSchema)
    def post():
        from flask import jsonify

        return request.parsed_obj

    @api.route("/restx/make_a_widget")
    class WidgetResource(Resource):
        @accepts(dict(name="some_arg", type=str), schema=WidgetSchema, api=api)
        @responds(schema=WidgetSchema, api=api)
        def post(self):
            from flask import jsonify

            return request.parsed_obj

    return app 
Example #3
Source File: dates_example.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_app(env=None):
    from flask_restx import Api, Namespace, Resource

    app = Flask(__name__)
    api = Api(app)

    @api.route("/restx/make_a_widget")
    class DoodadResource(Resource):
        @accepts(schema=DoodadSchema, api=api)
        @responds(schema=DoodadSchema, api=api)
        def post(self):
            from flask import jsonify

            return request.parsed_obj

    return app 
Example #4
Source File: schema_with_excluded_fields.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_app():
    app = Flask(__name__)
    api = Api(app)

    @api.route("/widget")
    class WidgetResource(Resource):
        @accepts("WidgetCreate", schema=WidgetSchema(only=["foo", "baz"]), api=api)
        @responds(schema=WidgetSchema, api=api)
        def post(self):
            # save data to a new record, and return ID
            return request.parsed_obj

        @accepts(dict(name="id", type=int), schema=WidgetSchema, api=api)
        @responds(schema=WidgetSchema(exclude=["created_at"]), api=api)
        def patch(self):
            return request.parsed_obj

    return app 
Example #5
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_responds_respects_envelope(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema, api=api, envelope='test-data')
        def get(self):
            from flask import make_response, Response

            obj = {"_id": 42, "name": "Jon Snow"}
            return obj

    with client as cl:
        resp = cl.get("/test")
        assert resp.status_code == 200
        assert resp.json == {'test-data': {'_id': 42, 'name': 'Jon Snow'}} 
Example #6
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_responds_respects_status_code(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema, api=api, status_code=999)
        def get(self):
            from flask import make_response, Response

            obj = {"_id": 42, "name": "Jon Snow"}
            return obj

    with client as cl:
        resp = cl.get("/test")
        assert resp.status_code == 999 
Example #7
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_arguments_are_added_to_request_with_Resource_and_schema(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(
            "Foo",
            dict(name="foo", type=int, help="An important foo"),
            schema=TestSchema,
            api=api,
        )
        def post(self):
            assert request.parsed_obj
            assert request.parsed_obj["_id"] == 42
            assert request.parsed_obj["name"] == "test name"
            return "success"

    with client as cl:
        resp = cl.post("/test?foo=3", json={"_id": 42, "name": "test name"})
        assert resp.status_code == 200 
Example #8
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_responds_passes_raw_responses_through_untouched(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema, api=api)
        def get(self):
            from flask import make_response, Response

            obj = {"_id": 42, "name": "Jon Snow"}
            return Response("A prebuild response that won't be serialised", 201)

    with client as cl:
        resp = cl.get("/test")
        assert resp.status_code == 201 
Example #9
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_respond_schema_instance_respects_many(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema(many=True), api=api)
        def get(self):
            obj = [{"_id": 42, "name": "Jon Snow"}]
            return obj

    with client as cl:
        resp = cl.get("/test")
        obj = resp.json
        assert obj == [{"_id": 42, "name": "Jon Snow"}] 
Example #10
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_respond_schema_respects_many(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema, many=True, api=api)
        def get(self):
            obj = [{"_id": 42, "name": "Jon Snow"}]
            return obj

    with client as cl:
        resp = cl.get("/test")
        obj = resp.json
        assert obj == [{"_id": 42, "name": "Jon Snow"}] 
Example #11
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_respond_schema_instance(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema(), api=api)
        def get(self):
            obj = {"_id": 42, "name": "Jon Snow"}
            return obj

    with client as cl:
        resp = cl.get("/test")
        obj = resp.json
        assert obj["_id"] == 42
        assert obj["name"] == "Jon Snow" 
Example #12
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_respond_schema_instance_respects_exclude(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @responds(schema=TestSchema(exclude=("_id",)), api=api)
        def get(self):
            obj = {"_id": 42, "name": "Jon Snow"}
            return obj

    with client as cl:
        resp = cl.get("/test")
        obj = resp.json
        assert "_id" not in obj
        assert obj["name"] == "Jon Snow" 
Example #13
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_validation_errors_from_all_added_to_request_with_Resource_and_schema(
    app, client
):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(
            "Foo",
            dict(name="foo", type=int, help="An important foo"),
            dict(name="foo2", type=int, help="An important foo2"),
            schema=TestSchema,
            api=api,
        )
        def post(self):
            pass  # pragma: no cover

    with client as cl:
        resp = cl.post(
            "/test?foo=not_int",
            json={"_id": "this is not an integer and will error", "name": "test name"},
        )

        assert resp.status_code == 400
        assert "Not a valid integer." in resp.json["errors"]["schema_errors"]["_id"] 
Example #14
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_accepts_with_nested_schema(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    class HostSchema(Schema):
        name = fields.String()
        child = fields.Nested(TestSchema)

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(
            "Foo",
            dict(name="foo", type=int, help="An important foo"),
            schema=HostSchema,
            api=api,
        )
        def post(self):
            assert request.parsed_obj
            assert request.parsed_obj["child"] == {"_id": 42, "name": "test name"}
            assert request.parsed_obj["name"] == "test host"
            return "success"

    with client as cl:
        resp = cl.post(
            "/test?foo=3",
            json={"name": "test host", "child": {"_id": 42, "name": "test name"}},
        )
        assert resp.status_code == 200 
Example #15
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_validation_errors_added_to_request_with_Resource_and_schema(
    app, client
):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(
            "Foo",
            dict(name="foo", type=int, help="An important foo"),
            schema=TestSchema,
            api=api,
        )
        def post(self):
            pass  # pragma: no cover

    with client as cl:
        resp = cl.post(
            "/test?foo=3",
            json={"_id": "this is not an integer and will error", "name": "test name"},
        )
        assert resp.status_code == 400
        assert "Not a valid integer." in resp.json["schema_errors"]["_id"] 
Example #16
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_arguments_are_added_to_request_with_Resource_and_schema_instance(
    app, client
):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(
            "Foo",
            dict(name="foo", type=int, help="An important foo"),
            schema=TestSchema(),
            api=api,
        )
        def post(self):
            assert request.parsed_obj
            assert request.parsed_obj["_id"] == 42
            assert request.parsed_obj["name"] == "test name"
            return "success"

    with client as cl:
        resp = cl.post("/test?foo=3", json={"_id": 42, "name": "test name"})
        assert resp.status_code == 200 
Example #17
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_arguments_are_added_to_request_with_Resource(app, client):  # noqa
    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts("Foo", dict(name="foo", type=int, help="An important foo"), api=api)
        def get(self):
            assert request.parsed_args
            return "success"

    with client as cl:
        resp = cl.get("/test?foo=3")
        assert resp.status_code == 200 
Example #18
Source File: marshmallow_validator_example.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_app(env=None):
    from flask_restx import Api, Namespace, Resource

    app = Flask(__name__)
    api = Api(app)

    @api.route("/restx/make_a_widget")
    class WidgetResource(Resource):
        @accepts(schema=WidgetSchema, api=api)
        @responds(schema=WidgetSchema, api=api)
        def post(self):
            from flask import jsonify

            return request.parsed_obj

    return app 
Example #19
Source File: load_dump_only.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_app():
    app = Flask(__name__)
    api = Api(app)

    @api.route("/widget")
    class WidgetResource(Resource):
        @accepts(schema=WidgetSchema, api=api)
        @responds(schema=WidgetSchema, api=api)
        def post(self):
            # save data to a new record, and return ID
            return request.parsed_obj

    return app 
Example #20
Source File: server.py    From prometheus_flask_exporter with MIT License 5 votes vote down vote up
def setup_api(app):
    api = Api()
    api.init_app(app)

    @api.route('/test')
    class ExampleEndpoint(Resource):
        def get(self):
            return {'hello': 'world'} 
Example #21
Source File: apiserver.py    From oxfs with MIT License 4 votes vote down vote up
def start_service(self, port):
        apiserver = self
        self.app = Flask(__name__)
        self.app.wsgi_app = ProxyFix(self.app.wsgi_app)
        self.api = Api(self.app, version='1.0', title='Oxfs Api',
                       description='The Oxfs Api')

        # Response model
        fs_namespace = self.api.namespace('fs', description='fs operations')
        status_model = self.api.model(
            'Status',
            {
                'status': fields.Boolean,
                'data': fields.String
            })

        # Request arguments
        string_args = self.api.parser()
        string_args.add_argument('path', required=True, help='absolute path')

        # Api
        @fs_namespace.route('/reload')
        @fs_namespace.expect(string_args)
        class Reload(Resource):
            @fs_namespace.marshal_with(status_model, envelope='data')
            def post(self):
                args = string_args.parse_args()
                path = apiserver.oxfs_fuse.remotepath(args['path'])
                status = (apiserver.cleanf(path), apiserver.cleand(path))
                return {'status': False not in status, 'data': path}

        @fs_namespace.route('/clear')
        class Clear(Resource):
            @fs_namespace.marshal_with(status_model, envelope='data')
            def delete(self):
                status = apiserver.clear()
                return {'status': True, 'data': 'success'}

        @fs_namespace.route('/directories')
        @fs_namespace.expect(string_args)
        class Directories(Resource):
            @fs_namespace.marshal_with(status_model, envelope='data')
            def get(self):
                args = string_args.parse_args()
                path = apiserver.oxfs_fuse.remotepath(args['path'])
                status, data = apiserver.fetchd(path)
                return {'status': status, 'data': data}

        self.set_flask_env()
        self.app.run(port=port, debug=False) 
Example #22
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_accepts_with_twice_nested_schema(app, client):  # noqa
    class TestSchema(Schema):
        _id = fields.Integer()
        name = fields.String()

    class HostSchema(Schema):
        name = fields.String()
        child = fields.Nested(TestSchema)

    class HostHostSchema(Schema):
        name = fields.String()
        child = fields.Nested(HostSchema)

    api = Api(app)

    @api.route("/test")
    class TestResource(Resource):
        @accepts(
            "Foo",
            dict(name="foo", type=int, help="An important foo"),
            schema=HostHostSchema,
            api=api,
        )
        def post(self):
            assert request.parsed_obj
            assert request.parsed_obj["child"]["child"] == {
                "_id": 42,
                "name": "test name",
            }
            assert request.parsed_obj["child"] == {
                "name": "test host",
                "child": {"_id": 42, "name": "test name"},
            }
            assert request.parsed_obj["name"] == "test host host"
            return "success"

    with client as cl:
        resp = cl.post(
            "/test?foo=3",
            json={
                "name": "test host host",
                "child": {
                    "name": "test host",
                    "child": {"_id": 42, "name": "test name"},
                },
            },
        )
        assert resp.status_code == 200