Giter Club home page Giter Club logo

fastapi's Introduction

Fast API Basics

Contents

Requirements

Install

pip install fastapi "uvicorn[standard"
pip install uvicorn

Notes

Fast API is production ready.

FAST API IS JUST A FRAMEWORK

you also need a Uvicorn server which is a ASGI server (asynchronous service gateway)

Uvicorn is similar to express js

Syntax Notes

Using data types: name:type or name:type=value

class userUpdateRequest():
	first_name:   str
	last_name:    str  = 'banana'

typing List

  • Simply adds hints for the list

format is:

varName: List[object] = [obj, obj...]

example:

db: List[User] = [
	User(
		id= UUID("4142061b-3062-4206-9de6-28f9f516db29"),
		first_name="Adam",
		last_name='mcminge',
		gender = Gender.male,
		roles = [Role.admin,Role.user]
		)
]

Build and run

  • Make a main.py
  • create a root
  • run below:
uvicorn main:app --reload
  • main is module name
  • app is the instance of our application

HTTP Methods

Head Request Resource representation

Head Same as Get but does not return body

Post Submits an entity to a specified resource , often changing state in server

Put Put replaces all representations of target resource with current payload.

Delete Delete a specified resource

Connect Establishes a connection or tunnel to server.

Testing post requests

Use thunder client in vs code or postman Switch to post add the json body of the user and send

Using Async

use async lets us use await

@app.get('/')
async def read_results():
    results = await some_library()
    return results

Swagger view

simply go to localhost:8000/docs

  • Automatically groups your API endpoints
  • Assuming swagger looks at the functions under the defined endpoints

redoc view

Non-interactive documentation

You can download it.

Pydantic and Data Models

  • Use typing, pydantic (basemodel) and others in the models.py

Examples:
Note the name:dataType format.

class User(BaseModel):
	# gives a uuid if not provided
	id:           Optional[UUID] =  uuid4()
	first_name:   str
	last_name:    str
	middle_name:  Optional[str]
	gender:       Gender 
	roles:        List[Role]

Flow

fastapi's People

Contributors

murchie85 avatar

Watchers

 avatar  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.