Desarrollo de software con Claude Code
Dani Zysman
AI Curriculum Manager @ DataCamp

music-analytics-api/
app.py # API existente (actualizada para registrar db_routes)
routes.py # Rutas existentes
models.py # Modelos Artist/Album
db_routes.py # <- Nuevo Blueprint con base de datos
chinook.db
test_routes.py
CLAUDE.md
> Create a new db_routes.py blueprint with an endpoint at
/artists/<artist_id>/albums that returns all albums by
a specific artist as JSON from chinook,db. Register it with @app.py
1. db_routes py - Nuevo Blueprint de Flask con:
- Helper get_db_connection() para conectar a chinook.db
- Endpoint añadido que consulta todos los álbumes de un/a artista concreto/a
- Devuelve JSON con los campos AlbumId, Title y ArtistId
2. app.py actualizado
- Registrado el blueprint para activar el nuevo endpoint
El endpoint devolverá 404 si el/la artista no existe
y devolverá los álbumes ordenados por título en JSON.
@app.route("/artists/<int:artist_id>/albums")
def get_artist_albums(artist_id):
conn = get_db()
artist = conn.execute("SELECT ... WHERE ArtistId = ?", ...)
albums = conn.execute("SELECT ... WHERE ArtistId = ?", ...)
conn.close()
return jsonify({"artist": ..., "albums": [...]})
$ python app.py & PID=$! && sleep 3 && curl http://127.0.0.1:5000/artists/1/albums
&& kill $PID
127.0.0.1 "GET /artists/1/albums HTTP/1.1" 200
[
{
"album_id": 1,
"artist_id": 1,
"title": "For Those About To Rock We Salute You"
},
{
"album_id": 4,
"artist_id": 1,
"title": "Let There Be Rock"
}
]
{"albums": []}from pydantic import BaseModel
from typing import List
class Album(BaseModel):
id: int
title: str
class ArtistAlbums(BaseModel):
artist_id: int
albums: List[Album]
> Add Pydantic validation to @db_routes.py. Return 404 with a message if the
artist doesn't exist or has no albums.
¡Hecho! Se añadió validación con Pydantic a db_routes.py:
Cambios:
- Añadido modelo Album (album_id, title, artist_id)
- Devuelve 404 "Artist not found" si el/la artista no existe
- Devuelve 404 "Artist has no albums" si no tiene álbumes
- Valida cada álbum con el modelo Album antes de devolver
- Devuelve 400 en errores de validación
Respuestas:
- 404 "Artist not found"
- 404 "Artist has no albums"
- 200 con datos de álbum validados
Desarrollo de software con Claude Code