本文整理汇总了Python中flask.wrappers.Response方法的典型用法代码示例。如果您正苦于以下问题:Python wrappers.Response方法的具体用法?Python wrappers.Response怎么用?Python wrappers.Response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.wrappers
的用法示例。
在下文中一共展示了wrappers.Response方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def request(self) -> Response:
return self.client.open(
method=self.method,
path=self.path.format(**self.path_parameters),
json=self.json,
query_string=self.query_string,
)
示例2: response
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def response(self) -> wrappers.Response:
return make_response(self.page())
示例3: all_exception_handler
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def all_exception_handler(error: Exception) -> Tuple[Response, int]:
"""Catches and handles all exceptions, add more specific error Handlers.
:param Exception
:returns Tuple of a Flask Response and int
"""
return create_response(message=str(error), status=500)
示例4: response_factory
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def response_factory():
def create_response(
data, status_code=200, content_type='application/json'):
return Response(data, status=status_code, content_type=content_type)
return create_response
示例5: delete
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def delete(self, widgetId: int) -> Response:
"""Delete Single Widget"""
from flask import jsonify
id = WidgetService.delete_by_id(widgetId)
return jsonify(dict(status="Success", id=id))
示例6: dispatch_request
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def dispatch_request(self, *args, **kwargs):
"""Logic of how to handle a request"""
method = getattr(self, request.method.lower(), None)
if method is None and request.method == 'HEAD':
method = getattr(self, 'get', None)
assert method is not None, 'Unimplemented method {}'.format(request.method)
headers = {'Content-Type': 'application/vnd.api+json'}
response = method(*args, **kwargs)
if isinstance(response, Response):
response.headers.add('Content-Type', 'application/vnd.api+json')
return response
if not isinstance(response, tuple):
if isinstance(response, dict):
response.update({'jsonapi': {'version': '1.0'}})
return make_response(json.dumps(response, cls=JSONEncoder), 200, headers)
try:
data, status_code, headers = response
headers.update({'Content-Type': 'application/vnd.api+json'})
except ValueError:
pass
try:
data, status_code = response
except ValueError:
pass
if isinstance(data, dict):
data.update({'jsonapi': {'version': '1.0'}})
if isinstance(data, FlaskResponse):
data.headers.add('Content-Type', 'application/vnd.api+json')
data.status_code = status_code
return data
elif isinstance(data, str):
json_reponse = data
else:
json_reponse = json.dumps(data, cls=JSONEncoder)
return make_response(json_reponse, status_code, headers)
示例7: open
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def open(self, *args, **kwargs):
"""Modified request.
Adds token and headers and asserts status code.
"""
# We are definetly going to add some headers
if 'headers' not in kwargs:
kwargs['headers'] = {}
# Add token
token = kwargs.pop('token', None)
if token:
kwargs['headers'].update({
# We support a auth header of the form "Token <thetoken>"
'Authorization': 'Token ' + token
})
# Add content-type: json header if nothing else is provided
if (not("content-type" in kwargs['headers']) and
("data" in kwargs)):
# Parse data
kwargs['data'] = json.dumps(kwargs['data'])
# Set header
kwargs['content_type'] = "application/json"
# get the actual response and assert status
expected_code = kwargs.pop('status_code', None)
response = super().open(*args, **kwargs)
status_code = response.status_code
if (expected_code is not None and expected_code != status_code):
raise AssertionError(
"Expected a status code of %i, but got %i instead\n"
"Response:\n%s\n%s\n%s" % (expected_code, status_code,
response, response.data,
response.status))
elif ((expected_code == 422) and
('exception' in response.json.get('_issues', {}))):
# The validator swallows exceptions and turns them into 'exception'
# validation errors. Ensure that tests do not miss this by raising
# them properly.
error = response.json['_issues']['exception']
raise AssertionError("Expected a validation error but the "
"validator raised an exception: %s" % error)
return response
示例8: API
# 需要导入模块: from flask import wrappers [as 别名]
# 或者: from flask.wrappers import Response [as 别名]
def API(method=None):
if method is None:
return partial(API)
@wraps(method)
def decorated(*args, **kwargs):
_return = method(*args, **kwargs)
if isinstance(_return, Response):
return _return
if request.url.find('v' + str(API_VERSION)) > 0:
try:
if request.headers['Accept'] == API_ACCEPT_HEADER:
ret, code = _return
else:
raise KeyError
except KeyError:
ret, code = ("Please check request accept again.", status.HTTP_406_NOT_ACCEPTABLE)
else:
ret, code = ("API has been updated. The latest version is v" + str(API_VERSION), status.HTTP_301_MOVED_PERMANENTLY)
return serialize(ret, code)
def serialize(ret, code):
_return = {'code': code}
if not status.is_success(code):
_return['status'] = 'fail'
if ret is not None:
if isinstance(ret, dict):
_return.update(ret)
else:
_return['message'] = ret
else:
_return['status'] = 'success'
if ret is not None:
if isinstance(ret, dict):
_return.update(ret)
else:
_return['data'] = ret
return _return, code
return decorated