Bouw één API-endpoint

Softwareontwikkeling met Claude Code

Dani Zysman

AI Curriculum Manager @ DataCamp

Van query naar API

recraft: half: Een developer aan een bureau met Claude AI-assistent die helpt een API te bouwen, met code en databaseverbinding op het scherm

  • Vorige video: Database interactief bevraagd
  • Nu: Een endpoint toevoegen aan de Music Analytics API
    • Eén prompt, één werkend endpoint, echte Python-code
Softwareontwikkeling met Claude Code

Het project tot nu toe

music-analytics-api/
  app.py             # Bestaande API (bijgewerkt om db_routes te registreren)
  routes.py          # Bestaande routes
  models.py          # Artist/Album-modellen
  db_routes.py       # <- Nieuwe database-gestuurde Blueprint
  chinook.db
  test_routes.py
  CLAUDE.md
Softwareontwikkeling met Claude Code

Vraag Claude om het te bouwen

> 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 - Nieuwe Flask-Blueprint met:
    - get_db_connection() helper om te verbinden met chinook.db
    - Endpoint toegevoegd dat alle albums voor een specifieke artiest uit de database ophaalt
    - Geeft JSON terug met velden AlbumId, Title en ArtistId
2. app.py bijgewerkt 
    - De blueprint geregistreerd zodat het nieuwe endpoint actief is

Het endpoint geeft 404 terug als de artiest niet bestaat,
en retourneert albums als JSON, gesorteerd op titel.
Softwareontwikkeling met Claude Code

De gegenereerde code

@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": [...]})
Softwareontwikkeling met Claude Code

Test lokaal

$ 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"
    }
]
Softwareontwikkeling met Claude Code

Randgevallen afhandelen

  • Wat als...
    • Artist ID bestaat niet?
    • Artiest heeft geen albums?
    • Ongeldig ID-formaat?
    • Huidige code geeft lege {"albums": []} terug
  • Betere responses:
    • 404 met duidelijke boodschap
    • Gevalideerde responsstructuur
    • Correcte foutafhandeling
Softwareontwikkeling met Claude Code

Voeg validatie toe met 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 biedt:
    • Typevalidatie
    • Automatische serialisatie
    • Duidelijke foutmeldingen
    • Zelfdocumenterende schema's
Softwareontwikkeling met Claude Code

Vraag Claude om validatie toe te voegen

> Add Pydantic validation to @db_routes.py. Return 404 with a message if the 
artist doesn't exist or has no albums.
Klaar! Pydantic-validatie toegevoegd aan db_routes.py:
Wijzigingen:
- Album-model toegevoegd (album_id, title, artist_id)
- Geeft 404 "Artist not found" als artiest niet bestaat
- Geeft 404 "Artist has no albums" als artiest geen albums heeft
- Valideert elk album tegen Album-model vóór teruggave
- Geeft 400 bij validatiefouten
Responses:
- 404 "Artist not found"
- 404 "Artist has no albums"  
- 200 met gevalideerde albumdata
Softwareontwikkeling met Claude Code

Laten we oefenen!

Softwareontwikkeling met Claude Code

Preparing Video For Download...