HTTP
- This is a standard protocol for communication
- HTTP:
- Request
- Verb (Method):
- GET
- POST
- PUT
- DELETE
- HEAD
- URL
- HEADERS:
- Body
- Verb (Method):
- Response
- Status Code
- Body
- Headers
- Request
- HTTP Protocol can work with any form of data
REST APIs (Representational state transfer)
- REST APIs are not protocols but an style of implementing Services
- Concepts:
- Resources
- Methods
- REST API Principles
| Principle | Description | Example / Notes |
| ———————————– | ——————————————————————————————————————————————– | ——————————————————————————————- |
| 1. Client-Server Separation | The client (frontend/UI) and server (backend/data) are independent. The client only needs to know the API endpoints, not how data is stored. | A mobile app calls /api/doctors — it doesn’t care if data comes from MySQL or MongoDB. |
| 2. Statelessness | Each API request must contain all necessary information; the server doesn’t store client session state between requests. | Every request includes authentication token (like JWT) and required data. |
| 3. Uniform Interface | REST APIs follow a consistent, predictable structure for endpoints, HTTP methods, and responses. | Use nouns (/patients/1), not verbs (/getPatient); use standard HTTP methods. |
| 4. Resource-Based | Everything is treated as a resource (patients, doctors, appointments), and each has a unique URI. | /api/appointments/101 uniquely identifies an appointment. |
| 5. Use of HTTP Methods Properly | Use standard HTTP verbs for CRUD: GET, POST, PUT, DELETE, PATCH. | GET /patients → read, POST /patients → create. |
| 6. Representations | A resource can be represented in multiple formats like JSON, XML, or HTML. | Typically, REST APIs use JSON for simplicity. |
| 7. Layered System | The API can have multiple layers — authentication, caching, load balancer — without the client knowing. | Client only sees /api/appointments, not the backend structure. |
| 8. Cacheability | Responses should define whether they can be cached to improve performance. | Add HTTP headers like Cache-Control: max-age=3600. |
| 9. HATEOAS (optional advanced) | API responses can include links to related resources for discoverability. | { "id":1, "name":"Dr. Smith", "_links": {"appointments": "/api/doctors/1/appointments"} } |
| 10. Versioning | Maintain backward compatibility by versioning APIs. | /api/v1/doctors, /api/v2/doctors. |
- Uniform design
| Operation | HTTP Method | Endpoint | Request Body | Description |
| —————— | ————— | —————- | ——————————————————- | ———————— |
| Get all doctors | GET | /api/doctors | — | Retrieve list of doctors |
| Get one doctor | GET | /api/doctors/1 | — | Retrieve specific doctor |
| Create new doctor | POST | /api/doctors | { "name": "Dr. Rao", "specialization": "Cardiology" } | Add a new doctor |
| Update doctor info | PUT | /api/doctors/1 | { "specialization": "Neurology" } | Update doctor details |
| Delete a doctor | DELETE | /api/doctors/1 | — | Remove doctor record |
- To document the REST ful apis there is a standard called as Open API. Generally swagger docs are popular way to document apis
Lets write a fake rest api client in python
-
APIs when calling will be of the following categories
- anonymous/unauthenticated
- Authenticated
- Token
- API Keys
-
Look at the following apis
https://jsonplaceholder.typicode.com/todoshttps://jsonplaceholder.typicode.com/todos/1
https://jsonplaceholder.typicode.com/usershttps://jsonplaceholder.typicode.com/users/1
- Create a new project, activate venv and addpackage
requests - and write a simple python code
import requests
BASE_URL = "https://jsonplaceholder.typicode.com"
USERS_URL = f"{BASE_URL}/users"
TODOS_URL = f"{BASE_URL}/todos"
def get_all_users():
response = requests.get(USERS_URL)
#print(response)
users = response.json()
for user in users:
print(user['name'])
def make_api_calls():
get_all_users()
if __name__ == "__main__":
make_api_calls()
-
Refer Here for changes
-
Exercise: play around with fake store api and write python functions to perform all operations on product also try
aiohttp
Goal: To Create RestAPIs in python
- Gaps:
- WSGI, ASGI
- Swagger/OpenApi
- Defining endpoints
- Dependency injection
- Next Steps:
- Authorization & Authentication
- Testability.
- Running in a container.
