Python Classroom notes 08/May/2025

HTTP(s)

  • HTTP has request and response
  • Request has
    • url/endpoint
    • headers: these are key value pairs sent along with request
    • body:
    • Action/Verb/Method:
      • GET: Select
      • PUT: Update (all)
      • PATCH: update (few)
      • POST: Create
      • DELETE: Delete
  • Response
    • Status Code: Refer Here
      • 1xx: Information
      • 2xx: Success
      • 3xx: Redirection
      • 4xx: client side errors
      • 5xx: Server side errors
    • headers
    • body

Exercise:

  1. Write a python code to send sms
  2. Write python code to find temperature of hyderabad tomorrow Refer Here

Sending HTTP Requests

  • Python has a pypi library requests and Refer Here for using requests library
import requests
url = "https://fakestoreapi.com/products/1"
response = requests.get(url)
if response.status_code == 200:
    print(response.content)
  • Generally all APIs are documented which give us how to use apis. The standard is OpenAPI

REST API

  • This is an architecural pattern to build APIs
  • Refer Here for RESTAPI docs

REST API Principles

Principle Description
Uniform Interface Standardizes interactions between client and server using consistent resource identification, representations, and HTTP methods (GET, POST, PUT, DELETE)[1][4][7].
Client-Server Separation Separates client and server concerns, allowing each to evolve independently as long as the interface is unchanged[1][3][4][7].
Statelessness Each request from client to server must contain all necessary information; no session state is stored on the server[1][3][4][7].
Cacheable Responses must define whether they are cacheable to improve performance and scalability[1][3][4][7].
Layered System The API architecture can include multiple layers (e.g., intermediaries, proxies) without clients knowing about them[1][3][4][7].
Code on Demand (optional) Servers can provide executable code (like JavaScript) to clients, extending client functionality when needed[1][4][7].

Would you like examples of how these principles are applied in real-world APIs?

Citations:
[1] https://restfulapi.net
[2] https://daily.dev/blog/restful-api-design-best-practices-guide-2024
[3] https://www.ibm.com/think/topics/rest-apis
[4] https://blog.dreamfactory.com/rest-apis-an-overview-of-basic-principles
[5] https://aws.amazon.com/what-is/restful-api/
[6] https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design
[7] https://about.gitlab.com/blog/2024/10/18/guide-to-rest-api/
[8] https://www.postman.com/api-platform/api-design/
[9] https://www.getambassador.io/blog/7-rest-api-design-best-practices
[10] https://upsun.com/blog/restful-api-design-principles/
[11] https://www.mulesoft.com/api/rest/what-is-rest-api-design
[12] https://codewithmukesh.com/blog/restful-api-best-practices-for-dotnet-developers/
[13] https://blog.postman.com/rest-api-examples/
[14] https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/
[15] https://swagger.io/resources/articles/best-practices-in-api-design/
[16] https://www.youtube.com/watch?v=8XK2o5MfxkE
[17] https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/

Lets Design a REST API for Job Portal

  • Identify Nouns which generally become Resources
    • User
    • Employer
    • Job
    • Account
  • On Each Resource (Noun) define Actions
    • Actions Account:
      • Register
      • UnRegister
    • User
    • Job
    • Employer
  • URI (Uniform Resource Identifier)
    • /Users
      • Get: Get all users
      • POST: Create a user with a body (json)
    • /Users/1
      • Get user with id 1
      • PUT: update the user information for a user with id 1

REST API Creation – Server

  • We find nouns and verbs and create a API with docs
  • In python, we can acheive creating this with many libraries but we will be using fastapi.
  • Other options:
    • Django-restful
    • flask

Rest API Usage – Client

  • On a larger note
    • Web stores (Reactjs/angularjs)
    • Mobile/Desktop App
    • Custom Tools:
      • from code
      • from cli
  • For using a Rest API we need API Docs (Swagger/OpenAPI)

References

Getting started with fastapi

  • Refer Here for fastapi official docs
  • Create a virtual environment and install fastapi
pip install "fastapi[standard]"
  • code which i have written
from fastapi import FastAPI

app = FastAPI(
    title="Hello-api",
    summary="Learning FastAPI",
    version="0.0.1")

@app.get("/")
def home():
    return { "message": "hello" }

@app.get("/square/{number}")
def square(number: int):
    return {
        "result" : number ** 2
    }

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube