Sensei 透過自動處理路由、資料驗證和回應對應來簡化建立 API 用戶端的流程。這降低了 HTTP 請求的複雜性,使您可以更輕鬆地將 API 整合到您的專案中,而無需編寫樣板程式碼。
Sensei 使用類型提示來產生 API 用戶端,為與 API 互動提供清晰的介面和強大的驗證。它的語法與框架FastAPI
非常相似- 文件:https://sensei.crocofactory.dev
- 原始碼:https://github.com/CrocoFactory/sensei
程式碼範例
from typing import Annotated from sensei import Router, Path, APIModel router = Router('https://pokeapi.co/api/v2/') class Pokemon(APIModel): name: str id: int height: int weight: int @router.get('/pokemon/{name}') def get_pokemon(name: Annotated[str, Path(max_length=300)]) -> Pokemon: pass pokemon = get_pokemon(name="pikachu") print(pokemon) # Pokemon(name='pikachu'> <p>Didn't it seem to you that the function doesn't contain the code? <strong>Sensei writes it instead of you!</strong> The result of the call get_pokemon(name="pikachu") is the object Pokemon(name='pikachu'> </p><p>There is a wonderful OOP approach proposed by Sensei:<br> </p> <pre class="brush:php;toolbar:false">class User(APIModel): email: EmailStr id: PositiveInt first_name: str last_name: str avatar: AnyHttpUrl @classmethod @router.get('/users') def query( cls, page: Annotated[int, Query()] = 1, per_page: Annotated[int, Query(le=7)] = 3 ) -> list[Self]: pass @classmethod @router.get('/users/{id_}') def get(cls, id_: Annotated[int, Path(alias='id')]) -> Self: pass @router.post('/token') def login(self) -> str: pass @login.prepare def _login_in(self, args: Args) -> Args: args.json_['email'] = self.email return args @login.finalize def _login_out(self, response: Response) -> str: return response.json()['token'] user = User.get(1) user.login() # User(id=1, email="john@example.com", first_name="John", ...)
當Sensei不知道如何處理請求時,你可以自己處理,使用預處理作為prepare,後處理作為finalize
比較
Sensei:它提供了高水準的抽象。 Sensei 簡化了 API 包裝器的創建,提供了輕鬆路由、資料驗證以及將 API 回應自動對應到模型的裝飾器。這減少了樣板檔案並提高了程式碼的可讀性和可維護性。
裸 HTTP 用戶端:像 requests 或 httpx 這樣的裸 HTTP 用戶端需要手動管理請求、處理回應解析、資料驗證和錯誤處理。您必須為每個端點編寫重複的程式碼。
特徵
Sensei 提供了對標準 API 和混亂 API 都有用的功能:
- 驗證? ️
- 速率限制處理 ⏳
- 自動處理回傳類型?
- 沒有重複的 DRY 架構?
- 非同步支援⚡
- 大小寫轉換和別名?
- 擁有快速請求的客戶端?
目標受眾
使用 API 的開發人員、資料科學家和分析師等
以上是Sensei:簡化 API 用戶端生成的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用