orchestrator
¶
External API Service Orchestrator.
This module provides the main coordination layer for fetching album release years from multiple API providers (MusicBrainz, Discogs). It replaces the legacy external API service with a modular architecture that maintains backward compatibility while providing better separation of concerns.
The orchestrator handles: - HTTP session management and connection pooling - Rate limiting coordination across all API providers - Request caching and response aggregation - Dependency injection for cache and verification services - Authentication token management with encryption support - Release year determination using the sophisticated scoring algorithm
ExternalApiOrchestrator
¶
ExternalApiOrchestrator(
*,
config,
console_logger,
error_logger,
analytics,
cache_service,
pending_verification_service
)
External API service orchestrator.
Coordinates API calls across multiple providers (MusicBrainz, Discogs) to determine the original release year for music albums. Provides rate limiting, caching, authentication, and sophisticated scoring to identify the most likely original release.
This class implements a modular architecture for external API services, providing unified access to MusicBrainz and Discogs APIs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Configuration dictionary |
required |
console_logger
|
Logger
|
Logger for general output |
required |
error_logger
|
Logger
|
Logger for errors and warnings |
required |
analytics
|
Analytics
|
Analytics service for performance tracking |
required |
cache_service
|
CacheOrchestrator
|
Service for caching API responses |
required |
pending_verification_service
|
PendingVerificationService
|
Service for managing verification queue |
required |
Source code in src/services/api/orchestrator.py
initialize
async
¶
Initialize the aiohttp ClientSession and API clients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
bool
|
If True, close existing session and reinitialize. |
False
|
Raises:
| Type | Description |
|---|---|
AttributeError
|
Re-raised after session cleanup if initialization fails due to a missing attribute. |
RuntimeError
|
Re-raised after session cleanup if initialization fails due to a runtime error. |
TypeError
|
Re-raised after session cleanup if initialization fails due to a type error. |
ValueError
|
Re-raised after session cleanup if initialization fails due to a value error. |
Source code in src/services/api/orchestrator.py
close
async
¶
Close the orchestrator and clean up resources gracefully.
This method: 1. Waits for pending fire-and-forget tasks to complete (PENDING_TASKS_SHUTDOWN_TIMEOUT) 2. Cancels any tasks that don't complete in time 3. Clears the _pending_tasks set 4. Logs API statistics 5. Closes the HTTP session
Source code in src/services/api/orchestrator.py
get_album_year
async
¶
Determine the original release year for an album using optimized API calls and revised scoring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist
|
str
|
Artist name |
required |
album
|
str
|
Album name |
required |
current_library_year
|
str | None
|
Year currently stored in the library for this album |
None
|
earliest_track_added_year
|
int | None
|
Year the earliest track of the album was added to the library |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str | None
|
Tuple of (year, is_definitive, confidence_score, year_scores) |
|
year_scores |
bool
|
dict mapping each year found by APIs to its max score |
Source code in src/services/api/orchestrator.py
get_artist_activity_period
async
¶
Retrieve the period of activity for an artist from MusicBrainz.
This method delegates to the MusicBrainz client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist_norm
|
str
|
Normalized artist name |
required |
Returns:
| Type | Description |
|---|---|
tuple[int | None, int | None]
|
Tuple of (start_year, end_year) as integers or (None, None) if not found |
Source code in src/services/api/orchestrator.py
get_artist_start_year
async
¶
Get artist's career start year with caching and fallback.
Uses MusicBrainz as primary source, iTunes as fallback. Results are cached in GenericCacheService.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist_norm
|
str
|
Normalized artist name |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Artist's career start year, or None if not found |
Cache TTL
- Positive result: 1 year (31,536,000 seconds)
- Negative result: 1 day (86,400 seconds)
Source code in src/services/api/orchestrator.py
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 | |
get_year_from_discogs
async
¶
Fetch the earliest release year for an album from Discogs.
This method delegates to the Discogs client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist
|
str
|
Artist name |
required |
album
|
str
|
Album name |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Year string or None if not found |
Source code in src/services/api/orchestrator.py
normalize_name
¶
Normalize artist/album name for API queries.
Performs substitutions that improve API matching: - & → and (Karma & Effect → Karma and Effect) - w/ → with (Split w/ Band → Split with Band) - Strips trailing compilation markers (Album + 4 → Album) - Normalizes whitespace
Note: This is for API QUERIES, not for scoring/matching. Scoring uses ReleaseScorer._normalize_name which is more aggressive.
Source code in src/services/api/orchestrator.py
create_external_api_orchestrator
¶
create_external_api_orchestrator(
*,
config,
console_logger,
error_logger,
analytics,
cache_service,
pending_verification_service
)
Create the configured ExternalApiOrchestrator instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Typed application configuration |
required |
console_logger
|
Logger
|
Logger for general output |
required |
error_logger
|
Logger
|
Logger for error messages and warnings |
required |
analytics
|
Analytics
|
Analytics service for performance tracking |
required |
cache_service
|
CacheOrchestrator
|
Service for caching API responses |
required |
pending_verification_service
|
PendingVerificationService
|
Service for managing verification queue |
required |
Returns:
| Type | Description |
|---|---|
ExternalApiOrchestrator
|
The configured ExternalApiOrchestrator instance |