Browse Source

add:增加通过fastapi搭建书籍和音乐搜索并跑通流程;

sszf7605 9 months ago
parent
commit
ccc65ce004

+ 11 - 11
api_demo/v1/impl/BookImpl.py

@@ -1,14 +1,14 @@
-# routes/login.py
-from fastapi import APIRouter, HTTPException
-from common.pojo import BookVo
-from service_demo.impl.AuthServiceImpl import authenticate_user
+from fastapi import APIRouter
+from service_demo.impl.BookServiceImpl import get_book, get_books
 
-router = APIRouter()
+book_router = APIRouter()
 
+# 路径参数通常用于指定资源的唯一标识符或者名称等信息。
+@book_router.get("/{book_id}")
+async def get_book_route(book_id: int):
+    return get_book(book_id)
 
-@router.post("/login")
-async def login(user: User):
-    if not authenticate_user(user.username, user.password):
-        raise HTTPException(
-            status_code=401, detail="Incorrect username or password")
-    return {"message": "Login successful", "username": user.username}
+
+@book_router.get("/")
+async def get_books_route():
+    return get_books()

+ 9 - 0
api_demo/v1/impl/MusicImpl.py

@@ -0,0 +1,9 @@
+from fastapi import APIRouter
+from service_demo.impl.MusicServiceImpl import search_music
+
+music_router = APIRouter()
+
+# 查询参数通常用于传递额外的信息,比如搜索关键字、过滤条件等等。
+@music_router.get("/search")
+async def search_music_route(query: str):
+    return search_music(query)

BIN
api_demo/v1/impl/__pycache__/BookImpl.cpython-310.pyc


BIN
api_demo/v1/impl/__pycache__/MusicImpl.cpython-310.pyc


+ 35 - 4
main.py

@@ -1,8 +1,39 @@
-# main.py
+# from fastapi import FastAPI
+
+# app = FastAPI()
+
+# # 书籍列表
+# books = [
+#     {"id": 1, "title": "Book 1", "author": "Author 1"},
+#     {"id": 2, "title": "Book 2", "author": "Author 2"},
+#     {"id": 3, "title": "Book 3", "author": "Author 3"}
+# ]
+
+
+# @app.get("/books/{book_id}")
+# async def get_book(book_id: int):
+#     for book in books:
+#         if book["id"] == book_id:
+#             return book
+#     return {"message": "Book not found"}
+
+
+# @app.get("/books")
+# async def get_books():
+#     return books
+
 from fastapi import FastAPI
-from api-demo.v1. import login
+from api_demo.v1.impl.BookImpl import book_router
+from api_demo.v1.impl.MusicImpl import music_router
 
 app = FastAPI()
 
-# 注册路由
-app.include_router(login.router)
+# 注册书籍相关的路由
+app.include_router(book_router, prefix="/books", tags=["books"])
+
+# 注册音乐搜索相关的路由
+app.include_router(music_router, prefix="/music", tags=["music"])
+
+if __name__ == "__main__":
+    import uvicorn
+    uvicorn.run(app, host="127.0.0.1", port=8000)

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+fastapi==0.68.0
+uvicorn==0.15.0

+ 16 - 4
service_demo/impl/BookServiceImpl.py

@@ -1,4 +1,16 @@
-# utils/chatgpt.py
-def process_data(data):
-    # 使用 ChatGPT 模型处理数据的函数
-    pass
+books = [
+    {"id": 1, "title": "Book 1", "author": "Author 1"},
+    {"id": 2, "title": "Book 2", "author": "Author 2"},
+    {"id": 3, "title": "Book 3", "author": "Author 3"}
+]
+
+
+def get_book(book_id: int):
+    for book in books:
+        if book["id"] == book_id:
+            return book
+    return {"message": "Book not found"}
+
+
+def get_books():
+    return books

+ 7 - 0
service_demo/impl/MusicServiceImpl.py

@@ -0,0 +1,7 @@
+def search_music(query: str):
+    # 这里可以是你实际的音乐搜索逻辑,暂时返回一个示例结果
+    return [
+        {"title": "Song 1", "artist": "Artist 1"},
+        {"title": "Song 2", "artist": "Artist 2"},
+        {"title": "Song 3", "artist": "Artist 3"}
+    ]

BIN
service_demo/impl/__pycache__/BookServiceImpl.cpython-310.pyc


BIN
service_demo/impl/__pycache__/MusicServiceImpl.cpython-310.pyc