最初,我编写了webservice
,POST
以允许在所有端点上都允许同时使用'GET'和方法。现在,我们的webservices工作流程有所更改,我们希望POST
为某些端点阻止该端点,并PUT
为它们启用某些端点,这些端点仍将POST
启用。
我将通过添加以下代码片段来解释更多 settings.py
__author__ = 'sappal'
# pulling DBSchema from DBTableSchema
from DBSchema.DBTableSchema import DBTableSchema
from Configs import Configs
dbtableSchema = DBTableSchema()
# Let's just use the local mongod instance. Edit as needed.
# Please note that MONGO_HOST and MONGO_PORT could very well be left
# out as they already default to a bare bones local 'mongod' instance.
## LOCALHOST ENTRIES
MONGO_HOST = Configs.MONGO_DB_HOST
MONGO_PORT = Configs.MONGO_DB_PORT
MONGO_USERNAME = Configs.MONGO_DB_USER_NAME
MONGO_PASSWORD = Configs.MONGO_DB_PASSWORD
MONGO_DBNAME = Configs.MONGO_DB
# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'PATCH', 'POST', 'DELETE']
# Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of
# individual items (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE', 'POST']
# Used for implementing user-resource restricted access.
# Returns the documents which are associated with particular user
AUTH_FIELD = 'userid'
people = {
'item_title': 'person',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'POST'],
'schema': dbtableSchema.schema_people,
'public_methods': ['POST']
}
org = {
'item_title': 'org',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'PATCH'],
'schema': dbtableSchema.schema_people_org
}
puburl = {
'item_title': 'puburl',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'PATCH'],
'schema': dbtableSchema.schema_people_pub_url
}
address = {
'item_title': 'address',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'PATCH'],
'schema': dbtableSchema.schema_people_address
}
contactnumber = {
'item_title': 'contactnumber',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'PATCH'],
'schema': dbtableSchema.schema_people_contact_number
}
template = {
'item_title': 'template',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'POST'],
'schema': dbtableSchema.schema_template
}
usersharedcontacts = {
'item_title': 'usersharedcontacts',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'PATCH'],
'schema': dbtableSchema.schema_people_with_user_shared_contacts
}
cardholder = {
'item_title': 'cardholder',
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'resource_methods': ['GET', 'PATCH'],
'schema': dbtableSchema.schema_people_card_holder
}
DOMAIN = {
'people': people,
'org': org,
'puburl': puburl,
'address': address,
'contactnumber': contactnumber,
'template': template,
'usersharedcontacts': usersharedcontacts,
'cardholder': cardholder
}
我想使POST
方法people
和template
终结点都启用,如您所见,我已经'resource_methods': ['GET', 'POST']
为上述终结点进行了此配置。
我还想POST
为其余端点禁用方法,因此我为其余端点配置了以下内容'resource_methods': ['GET', 'PATCH'],
。
我还配置了 RESOURCE_METHODS = ['GET', 'PATCH', 'POST', 'DELETE']
但是,当我尝试运行应用程序时,出现以下类型的控制台错误
eve.exceptions.ConfigException: Unallowed [usersharedcontacts] resource method(s): PATCH. Supported: GET, POST, DELETE
Process finished with exit code 1
PATCH
是文档(项目)方法,而不是资源方法,这就是为什么您会收到Unallowed异常的原因。尝试:
'resource_methods': ['GET'], # read-only resource endpoint
'item_methods': ['PATCH'] # still allow edits at the document endpoint
有关更多详细信息,请参见文档中的CRUD操作表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句