GithubHelp home page GithubHelp logo

fastapi's Introduction

#Have python 3.6 or more installed in your computer #install required packages with below commands

Pip install fastapi, uvicorn

import all required packages as below

from fastapi import FastAPI, Path from typing import Optional from pydantic import BaseModel

app = FastAPI()

we are going to experiment below CRUD operations

GET- GET object data

POST- Create new object

PUT- Update an Object

Delete - delete object

To You application use below command

uvicorn startapi:app --reload

visit http://127.0.0.1:8000 after you run the app then visit http://127.0.0.1:8000/docs for all the apis created below.

create a dictionary by the name student and add one student as below

students = { 1: { "name": "john", "age": 17, "class": "year 12" } }

class Student will help us add a new student in the POST Method

class Student(BaseModel): name: str age: int year: str

UpdateStudent will help us Update student details

class UpdateStudent(BaseModel): name: Optional[str] = None age: Optional[int] = None year: Optional[str] = None

#first Api will be get method as below @app.get("/") def home(): return {"name": "First Data"}

#get method with params passed @app.get("/get-student/{student_id}") def get_student(student_id: int = Path(None, description="The ID of the student you want to view", gt=0)): return students[student_id]

Get method with query params plus optional params/args feature

@app.get("/get-by-name") def get_student(*, name: Optional[str] = None, test: int): for student_id in students: if students[student_id]["name"] == name: return students[student_id]

return {"Data": "Not found"}

Get method with combining Path ahd Query parameters

@app.get("/get-by-name/{student_id}") def get_student(*, student_id: int, name: Optional[str] = None, test: int): for student_id in students: if students[student_id]["name"] == name: return students[student_id]

return {"Data": "Not found"}

#Post method with Request Body and The post Method we use this to create a new student @app.post("/create-student/{student_id}") def create_student(student_id: int, student: Student): if student_id in students: return {"Error": "Student exists"}

students[student_id] = student
return students[student_id] 

Put method to update student details

@app.put("/update-student/{student_id}") def update_student(student_id: int, student: UpdateStudent): if student_id not in students: return {"Error": "Student does not exist"} if student.name is not None: students[student_id].name = student.name if student.age is not None: students[student_id].age = student.age if student.year is not None: students[student_id].year = student.year return students[student_id]

delete method to delete Student

@app.delete("/delete_student/{student_id}") def delete_student(student_id : int): if student_id not in students: return {"Error" : "Student Does not exist"} del students[student_id] return {"message": "Student Deleted Successfully"}

See below my apis . img_1.png

img.png

Happy coding :)

fastapi's People

Contributors

franciskinyuru avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.