def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted or data that is sent via an html form post. """ app.logger.info('Request to create a Pet') data = {} # Check for form submission data if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded': app.logger.info('Processing FORM data') data = { 'name': request.form['name'], 'category': request.form['category'], 'available': request.form['available'].lower() in ['true', '1', 't'] } else: app.logger.info('Processing JSON data') data = request.get_json() pet = Pet() pet.deserialize(data) pet.save() message = pet.serialize() return make_response(jsonify(message), HTTP_201_CREATED, {'Location': url_for('get_pets', pet_id=pet.id, _external=True)})
def test_deserialize_a_pet(self): data = {"id": 1, "name": "kitty", "category": "cat", "available": True} pet = Pet(data['id']) pet.deserialize(data) self.assertNotEqual(pet, None) self.assertEqual(pet.id, 1) self.assertEqual(pet.name, "kitty") self.assertEqual(pet.category, "cat")
def test_deserialize_a_pet(self): data = {"id":1, "name": "kitty", "category": "cat"} pet = Pet() pet.deserialize(data) self.assertNotEqual( pet, None ) self.assertNotEqual( pet.id, 0 ) self.assertEqual( pet.name, "kitty" ) self.assertEqual( pet.category, "cat" )
def create_pets(): """ Creates a Pet in the datbase from the posted database """ payload = request.get_json() pet = Pet() pet.deserialize(payload) pet.save() message = pet.serialize() response = make_response(jsonify(message), HTTP_201_CREATED) response.headers['Location'] = url_for('get_pets', id=pet.id, _external=True) return response
def test_deserialize_a_pet(self): """ Test deserialization of a Pet """ data = {"id": 1, "name": "kitty", "category": "cat", "available": True} pet = Pet() pet.deserialize(data) self.assertNotEqual(pet, None) self.assertEqual(pet.id, None) self.assertEqual(pet.name, "kitty") self.assertEqual(pet.category, "cat") self.assertEqual(pet.available, True)
def create_pets(): """ Creates a Pet in the datbase from the posted database """ payload = request.get_json() pet = Pet() pet.deserialize(payload) pet.save() message = pet.serialize() response = make_response(jsonify(message), HTTP_201_CREATED) response.headers['Location'] = url_for('get_pets', id=pet.id, _external=True) return response
def create_pets(): data = {} # Check for form submission data if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded': data = {'name': request.form['name'], 'category': request.form['category']} else: data = request.get_json() pet = Pet() pet.deserialize(data) pet.save() message = pet.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': pet.self_url() })
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted """ check_content_type('application/json') pet = Pet() pet.deserialize(request.get_json()) pet.save() message = pet.serialize() location_url = url_for('get_pets', pet_id=pet.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted --- tags: - Pets consumes: - application/json produces: - application/json parameters: - in: body name: body required: true schema: id: data required: - name - category properties: name: type: string description: name for the Pet category: type: string description: the category of pet (dog, cat, etc.) responses: 201: description: Pet created schema: id: Pet properties: id: type: integer description: unique id assigned internally by service name: type: string description: the pets's name category: type: string description: the category of pet (e.g., dog, cat, fish, etc.) 400: description: Bad Request (the posted data was not valid) """ pet = Pet() pet.deserialize(request.get_json()) pet.save() message = pet.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': pet.self_url() })
def test_deserialize_a_pet(self): """ Test deserialization of a Pet """ data = {"id": 1, "name": "kitty", "category": "cat"} pet = Pet() pet.deserialize(data) self.assertNotEqual(pet, None) self.assertEqual(pet.id, 0) # id should be ignored self.assertEqual(pet.name, "kitty") self.assertEqual(pet.category, "cat") # test with id passed into constructor pet = Pet(1) pet.deserialize(data) self.assertNotEqual(pet, None) self.assertEqual(pet.id, 1) self.assertEqual(pet.name, "kitty") self.assertEqual(pet.category, "cat")