当前位置: 首页>>代码示例>>Python>>正文


Python Api.add_resource方法代码示例

本文整理汇总了Python中flask_restplus.Api.add_resource方法的典型用法代码示例。如果您正苦于以下问题:Python Api.add_resource方法的具体用法?Python Api.add_resource怎么用?Python Api.add_resource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在flask_restplus.Api的用法示例。


在下文中一共展示了Api.add_resource方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_will_prettyprint_json_in_debug_mode

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_will_prettyprint_json_in_debug_mode(self):
        self.app.config["DEBUG"] = True
        api = Api(self.app)

        class Foo1(Resource):
            def get(self):
                return {"foo": "bar", "baz": "asdf"}

        api.add_resource(Foo1, "/foo", endpoint="bar")

        with self.app.test_client() as client:
            foo = client.get("/foo")

            # Python's dictionaries have random order (as of "new" Pythons,
            # anyway), so we can't verify the actual output here.  We just
            # assert that they're properly prettyprinted.
            lines = foo.data.splitlines()
            lines = [line.decode() for line in lines]
            self.assertEquals("{", lines[0])
            self.assertTrue(lines[1].startswith("    "))
            self.assertTrue(lines[2].startswith("    "))
            self.assertEquals("}", lines[3])

            # Assert our trailing newline.
            self.assertTrue(foo.data.endswith(b"\n"))
开发者ID:kenhancoder,项目名称:flask-restplus,代码行数:27,代码来源:test_marshalling.py

示例2: test_no_crossdomain

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_no_crossdomain(self, app, client):
        class Foo(Resource):
            def get(self):
                return "data"

        api = Api(app)
        api.add_resource(Foo, '/test/')

        res = client.get('/test/')
        assert res.status_code == 200
        assert 'Access-Control-Allow-Origin' not in res.headers
        assert 'Access-Control-Allow-Methods' not in res.headers
        assert 'Access-Control-Max-Age' not in res.headers
开发者ID:bedge,项目名称:flask-restplus,代码行数:15,代码来源:test_cors.py

示例3: test_no_crossdomain

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_no_crossdomain(self):
        class Foo(Resource):
            def get(self):
                return "data"

        api = Api(self.app)
        api.add_resource(Foo, '/test/')

        res = self.get('/test/')
        self.assertEqual(res.status_code, 200)
        self.assertNotIn('Access-Control-Allow-Origin', res.headers)
        self.assertNotIn('Access-Control-Allow-Methods', res.headers)
        self.assertNotIn('Access-Control-Max-Age', res.headers)
开发者ID:Dlotan,项目名称:flask-restplus,代码行数:15,代码来源:test_cors.py

示例4: test_access_control_expose_headers

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_access_control_expose_headers(self, app, client):
        class Foo(Resource):
            @cors.crossdomain(origin='*',
                              expose_headers=['X-My-Header', 'X-Another-Header'])
            def get(self):
                return "data"

        api = Api(app)
        api.add_resource(Foo, '/test/')

        res = client.get('/test/')
        assert res.status_code == 200
        assert 'X-MY-HEADER' in res.headers['Access-Control-Expose-Headers']
        assert 'X-ANOTHER-HEADER' in res.headers['Access-Control-Expose-Headers']
开发者ID:bedge,项目名称:flask-restplus,代码行数:16,代码来源:test_cors.py

示例5: test_json_float_marshalled

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_json_float_marshalled(self, app, client):
        api = Api(app)

        class FooResource(Resource):
            fields = {'foo': fields.Float}

            def get(self):
                return marshal({"foo": 3.0}, self.fields)

        api.add_resource(FooResource, '/api')

        resp = client.get('/api')
        assert resp.status_code == 200
        assert resp.data.decode('utf-8') == '{"foo": 3.0}\n'
开发者ID:hivelearning,项目名称:flask-restplus,代码行数:16,代码来源:test_marshalling.py

示例6: test_access_control_expose_headers

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_access_control_expose_headers(self):
        class Foo(Resource):
            @cors.crossdomain(origin='*',
                              expose_headers=['X-My-Header', 'X-Another-Header'])
            def get(self):
                return "data"

        api = Api(self.app)
        api.add_resource(Foo, '/test/')

        res = self.get('/test/')
        self.assertEqual(res.status_code, 200)
        self.assertIn('X-MY-HEADER', res.headers['Access-Control-Expose-Headers'])
        self.assertIn('X-ANOTHER-HEADER', res.headers['Access-Control-Expose-Headers'])
开发者ID:Dlotan,项目名称:flask-restplus,代码行数:16,代码来源:test_cors.py

示例7: test_json_float_marshalled

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_json_float_marshalled(self):
        api = Api(self.app)

        class FooResource(Resource):
            fields = {"foo": fields.Float}

            def get(self):
                return marshal({"foo": 3.0}, self.fields)

        api.add_resource(FooResource, "/api")

        app = self.app.test_client()
        resp = app.get("/api")
        self.assertEquals(resp.status_code, 200)
        self.assertEquals(resp.data.decode("utf-8"), '{"foo": 3.0}\n')
开发者ID:kenhancoder,项目名称:flask-restplus,代码行数:17,代码来源:test_marshalling.py

示例8: test_crossdomain

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_crossdomain(self, app, client):
        class Foo(Resource):
            @cors.crossdomain(origin='*')
            def get(self):
                return "data"

        api = Api(app)
        api.add_resource(Foo, '/test/')

        res = client.get('/test/')
        assert res.status_code == 200
        assert res.headers['Access-Control-Allow-Origin'] == '*'
        assert res.headers['Access-Control-Max-Age'] == '21600'
        assert 'HEAD' in res.headers['Access-Control-Allow-Methods']
        assert 'OPTIONS' in res.headers['Access-Control-Allow-Methods']
        assert 'GET' in res.headers['Access-Control-Allow-Methods']
开发者ID:bedge,项目名称:flask-restplus,代码行数:18,代码来源:test_cors.py

示例9: test_crossdomain

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_crossdomain(self):
        class Foo(Resource):
            @cors.crossdomain(origin='*')
            def get(self):
                return "data"

        api = Api(self.app)
        api.add_resource(Foo, '/test/')

        res = self.get('/test/')
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.headers['Access-Control-Allow-Origin'], '*')
        self.assertEqual(res.headers['Access-Control-Max-Age'], '21600')
        self.assertIn('HEAD', res.headers['Access-Control-Allow-Methods'])
        self.assertIn('OPTIONS', res.headers['Access-Control-Allow-Methods'])
        self.assertIn('GET', res.headers['Access-Control-Allow-Methods'])
开发者ID:Dlotan,项目名称:flask-restplus,代码行数:18,代码来源:test_cors.py

示例10: test_will_prettyprint_json_in_debug_mode

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
    def test_will_prettyprint_json_in_debug_mode(self, app, client):
        api = Api(app)

        class Foo1(Resource):
            def get(self):
                return {'foo': 'bar', 'baz': 'asdf'}

        api.add_resource(Foo1, '/foo', endpoint='bar')

        foo = client.get('/foo')

        # Python's dictionaries have random order (as of "new" Pythons,
        # anyway), so we can't verify the actual output here.  We just
        # assert that they're properly prettyprinted.
        lines = foo.data.splitlines()
        lines = [line.decode() for line in lines]
        assert "{" == lines[0]
        assert lines[1].startswith('    ')
        assert lines[2].startswith('    ')
        assert "}" == lines[3]

        # Assert our trailing newline.
        assert foo.data.endswith(b'\n')
开发者ID:hivelearning,项目名称:flask-restplus,代码行数:25,代码来源:test_marshalling.py

示例11: abort

# 需要导入模块: from flask_restplus import Api [as 别名]
# 或者: from flask_restplus.Api import add_resource [as 别名]
            abort(404)
        return data


# Blueprint is needed to avoid swagger ui static assets 404 error
app = Flask(__name__)
# register swagger ui static assets to avoid 404 error when use nginx 
# as reverse proxy server with sub path
app.register_blueprint(apidoc.apidoc, url_prefix='/api/starhistory/1.0')

blueprint = Blueprint('starhistory', __name__,
                      url_prefix='/api/starhistory/1.0')
api = Api(blueprint, version='1.0', title='Github star history api',
          description='API for getting star history',
          doc='/doc/')
api.add_resource(StarHistoryAPI, '/<user>/<repo>/',
                 endpoint='starhistory')
app.register_blueprint(blueprint)


@app.after_request
def after_request(response):
    """
    Handle CORS Requests
    """
    # TODO remove CORS support after blog using domain?
    response.headers.add('Access-Control-Allow-Origin', '*')
    return response


if __name__ == '__main__':
    app.run()
开发者ID:gas1121,项目名称:py-star-history,代码行数:34,代码来源:run.py


注:本文中的flask_restplus.Api.add_resource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。