-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastAPI_2.py
More file actions
96 lines (81 loc) Β· 4.02 KB
/
Copy pathFastAPI_2.py
File metadata and controls
96 lines (81 loc) Β· 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from fastapi import FastAPI, HTTPException
from enum import Enum
from schema_2 import Band, GenreChoice
app = FastAPI()
# Bands data (genre in lowercase for consistency)
Bands = [
{"id": 1, "name": "The Beatles", "genre": "rock"},
{"id": 2, "name": "Led Zeppelin", "genre": "rock"},
{"id": 3, "name": "Pink Floyd", "genre": "progressive rock"},
{"id": 4, "name": "Queen", "genre": "rock"},
{"id": 5, "name": "The Rolling Stones", "genre": "rock"},
{"id": 6, "name": "Nirvana", "genre": "grunge"},
{"id": 7, "name": "Metallica", "genre": "metal",'albums': [
{"id": 1, "title": "Master of Puppets", "band_id": 7, "year": 1986},
{"id": 2, "title": "Ride the Lightning", "band_id": 7, "year": 1984}
]},
{"id": 8, "name": "U2", "genre": "rock"},
{"id": 9, "name": "Radiohead", "genre": "alternative rock"},
{"id": 10, "name": "Coldplay", "genre": "alternative rock"},
{"id": 11, "name": "Adele", "genre": "pop"},
{"id": 12, "name": "BeyoncΓ©", "genre": "pop"},
{"id": 13, "name": "Taylor Swift", "genre": "pop"},
{"id": 14, "name": "Drake", "genre": "hip-hop"},
{"id": 15, "name": "Kendrick Lamar", "genre": "hip-hop"},
{"id": 16, "name": "Eminem", "genre": "hip-hop"},
{"id": 17, "name": "Miles Davis", "genre": "jazz"},
{"id": 18, "name": "John Coltrane", "genre": "jazz"},
{"id": 19, "name": "Ella Fitzgerald", "genre": "jazz"},
{"id": 20, "name": "Ludwig van Beethoven", "genre": "classical"},
{"id": 21, "name": "Wolfgang Amadeus Mozart", "genre": "classical"},
{"id": 22, "name": "Johann Sebastian Bach", "genre": "classical"},
{"id": 23, "name": "Bob Marley", "genre": "reggae"},
{"id": 24, "name": "Peter Tosh", "genre": "reggae"},
{"id": 25, "name": "Jimmy Cliff", "genre": "reggae"},
{"id": 26, "name": "B.B. King", "genre": "blues"},
{"id": 27, "name": "Muddy Waters", "genre": "blues"},
{"id": 28, "name": "Robert Johnson", "genre": "blues"},
{"id": 29, "name": "Taylor Swift", "genre": "country"},
{"id": 30, "name": "Johnny Cash", "genre": "country"},
{"id": 31, "name": "Dolly Parton", "genre": "country"},
{"id": 32, "name": "Daft Punk", "genre": "electronic"},
{"id": 33, "name": "Deadmau5", "genre": "electronic"},
]
# -------------------
# Bands Endpoints
# -------------------
@app.get('/bands', summary="Get all bands", description="Returns a list of all bands.")
async def get_bands() -> list[Band]:
return [
Band(**band) for band in Bands
]
@app.get('/bands/{band_id}', summary="Get band by ID", description="Returns a band by its ID.")
async def get_band(band_id: int)-> Band:
band = next((Band(**b) for b in Bands if b["id"] == band_id), None)
if not band:
raise HTTPException(status_code=404, detail="Band not found")
return band
@app.get('/bands/name/{band_name}', summary="Search bands by name", description="Search bands by partial name match (case-insensitive).")
async def get_band_by_name(band_name: str):
matched = [b for b in Bands if band_name.lower() in b["name"].lower()]
if not matched:
raise HTTPException(status_code=404, detail="No bands found with this name")
return matched
@app.get('/bands/genre/{genre_name}', summary="Get bands by genre", description="Returns bands of a specific genre (case-insensitive).")
async def get_bands_by_genre(genre_name: str):
matched = [b for b in Bands if b["genre"].lower() == genre_name.lower()]
if not matched:
raise HTTPException(status_code=404, detail="No bands found for this genre")
return matched
# -------------------
# General Endpoints
# -------------------
@app.get('/', summary="Root endpoint", description="Welcome message.")
async def root():
return {"message": "Hello World!"}
@app.get('/status', summary="Server status", description="Returns the status of the server.")
async def status():
return {"status": "ok"}
@app.get('/about', summary="About this API", description="Returns information about this API.")
async def about():
return "This is a sample FastAPI application."