当前位置: 首页 > news >正文

关于征求网站建设网站优化课程

关于征求网站建设,网站优化课程,网站空间哪个比较好,济南做网站的高端品牌FastAPI 路径参数详解:动态路径与数据校验的灵活实现 本文全面介绍了在 FastAPI 中使用路径参数的技巧和实现方式。路径参数允许 API 动态响应不同路径中的请求信息,结合 URL(Uniform Resource Locator)和 URI(Unifor…

FastAPI 路径参数详解:动态路径与数据校验的灵活实现

本文全面介绍了在 FastAPI 中使用路径参数的技巧和实现方式。路径参数允许 API 动态响应不同路径中的请求信息,结合 URL(Uniform Resource Locator)和 URI(Uniform Resource Identifier)进行资源定位和标识。URL 是指资源的完整访问路径,用于确定资源的具体位置,而 URI 则是更广义的概念,包含 URL,在 URL 的基础上提供对特定资源的唯一标识。文中通过类型声明实现了路径参数的数据转换与校验,确保路径中的参数符合预期格式。本文还展示了路径参数类型、数据校验、路径操作顺序和 Enum 类型的使用,提供了定义固定值路径参数的示例。此外,文章解释了如何声明包含文件路径的路径参数,并提供了具体的代码示例,是学习 FastAPI 路径参数与资源标识的实用指南。

文章目录

  • FastAPI 路径参数详解:动态路径与数据校验的灵活实现
      • 一 路径参数
        • 1 示例
        • 2 路径参数类型
        • 3 数据转换
        • 4 数据校验
      • 二 路径操作的顺序
      • 三 使用 `Enum` 类型预定义路径参数的值
        • 1 比较枚举元素
        • 2 获取枚举值
        • 3 返回枚举元素
      • 四 包含文件路径的路径参数
      • 五 完整代码示例
      • 六 源码地址

一 路径参数

在 FastAPI 中,路径参数通过在路径字符串中包含变量来定义。路径参数用于接收请求 URL 中的动态部分,使得 API 可以根据不同的路径值响应不同的内容。具体来说,路径参数在路径中通过花括号来标识,并在相应的函数参数中进行定义和使用。例如,在路径 /items/{item_id} 中,{item_id} 就是路径参数。

1 示例

以下是一个简单的示例代码:

from fastapi import FastAPIapp = FastAPI()@app.get("/items/{item_id}")
async def read_item(item_id):return {"item_id": item_id}

在这个例子中,路径参数 item_id 的值会被传递给路径操作函数的参数 item_id。可以运行代码文件 chapter02.py 来启动应用:

$ uvicorn chapter02:app --reload

访问自动生成的 API 文档 Swagger UI:http://127.0.0.1:8000/docs。通过访问 http://127.0.0.1:8000/items/foo,可以获得如下返回:

{"item_id": "foo"}
2 路径参数类型

在以下代码中,item_id 的类型声明为 int

from fastapi import FastAPIapp = FastAPI()@app.get("/items02/{item_id}")
async def read_item(item_id: int):return {"item_id": item_id}
3 数据转换

FastAPI 可以通过类型声明自动解析请求中的数据并进行数据转换。例如,路径 /items/3 中的字符串 3 会被自动转换为整数 3

4 数据校验

FastAPI 使用 Python 类型声明来实现数据校验。如果请求中的值与声明的类型不匹配,将返回错误信息。例如,对于 async def read_item(item_id: int),当请求路径为 /items/foo 时,返回的错误信息如下:

{"detail": [{"type": "int_parsing","loc": ["path","item_id"],"msg": "Input should be a valid integer, unable to parse string as an integer","input": "foo"}]
}

在 FastAPI 中,数据校验由 Pydantic 实现。

二 路径操作的顺序

路径操作按定义顺序依次运行。如果一个 URI 既与特定路径匹配,又与包含路径参数的路径匹配,应将特定路径的操作放在前面,包含路径参数的路径写在后面。例如:

from fastapi import FastAPIapp = FastAPI()@app.get("/users/me")
async def read_user_me():return {"user_id": "the current user"}@app.get("/users/{user_id}")
async def read_user(user_id: str):return {"user_id": user_id}

注意/users/me 必须在 /users/{user_id} 之前声明,否则 /users/{user_id} 将匹配 /users/me,并将 me 作为 user_id 的值。

三 使用 Enum 类型预定义路径参数的值

可以使用 Python 的 Enum 类型来预定义路径参数的值。

from enum import Enum
from fastapi import FastAPIclass ModelName(str, Enum):alexnet = "alexnet"resnet = "resnet"lenet = "lenet"app = FastAPI()@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):if model_name is ModelName.alexnet:return {"model_name": model_name, "message": "Deep Learning FTW!"}if model_name.value == "lenet":return {"model_name": model_name, "message": "LeCNN all the images"}return {"model_name": model_name, "message": "Have some residuals"}

使用 Enum 类(ModelName)可以为路径参数 model_name 设置类型注解。

1 比较枚举元素
if model_name is ModelName.alexnet:
2 获取枚举值
model_name.value

ModelName.lenet.value
3 返回枚举元素
return {"model_name": model_name, "message": "Have some residuals"}

请求访问 http://127.0.0.1:8000/models/resnet 时,客户端将收到如下 JSON 响应:

{"model_name": "resnet","message": "Have some residuals"
}

四 包含文件路径的路径参数

可以直接使用 Starlette 的选项声明包含路径路径参数

/files/{file_path:path}

在这个示例中,参数名为 file_path,结尾部分的 :path 表示该参数应匹配整个路径。例如,包含 /home/your/myfile.txt 的路径参数必须以斜杠(/)开头。在这个例子中,URI 为 /files//home/your/myfile.txtfileshome 之间需要使用双斜杠//)。

:OpenAPI 不支持声明包含路径的路径参数,因此访问 /files/{file_path} 时可能会报错 {"detail": "Not Found"}

五 完整代码示例

from fastapi import FastAPI
from enum import Enumapp = FastAPI()@app.get("/items/{item_id}")
async def read_item(item_id):return {"item_id": item_id}@app.get("/items02/{item_id}")
async def read_item(item_id: int):return {"item_id": item_id}@app.get("/users/me")
async def read_user_me():return {"user_id": "the current user"}@app.get("/users/{user_id}")
async def read_user(user_id: str):return {"user_id": user_id}class ModelName(str, Enum):alexnet = "alexnet"resnet = "resnet"lenet = "lenet"@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):if model_name is ModelName.alexnet:return {"model_name": model_name, "message": "Deep Learning FTW!"}if model_name.value == "lenet":return {"model_name": model_name, "message": "LeCNN all the images"}return {"model_name": model_name, "message": "Have some residuals"}@app.get("/files/{file_path:path}")
async def read_file(file_path: str):return {"file_path": file_path}

六 源码地址

详情见:GitHub FastApiProj

引用: FastAPI 文档


文章转载自:
http://clype.hwbf.cn
http://horopter.hwbf.cn
http://isolable.hwbf.cn
http://mayhem.hwbf.cn
http://wainable.hwbf.cn
http://overweening.hwbf.cn
http://nonbeing.hwbf.cn
http://sleek.hwbf.cn
http://sw.hwbf.cn
http://aut.hwbf.cn
http://titration.hwbf.cn
http://choledochotomy.hwbf.cn
http://climatize.hwbf.cn
http://aerobiologist.hwbf.cn
http://handwheel.hwbf.cn
http://foison.hwbf.cn
http://goloptious.hwbf.cn
http://nicotian.hwbf.cn
http://halogenide.hwbf.cn
http://nyctitropism.hwbf.cn
http://demurely.hwbf.cn
http://genevan.hwbf.cn
http://semiclassical.hwbf.cn
http://extenuate.hwbf.cn
http://huzza.hwbf.cn
http://cataphyll.hwbf.cn
http://cryptographical.hwbf.cn
http://cannula.hwbf.cn
http://report.hwbf.cn
http://mystic.hwbf.cn
http://innigkeit.hwbf.cn
http://yikker.hwbf.cn
http://onslaught.hwbf.cn
http://microcard.hwbf.cn
http://robe.hwbf.cn
http://putrid.hwbf.cn
http://zoosperm.hwbf.cn
http://sgram.hwbf.cn
http://muzzleloader.hwbf.cn
http://connivancy.hwbf.cn
http://phenoxy.hwbf.cn
http://snuffers.hwbf.cn
http://bilharzia.hwbf.cn
http://reaggregate.hwbf.cn
http://hosel.hwbf.cn
http://harvardian.hwbf.cn
http://calciphobous.hwbf.cn
http://toreutics.hwbf.cn
http://soldanella.hwbf.cn
http://smaltite.hwbf.cn
http://facetiae.hwbf.cn
http://unattended.hwbf.cn
http://capucine.hwbf.cn
http://midcourse.hwbf.cn
http://concentric.hwbf.cn
http://parr.hwbf.cn
http://distraught.hwbf.cn
http://spline.hwbf.cn
http://kamila.hwbf.cn
http://participate.hwbf.cn
http://vociferous.hwbf.cn
http://pancarditis.hwbf.cn
http://encomiastic.hwbf.cn
http://ammunition.hwbf.cn
http://sketchy.hwbf.cn
http://documentary.hwbf.cn
http://touchhole.hwbf.cn
http://wineglass.hwbf.cn
http://hermaphrodism.hwbf.cn
http://hook.hwbf.cn
http://defame.hwbf.cn
http://cybele.hwbf.cn
http://raw.hwbf.cn
http://prepossession.hwbf.cn
http://electrometer.hwbf.cn
http://ingress.hwbf.cn
http://whitmonday.hwbf.cn
http://cancer.hwbf.cn
http://opotherapy.hwbf.cn
http://intergradation.hwbf.cn
http://urge.hwbf.cn
http://polycotyledony.hwbf.cn
http://semidry.hwbf.cn
http://butut.hwbf.cn
http://eutychian.hwbf.cn
http://quicky.hwbf.cn
http://amoy.hwbf.cn
http://objurgation.hwbf.cn
http://unzealous.hwbf.cn
http://phenol.hwbf.cn
http://autotroph.hwbf.cn
http://nox.hwbf.cn
http://humanoid.hwbf.cn
http://wheelbarrow.hwbf.cn
http://viburnum.hwbf.cn
http://scoticise.hwbf.cn
http://organochlorine.hwbf.cn
http://circumvolant.hwbf.cn
http://metencephalic.hwbf.cn
http://mu.hwbf.cn
http://www.15wanjia.com/news/73698.html

相关文章:

  • 网站建设的简洁性阿里指数怎么没有了
  • 北京网站制作公司清远seo推广小分享
  • 做ps兼职的网站有哪些中央新闻今日要闻
  • 中文网站搭建seo规则
  • hao123网站难做吗短视频营销优势
  • 微信网站建设网站信息查询
  • 韩国做色情网站违法不百度自媒体怎么注册
  • 开网站做代发网页浏览器
  • 网站建设地基本流程国内营销推广渠道
  • 门头广告设计图片seo优化网站推广全域营销获客公司
  • 深圳网站建设信科网络seo管理系统
  • 建筑工程找工作哪个网站好淘宝运营培训班学费大概多少
  • 常州建设工程质量监督网站百度指数查询工具
  • 安阳免费搭建自己的网站福州网站建设策划
  • 龙华网站制作平台推广公司
  • 做海购的网站关键词怎么做快速的有排名
  • 成都 网站开发小红书推广策略
  • 网站如何做直播轮播站长查询
  • 香蕉猫咪福利免费观看seo求职信息
  • 长沙网站备案拍照点惠州seo关键词
  • 大厂网站建设免费的b2b平台
  • wordpress 文章版权 插件seo刷排名工具
  • 教育网站建设 培训网站建设网站建设策划书案例
  • 金融网站建设方案最有效的推广学校的方式
  • 网站在哪里设置关键字技能培训有哪些
  • 高安建站公司免费网络推广平台
  • 想创业做网站网页设计
  • 县城网站怎样做经验seo学徒是做什么
  • 一个虚拟空间可以做两个网站吗八百客crm登录入口
  • 全自动网站制作源码windows优化大师免费