本文整理汇总了Python中falcon.HTTP_BAD_REQUEST属性的典型用法代码示例。如果您正苦于以下问题:Python falcon.HTTP_BAD_REQUEST属性的具体用法?Python falcon.HTTP_BAD_REQUEST怎么用?Python falcon.HTTP_BAD_REQUEST使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类falcon
的用法示例。
在下文中一共展示了falcon.HTTP_BAD_REQUEST属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_both_schemas_validation_failure
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def test_both_schemas_validation_failure():
with pytest.raises(HTTPError) as excinfo:
Resource().both_validated(GoodData(), BadData())
assert excinfo.value.title == falcon.HTTP_INTERNAL_SERVER_ERROR
assert excinfo.value.description == "Response data failed validation"
with pytest.raises(HTTPError) as excinfo:
Resource().both_validated(BadData(), GoodData())
msg = "Request data failed validation: data must contain ['message'] properties"
assert excinfo.value.title == falcon.HTTP_BAD_REQUEST
assert excinfo.value.description == msg
client = testing.TestClient(falcon.API())
client.app.add_route("/test", Resource())
result = client.simulate_put("/test", json=BadData.media)
assert result.status_code == 400
示例2: on_post
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def on_post(self, req, resp):
self.process_request(req, resp)
password = req.context.get('_body', {}).get('password')
if password is None:
resp.status = falcon.HTTP_BAD_REQUEST
return None
result = self.parse_password(password)
resp.body = json.dumps(result)
示例3: test_create_bulk_without_list_results_in_bad_request
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def test_create_bulk_without_list_results_in_bad_request(self):
self.do_create_bulk(
{'writable': 'changed', 'unsigned': 12}
)
assert self.srmock.status == falcon.HTTP_BAD_REQUEST
# actual test case classes here
示例4: retrieve_obj
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def retrieve_obj(id_: str, mapper_: Mapper) -> Model:
name = mapper_.__class__.__name__.lower().split("mapper")[0].capitalize()
obj = mapper_.get(id_)
if obj is None:
raise HTTPError(falcon.HTTP_BAD_REQUEST, f"{name} not found")
elif obj.deleted_at is not None:
raise HTTPError(falcon.HTTP_GONE, f"{name} was deleted")
return obj
示例5: _validate
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def _validate(req_schema: Dict = None, resp_schema: Dict = None):
def decorator(func):
@wraps(func)
def wrapper(self, req, resp, *args, **kwargs):
if req_schema is not None:
try:
schema = fastjsonschema.compile(req_schema)
schema(req.media)
except fastjsonschema.JsonSchemaException as e:
msg = "Request data failed validation: {}".format(e.message)
raise HTTPError(falcon.HTTP_BAD_REQUEST, msg)
result = func(self, req, resp, *args, **kwargs)
if resp_schema is not None:
try:
schema = fastjsonschema.compile(resp_schema)
schema(resp.media)
except fastjsonschema.JsonSchemaException:
raise HTTPError(
falcon.HTTP_INTERNAL_SERVER_ERROR,
"Response data failed validation",
)
# Do not return 'e.message' in the response to
# prevent info about possible internal response
# formatting bugs from leaking out to users.
return result
return wrapper
return decorator
示例6: test_req_schema_validation_failure
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def test_req_schema_validation_failure():
with pytest.raises(falcon.HTTPError) as excinfo:
Resource().request_validated(BadData(), None)
msg = "Request data failed validation: data must contain ['message'] properties"
assert excinfo.value.title == falcon.HTTP_BAD_REQUEST
assert excinfo.value.description == msg
示例7: on_post
# 需要导入模块: import falcon [as 别名]
# 或者: from falcon import HTTP_BAD_REQUEST [as 别名]
def on_post(self, req, resp):
block_hash = None
if req.media.get('block_id'):
substrate = SubstrateInterface(SUBSTRATE_RPC_URL)
block_hash = substrate.get_block_hash(req.media.get('block_id'))
elif req.media.get('block_hash'):
block_hash = req.media.get('block_hash')
else:
resp.status = falcon.HTTP_BAD_REQUEST
resp.media = {'errors': ['Either block_hash or block_id should be supplied']}
if block_hash:
print('Processing {} ...'.format(block_hash))
harvester = PolkascanHarvesterService(
db_session=self.session,
type_registry=TYPE_REGISTRY,
type_registry_file=TYPE_REGISTRY_FILE
)
block = Block.query(self.session).filter(Block.hash == block_hash).first()
if block:
resp.status = falcon.HTTP_200
resp.media = {'result': 'already exists', 'parentHash': block.parent_hash}
else:
amount = req.media.get('amount', 1)
for nr in range(0, amount):
try:
block = harvester.add_block(block_hash)
except BlockAlreadyAdded as e:
print('Skipping {}'.format(block_hash))
block_hash = block.parent_hash
if block.id == 0:
break
self.session.commit()
resp.status = falcon.HTTP_201
resp.media = {'result': 'added', 'parentHash': block.parent_hash}
else:
resp.status = falcon.HTTP_404
resp.media = {'result': 'Block not found'}