python的封装restful api_Python restful.Api方法代码示例

# 需要导入模块: from flask.ext import restful [as 别名]

# 或者: from flask.ext.restful import Api [as 别名]

def lmio_api(dev=False, username=None, password=None):

r"""

Generate a Flask App that will serve meshes landmarks and templates to

landmarker.io

Parameters

----------

adapter: :class:`LandmarkerIOAdapter`

Concrete implementation of the LandmarkerIOAdapter. Will be queried for

all data to pass to landmarker.io.

dev: `bool`, optional

If True, listen to anyone for CORS.

username : str, optional

If provided basic auth will be applied for this username. Requires

password to also be provided.

password : str, optional

If provided basic auth will be applied for this password. Requires

username to also be provided.

Returns

-------

api, app, api_endpoint

"""

app = Flask(__name__) # create the flask app

# 1. configure CORS decorator

cors_dict = {

'allowed_origins': Server.allowed_origins,

'headers': ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'],

'methods': ['HEAD', 'GET', 'POST', 'PATCH', 'PUT', 'OPTIONS', 'DELETE'],

'credentials': True

}

if dev:

# in development mode we can't use basic auth

cors_dict['credentials'] = False

app.debug = True

# create the cors decorator

decorators = [cors.crossdomain(**cors_dict)]

if username is not None and password is not None:

print('enabling basic auth')

# note the we cors is the last decorator -> the first that is hit. This

# is what we want as CORS will detect OPTIONS requests and allow them

# immediately. All other requests will be sent through the basicauth

# decorator.

decorators.insert(0, basicauth(username, password))

api = Api(app, decorators=decorators)

return api, app

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值