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 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_serialize_a_pet(self): pet = Pet(0, "fido", "dog") data = pet.serialize() self.assertNotEqual( data, None ) self.assertIn( 'id', data ) self.assertEqual( data['id'], 0 ) self.assertIn( 'name', data ) self.assertEqual( data['name'], "fido" ) self.assertIn( 'category', data ) self.assertEqual( data['category'], "dog" )
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 test_serialize_a_pet(self): """ Test serialization of a Pet """ pet = Pet(name="fido", category="dog", available=False) data = pet.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], None) self.assertIn('name', data) self.assertEqual(data['name'], "fido") self.assertIn('category', data) self.assertEqual(data['category'], "dog") self.assertIn('available', data) self.assertEqual(data['available'], False)
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() })