Spaces:
Sleeping
Sleeping
Commit
·
8e4ba0a
1
Parent(s):
02a15c2
pagination
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
import logging
|
| 4 |
import os
|
|
@@ -51,8 +51,36 @@ async def get_music_store():
|
|
| 51 |
return load_balancer.MUSIC_STORE
|
| 52 |
|
| 53 |
@app.get("/api/get/music/all")
|
| 54 |
-
async def get_all_music_api(
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
@app.get('/api/get/instances')
|
| 58 |
async def get_instances():
|
|
@@ -88,7 +116,28 @@ async def get_categories():
|
|
| 88 |
return load_balancer.get_all_categories()
|
| 89 |
|
| 90 |
@app.get("/api/get/category/{category}")
|
| 91 |
-
async def get_all_from_a_category(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if not category:
|
| 93 |
raise HTTPException(status_code=400, detail="category parameter is required")
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request, Query
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
import logging
|
| 4 |
import os
|
|
|
|
| 51 |
return load_balancer.MUSIC_STORE
|
| 52 |
|
| 53 |
@app.get("/api/get/music/all")
|
| 54 |
+
async def get_all_music_api(
|
| 55 |
+
page: int = Query(None, ge=1), # Default to None, but must be at least 1 if provided
|
| 56 |
+
limit: int = Query(None, ge=1, le=100) # Default to None, but must be between 1 and 100 if provided
|
| 57 |
+
):
|
| 58 |
+
# Fetch all music files from the load balancer
|
| 59 |
+
all_music = load_balancer.get_all_music()
|
| 60 |
+
|
| 61 |
+
# If pagination parameters are not provided, return all music
|
| 62 |
+
if page is None or limit is None:
|
| 63 |
+
return {"total_files": len(all_music), "files": all_music}
|
| 64 |
+
|
| 65 |
+
# Calculate the starting index and the end index for pagination
|
| 66 |
+
start_index = (page - 1) * limit
|
| 67 |
+
end_index = start_index + limit
|
| 68 |
+
|
| 69 |
+
# Handle the case where the requested page exceeds available files
|
| 70 |
+
if start_index >= len(all_music):
|
| 71 |
+
raise HTTPException(status_code=404, detail="No more files available for the requested page.")
|
| 72 |
+
|
| 73 |
+
# Get the paginated music files
|
| 74 |
+
paginated_music = all_music[start_index:end_index]
|
| 75 |
+
|
| 76 |
+
# Prepare the response
|
| 77 |
+
return {
|
| 78 |
+
"page": page,
|
| 79 |
+
"limit": limit,
|
| 80 |
+
"total_files": len(all_music),
|
| 81 |
+
"files": paginated_music
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
|
| 85 |
@app.get('/api/get/instances')
|
| 86 |
async def get_instances():
|
|
|
|
| 116 |
return load_balancer.get_all_categories()
|
| 117 |
|
| 118 |
@app.get("/api/get/category/{category}")
|
| 119 |
+
async def get_all_from_a_category(
|
| 120 |
+
category: str,
|
| 121 |
+
page: int = Query(1, ge=1), # Default to page 1, minimum value is 1
|
| 122 |
+
limit: int = Query(10, ge=1, le=100) # Default limit is 10, max limit is 100
|
| 123 |
+
):
|
| 124 |
if not category:
|
| 125 |
raise HTTPException(status_code=400, detail="category parameter is required")
|
| 126 |
+
|
| 127 |
+
# Fetch all files from the category
|
| 128 |
+
all_files = load_balancer.get_files_from_category(category)
|
| 129 |
+
|
| 130 |
+
# Calculate the starting index and the end index for pagination
|
| 131 |
+
start_index = (page - 1) * limit
|
| 132 |
+
end_index = start_index + limit
|
| 133 |
+
|
| 134 |
+
# Get the paginated files
|
| 135 |
+
paginated_files = all_files[start_index:end_index]
|
| 136 |
+
|
| 137 |
+
# Prepare the response
|
| 138 |
+
return {
|
| 139 |
+
"page": page,
|
| 140 |
+
"limit": limit,
|
| 141 |
+
"total_files": len(all_files),
|
| 142 |
+
"files": paginated_files
|
| 143 |
+
}
|