def create_product(self, request):
"""Create a new product - product data is posted as json
Example request ::
{
"id": "the_odyssey",
"title": "The Odyssey",
"passenger_capacity": 101,
"maximum_speed": 5,
"in_stock": 10
}
The response contains the new product ID in a json document ::
{"id": "the_odyssey"}
"""
schema = ProductSchema(strict=True)
try:
# load input data through a schema (for validation)
# Note - this may raise `ValueError` for invalid json,
# or `ValidationError` if data is invalid.
product_data = schema.loads(request.get_data(as_text=True)).data
except ValueError as exc:
raise BadRequest("Invalid json: {}".format(exc))
# Create the product
self.products_rpc.create(product_data)
return Response(
json.dumps({'id': product_data['id']}), mimetype='application/json'
)
评论列表
文章目录