DevOps Classroom notes 25/Oct/2025

Need for Rest API

HTTP

  • HTTP Protocol runs over TCP
  • HTTP Protocol needs a connection and it has
    • Request:
      • URL
      • Method/Verb
        • GET
        • PUT
        • POST
        • DELETE
        • HEAD
      • Headers
        • Key value pairs
      • Body
        • content
    • Response
      • Status code
        • 1xx (information)
        • 2xx (success)
        • 3xx (Redirection)
        • 4xx (client side errors)
        • 5xx (server side errors)
      • Headers
      • Body

Restful API

  • A restful API is not a protocol, rather a practice or an approach recommended
  • Restful APIs (Rest API or APIs) have principles

REST API Design Principles — Step by Step


Step 1: Client–Server Architecture

Principle: Separation of concerns
Description:
REST divides responsibilities — the client handles the user interface (UI/UX), while the server manages data and business logic. This allows both to evolve independently.
Example:
A web app (client) requests /api/doctors, and the server responds with JSON data.


Step 2: Statelessness

Principle: No session state on the server
Description:
Each client request must contain all necessary information for the server to process it. The server does not store session context between requests.
Example:
Every request includes an authentication token (e.g., JWT in headers).


Step 3: Cacheability

Principle: Enable caching where appropriate
Description:
Responses should specify whether they are cacheable to improve performance and scalability.
Example:
HTTP headers like Cache-Control: max-age=3600 tell clients how long they can cache a response.


Step 4: Uniform Interface

Principle: Consistent interaction between client and server
Description:
A uniform interface simplifies interaction and decouples clients from servers. REST achieves this through consistent conventions.
Example:
Use standard HTTP methods (GET, POST, PUT, DELETE) and well-defined resource URIs.


Step 5: Layered System

Principle: Use independent layers
Description:
REST systems can include multiple layers such as security, load balancing, and application servers. Each layer operates independently without needing full visibility into others.
Example:
A proxy or gateway handles authentication before forwarding requests to backend APIs.


Step 6: Code on Demand (Optional)

Principle: Extend client functionality dynamically
Description:
Servers can send executable code (like JavaScript) to clients to extend their behavior. This is optional and rarely used in modern APIs.
Example:
A server sends a JS snippet to be executed by the browser.


Step 7: Resource-Based (Noun-Oriented)

Principle: Focus on resources, not actions
Description:
REST APIs represent resources (nouns) rather than actions (verbs). HTTP methods define actions on these resources.
Example:
/api/patients/123 instead of /api/getPatient?id=123

Building Rest APIs in plain english

  • Identify nouns & Verbs.
  • Nouns become Resources, Verbs will be methods or sub resources
  • Method:
    • GET: Retrive or select
    • PUT/PATCH: update
    • POST: create
    • DELETE: delete
  • Endpoints:
    • A resource called as users
      • /api/v1/users
        • GET: Get all users
        • POST: Create a new user
      • /api/v1/users/1
        • GET: Get the user with id 1
        • PATCH/POST: Update the user info
        • DELETE: Delete the user
      • /api/v1/users/1/orders:
        • GET: Get all the orders of user with 1
        • POST: Create a order for user with id 1
  • For using REST APIs, Documentations need to be published for this we have
    • OpenAPI spec
    • Swagger
  • To test apis we use a tool called as postman

APIS

  • We have two types on larger note
    • anonymous
    • authenticated
      • API Keys
      • Tokens

Consuming APIs from python

  • Lets create a new project and add a dependency requests
import requests

BASE_URL = "https://petstore.swagger.io/v2"
PETS_URL = f"{BASE_URL}/pet"


def get_all_pets(status:str ='available'):
    PET_STATUS_URL = f"{PETS_URL}/findByStatus?status={status}"
    response = requests.get(
        PET_STATUS_URL,
        timeout=30)
    print(response)

    pets = response.json()
    for pet in pets:
        if 'id' in pet and 'name' in pet:
            print(f"{pet['id']} <==> {pet['name']}")
        else:
            print(pet)


if __name__ == "__main__":
    get_all_pets()

  • Exercise: Create a simple python functions to use the product apis from here

Building Restful Apis using fastapi

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a comment

Turn on JavaScript to leave a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company
Exit mobile version
%%footer%%