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.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Configuration dictionary |
|
console_logger |
Logger for general output |
|
error_logger |
Logger for errors and warnings |
|
cache_service |
Service for caching API responses |
|
pending_verification_service |
Service for managing verification queue |
|
session |
ClientSession | None
|
HTTP session for API requests |
rate_limiters |
ClientSession | None
|
Rate limiters for each API provider |
scoring_config |
ClientSession | None
|
Configuration for release scoring algorithm |
release_scorer |
ClientSession | None
|
Scorer for evaluating release candidates |
Initialize the API orchestrator with configuration, loggers, and dependencies.
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 |
|---|---|
Exception
|
Re-raises any exception from initialization after cleanup. |
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.
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
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 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 | |
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 |