API Endpoint: GET /tracks/artist/{artist_name}
Relevant source files
The following files were used as context for generating this wiki page:
The GET /tracks/artist/{artist_name} endpoint is a core component of the Spotify API Data Collector's web interface. Its primary purpose is to allow users to query the local SQLite database for tracks associated with a specific artist. This endpoint facilitates targeted data retrieval from the collection of music metadata gathered from the Spotify Web API.
Within the project architecture, this endpoint serves as a read-only interface to the indexed tracks table. It supports case-insensitive searching and result limiting to optimize performance and usability for downstream analysis or user interfaces.
Architecture and Data Flow
The endpoint follows a standard FastAPI path operation structure, interacting with the database layer to fetch records. When a request is received, the system retrieves a database connection and executes a case-insensitive SQL query against the artist_name column.
Request Flow
The following sequence diagram illustrates the interaction between the Client, the FastAPI application, and the SQLite database during a request to this endpoint.
Sources: app/api.py:164-187, app/database.py:108-111
Endpoint Specification
The endpoint is defined in app/api.py and is configured to return a dictionary containing the artist's name, the count of tracks found, and the list of track objects.
Path and Query Parameters
| Parameter | Type | Location | Description |
|---|---|---|---|
artist_name | string | Path | The name of the artist to search for (exact match, case-insensitive). |
limit | integer | Query | Optional. Max results to return (1-1000). |
Sources: app/api.py:164-173, README.md:126-130
Response Structure
The response is a JSON object with the following fields:
artist_name: The name provided in the request.count: The number of tracks returned in the current response after applying limits.tracks: An array of track objects containing metadata such astrack_name,track_popularity, andalbum_name.
Sources: app/api.py:182-187
Database Logic and Performance
The endpoint relies on a specific database function, get_tracks_by_artist, located in app/database.py. To ensure high performance even with large datasets, the system utilizes a database index on the artist_name column.
SQL Implementation
The underlying query uses the LOWER() function on both the column and the input parameter to ensure case-insensitive matching. Results are ordered by track_popularity in descending order.
def get_tracks_by_artist(db: Database, artist_name: str) -> list[dict]:
"""Récupère tous les tracks d'un artiste donné (insensible à la casse)."""
query = "SELECT * FROM tracks WHERE LOWER(artist_name) = LOWER(?) ORDER BY track_popularity DESC"
return db.execute(query, (artist_name,))
Sources: app/database.py:108-111
Data Indexing
An index named idx_tracks_artist_name is created during database initialization to accelerate these specific lookups.
Sources: app/database.py:32, README.md:46
Testing and Validation
The functionality of this endpoint is validated through comprehensive tests in test/test_api.py and test/test_api_extended.py. These tests ensure that:
- The endpoint returns the correct status code (200 OK) for valid artists.
- The search is truly case-insensitive (e.g., searching for "test artist" matches "Test Artist").
- The
limitparameter correctly restricts the number of items in thetrackslist. - Empty results are handled gracefully when an artist does not exist in the database.
Sources: test/test_api.py:186-231, test/test_api_extended.py:68-79
Summary
The GET /tracks/artist/{artist_name} endpoint provides a reliable and efficient way to filter the collected Spotify dataset by artist. By leveraging SQLite indexing and case-insensitive SQL queries, it maintains performance while providing flexible access to music metadata.