This is the second video for the “Python API with Flask” series, showing how to make POST and GET commands towards a dummy database, which is a python list in our case.
We are using only CREATE and READ commands – creating a task (POST) and reading it (GET).
Additionally, reading it by number and name is implemented as well (GET).
This is how the code looks like (run it with python app.py from the terminal):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
from flask import Flask, request, jsonify app = Flask(__name__) # Dummy database items = [] # Create an item @app.route('/api/items', methods = ['POST']) def create_item(): data = request.get_json() if 'name' not in data: return jsonify({'error': 'Required - name!'}), 400 item_id = len(items) + 1 name = data['name'] new_item = {'id':item_id, 'name':name} items.append(new_item) return jsonify({'message': 'Item created successfully', 'item': new_item}), 201 # Get all items @app.route('/api/items', methods=['GET']) def get_all_items(): return jsonify({'items': items}) # Get item by ID @app.route('/api/items/<int:item_id>', methods=['GET']) def get_item_by_id(item_id): item = next((item for item in items if item['id'] == item_id), None) if item is None: return jsonify({'error':f'Item {item_id} is not found, sorry!'}), 404 return jsonify({'item':item}) # Get item by name @app.route('/api/items/name/<string:item_name>', methods=['GET']) def get_item_by_name(item_name): item = next((item for item in items if item['name'] == item_name), None) if item is None: return jsonify({'error':f'Item {item_name} is not found, sorry!'}), 404 return jsonify({'item':item}) if __name__ == '__main__': app.run(debug = True) |
The tests should be created in the same folder. These should be successful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import unittest import json from app import app class TestApp(unittest.TestCase): def setUp(self): self.app = app.test_client() self.app.testing = True def test_create_item(self): data = {'name':'New Item'} response = self.app.post('/api/items', json=data) result = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 201) self.assertEqual(result['message'], 'Item created successfully') def test_create_item_with_missing_name(self): data = {} response = self.app.post('/api/items', json=data) result = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 400) self.assertIn('error', result) def test_get_all_items(self): response = self.app.get('/api/items') result = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertIn('items', result) def test_create_items_and_get_count(self): response = self.app.get('/api/items') items_count = json.loads(response.data.decode('utf-8')) items_count_before_test = len(items_count.get('items',[])) # Create `n` additional items n = 5 for i in range(n): data = { 'name': f'Item-{i+1}' } self.app.post('/api/items', json=data) # Get all items: response = self.app.get('/api/items') result = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) # Check the if the count() is `n`: items_count = len(result.get('items', [])) self.assertEqual(items_count, n + items_count_before_test) if __name__ == '__main__': unittest.main() |
Finally, the YouTube video is here:
Enjoy it!