Vytvoř jeden API endpoint

Vývoj softwaru s Claude Code

Dani Zysman

AI Curriculum Manager @ DataCamp

Od dotazu k API

recraft: half: Vývojář u stolu s AI asistentem Claude, který pomáhá vytvořit API, na obrazovce kód s připojením k databázi

  • Předchozí video: Interaktivní dotazování databáze
  • Teď: Přidej endpoint do Music Analytics API
    • Jeden prompt, jeden funkční endpoint, skutečný Python kód
Vývoj softwaru s Claude Code

Stav projektu

music-analytics-api/
  app.py             # Existing API (updated to register db_routes)
  routes.py          # Existing routes
  models.py          # Artist/Album models
  db_routes.py       # <- New database-backed Blueprint
  chinook.db
  test_routes.py
  CLAUDE.md
Vývoj softwaru s Claude Code

Požádej Claude o vytvoření

> 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 - New Flask Blueprint with:
    - get_db_connection() helper to connect to chinook.db
    - Added endpoint that queries the database for all albums by a specific artist
    - Returns JSON with AlbumId, Title, and ArtistId fields
2. Updated app.py 
    - Registered the blueprint so the new endpoint is active

The endpoint will return a 404 if the artist doesn't exist, 
and returns albums sorted by title as JSON.
Vývoj softwaru s Claude Code

Vygenerovaný kód

@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": [...]})
Vývoj softwaru s Claude Code

Otestuj to lokálně

$ 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"
    }
]
Vývoj softwaru s Claude Code

Ošetři krajní případy

  • Co když...
    • ID umělce neexistuje?
    • Umělec nemá žádná alba?
    • Neplatný formát ID?
    • Aktuální kód vrací prázdné {"albums": []}
  • Lepší odpovědi:
    • 404 s jasnou zprávou
    • Ověřená struktura odpovědi
    • Správné zpracování chyb
Vývoj softwaru s Claude Code

Přidej validaci pomocí Pydantic

from pydantic import BaseModel
from typing import List

class Album(BaseModel):
    id: int
    title: str

class ArtistAlbums(BaseModel):
    artist_id: int
    albums: List[Album]
  • Pydantic nabízí:
    • Validaci typů
    • Automatickou serializaci
    • Srozumitelné chybové zprávy
    • Samodokumentující schémata
Vývoj softwaru s Claude Code

Požádej Claude o přidání validace

> Add Pydantic validation to @db_routes.py. Return 404 with a message if the 
artist doesn't exist or has no albums.
Done! Added Pydantic validation to db_routes.py:
Changes:
- Added Album model (album_id, title, artist_id)
- Returns 404 "Artist not found" if artist doesn't exist
- Returns 404 "Artist has no albums" if artist has no albums
- Validates each album against Album model before returning
- Returns 400 on validation errors
Responses:
- 404 "Artist not found"
- 404 "Artist has no albums"  
- 200 with validated album data
Vývoj softwaru s Claude Code

Pojďme cvičit!

Vývoj softwaru s Claude Code

Preparing Video For Download...