随笔分类 - FastApi
1
摘要:1.安装 1.下载软件包 下载地址 下载最新release的win版,例如:poetry-1.1.13-win32.tar.gz 2.获取安装脚本 脚本获取地址 直接复制到本地,新建一个文件即可,名字随意,后续要用到 3.使用命令安装 打开cmd,进入安装包和脚本文件所在目录 执行命令:python
阅读全文
摘要:import uvicorn from fastapi import FastAPI app = FastAPI() def register(server_name, ip, port): c = consul.Consul(host="127.0.0.1", port=8500) # consu
阅读全文
摘要:1.基于用户名密码认证 from typing import Optional from fastapi import APIRouter, Depends, HTTPException from fastapi.security import OAuth2PasswordBearer, OAuth
阅读全文
摘要:1.Tortoise ORM 在项目中的配置 1.配置TORTOISE_ORM,参见:https://www.cnblogs.com/puffer/p/16428100.html 2.main.py主程序中进行注册 from tortoise.contrib.fastapi import regis
阅读全文
摘要:1.Tortoise ORM Tortoise ORM是一个易于使用的ORM(Object Relational Mapper),灵感来自Django,不多做介绍 2.模型定义 from tortoise import fields from tortoise.models import Model
阅读全文
摘要:1.JWT认证 from datetime import datetime, timedelta from passlib.context import CryptContext from jose import JWTError, jwt SECRET_KEY = "60a633523fb4358
阅读全文
摘要:1.函数依赖注入简单示例 from typing import Optional import uvicorn from fastapi import FastAPI, Depends async def common_parameters(q: Optional[str] = None, page
阅读全文
摘要:1.路径操做配置 @app.post( "/path_operation_configuration", response_model=xxx, # 接口描述,一般放在`app.include_router`里面,做一个模块划分 # 如果有有多个,则在文档中国该接口会分别展示多条 tags=['pa
阅读全文
摘要:静态文件配置 import os from pathlib import Path from fastapi import FastAPI import uvicorn from fastapi.staticfiles import StaticFiles app = FastAPI() base_
阅读全文
摘要:1.小文件上传 1.单文件上传 import uvicorn from fastapi import FastAPI from fastapi import File app = FastAPI() @app.post("/file") async def file_upload(file: byt
阅读全文
摘要:1.Response对象 1.Response Response类接收以下参数 content:str or bytes status_code:HTTP 状态码 headers:字符串字典 media_type:例如"text/html" Response会自动包含以下头信息 Content-Le
阅读全文
摘要:1.获取cookie信息 from fastapi import Cookie @users.get("/cookie") def cookie(cookie_id: Optional[str] = Cookie(None)): # 此处如果不使用Cookie转换参数,则会被当作查询参数处理 ret
阅读全文
摘要:1.表单格式 from fastapi import Form app = FastAPI() @app.post('/login') def user_login(username: str = Form(...), password: str = Form(...)): """pip insta
阅读全文
摘要:1.路径参数 1.方法中的形参需要和路径参数名保持一致 @app.get("/enum/{city}") def view_city(city: CityName): 2.对于路径作为路径参数,需要使用path转义 # file_path: "/etc/conf/nginx.conf" @app.g
阅读全文
摘要:1.model.dict(...) 将模型转换为字典的主要方法。子模型将递归转换为字典。 参数如下: include:要包含在返回的字典中的字段 exclude:要从返回的字典中排除的字段 by_alias:字段别名是否应用作返回字典中的键 exclude_unset:创建模型时未显式设置的字段是否
阅读全文
摘要:1.根据模型自动创建JSON结构 from enum import Enum from pydantic import BaseModel, Field class FooBar(BaseModel): count: int size: float = None class Gender(str,
阅读全文
摘要:1.装饰器实现验证器 from pydantic import BaseModel, ValidationError, validator class UserModel(BaseModel): name: str username: str password1: str password2: st
阅读全文
摘要:1.复合类型 Union,支持将多种类型作为可选项 from uuid import UUID from typing import Union from pydantic import BaseModel class User(BaseModel): id: Union[UUID, int, st
阅读全文
摘要:错误处理 每当pydantic在它正在验证的数据中发现错误时,它就会引发。ValidationError from typing import List from pydantic import BaseModel, ValidationError, conint class Location(Ba
阅读全文
摘要:模型 在pydantic中定义对象的主要方法是通过模型BaseModel。 1.1基本模型 from pydantic import BaseModel class User(BaseModel): id: int # 整形、必须 name = 'Jane Doe' # 通过默认值推断类型为字符串,
阅读全文
1