|
| 1 | +# API Examples — FastAPI & Flask |
| 2 | + |
| 3 | +> Build REST APIs with Python |
| 4 | +
|
| 5 | +--- |
| 6 | + |
| 7 | +## 📁 Structure |
| 8 | + |
| 9 | +``` |
| 10 | +├── FastAPI/ # Modern, fast API |
| 11 | +│ └── main.py # Campaign management API |
| 12 | +└── RESTAPI/ # Flask + SQLAlchemy |
| 13 | + └── main.py # Travel destinations API |
| 14 | +``` |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 🚀 FastAPI |
| 19 | + |
| 20 | +Modern, fast web framework for building APIs. |
| 21 | + |
| 22 | +### Features |
| 23 | +- Automatic docs (`/docs`) |
| 24 | +- Type validation |
| 25 | +- Async support |
| 26 | +- Fast performance |
| 27 | + |
| 28 | +### Endpoints |
| 29 | + |
| 30 | +| Method | Endpoint | Description | |
| 31 | +|--------|----------|-------------| |
| 32 | +| GET | `/` | Welcome message | |
| 33 | +| GET | `/campaigns` | List all campaigns | |
| 34 | +| GET | `/campaigns/{id}` | Get campaign by ID | |
| 35 | + |
| 36 | +### Run |
| 37 | +```bash |
| 38 | +cd FastAPI |
| 39 | +uvicorn main:app --reload |
| 40 | + |
| 41 | +# Visit: |
| 42 | +# http://127.0.0.1:8000 |
| 43 | +# http://127.0.0.1:8000/docs |
| 44 | +``` |
| 45 | + |
| 46 | +### Example Request |
| 47 | +```bash |
| 48 | +curl http://127.0.0.1:8000/campaigns |
| 49 | +``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 🌸 Flask REST API |
| 54 | + |
| 55 | +Simple, flexible web framework with SQLAlchemy. |
| 56 | + |
| 57 | +### Features |
| 58 | +- SQLite database |
| 59 | +- Full CRUD operations |
| 60 | +- JSON responses |
| 61 | + |
| 62 | +### Endpoints |
| 63 | + |
| 64 | +| Method | Endpoint | Description | |
| 65 | +|--------|----------|-------------| |
| 66 | +| GET | `/` | Welcome message | |
| 67 | +| GET | `/destinations` | List all destinations | |
| 68 | +| GET | `/destinations/{id}` | Get destination by ID | |
| 69 | +| POST | `/destinations` | Add destination | |
| 70 | +| PUT | `/destinations/{id}` | Update destination | |
| 71 | +| DELETE | `/destinations/{id}` | Delete destination | |
| 72 | + |
| 73 | +### Run |
| 74 | +```bash |
| 75 | +cd RESTAPI |
| 76 | +python main.py |
| 77 | + |
| 78 | +# Visit: http://127.0.0.1:5000 |
| 79 | +``` |
| 80 | + |
| 81 | +### Example Requests |
| 82 | + |
| 83 | +**Get all destinations:** |
| 84 | +```bash |
| 85 | +curl http://127.0.0.1:5000/destinations |
| 86 | +``` |
| 87 | + |
| 88 | +**Add destination:** |
| 89 | +```bash |
| 90 | +curl -X POST http://127.0.0.1:5000/destinations \ |
| 91 | + -H "Content-Type: application/json" \ |
| 92 | + -d '{"destination":"Paris","country":"France","rating":4.8}' |
| 93 | +``` |
| 94 | + |
| 95 | +**Delete destination:** |
| 96 | +```bash |
| 97 | +curl -X DELETE http://127.0.0.1:5000/destinations/1 |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## 📊 Comparison |
| 103 | + |
| 104 | +| Feature | FastAPI | Flask | |
| 105 | +|---------|---------|-------| |
| 106 | +| Speed | ⚡ Very Fast | 🐢 Moderate | |
| 107 | +| Type Validation | ✅ Built-in | ⚠️ Manual | |
| 108 | +| Auto Docs | ✅ Yes | ❌ No | |
| 109 | +| Learning Curve | 📈 Medium | 📉 Low | |
| 110 | +| Best For | Modern APIs | Simple APIs | |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 💡 Tips |
| 115 | + |
| 116 | +1. **Use FastAPI** for new projects |
| 117 | +2. **Use Flask** for simple, quick APIs |
| 118 | +3. **Always test** your endpoints |
| 119 | +4. **Use Postman/curl** for testing |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## 📚 Resources |
| 124 | + |
| 125 | +- [FastAPI Docs](https://fastapi.tiangolo.com/) |
| 126 | +- [Flask Docs](https://flask.palletsprojects.com/) |
| 127 | +- [SQLAlchemy Docs](https://www.sqlalchemy.org/) |
| 128 | + |
| 129 | +Happy coding! 🚀 |
0 commit comments