223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498 | class AsyncDeezerClient(BaseClient):
"""Async client (async context-manager aware).
Example:
>>> async with AsyncDeezerClient() as client:
... track = await client.track.get(3135556)
"""
def __init__(
self,
access_token: str | None = None,
*,
base_url: str = API_BASE_URL,
timeout: float = DEFAULT_TIMEOUT,
user_agent: str | None = None,
http_client: httpx.AsyncClient | None = None,
rate_limit: AsyncRateLimiter | bool | None = None,
max_retries: int = 3,
retry_base: float = 0.5,
) -> None:
# NOTE: resources call the sync-named hooks which we implement as
# coroutines; async use goes through ``Async*`` proxy methods below.
super().__init__(
access_token=access_token,
base_url=base_url,
timeout=timeout,
user_agent=user_agent,
)
from .http import DEFAULT_USER_AGENT
self._transport = AsyncTransport(
base_url=self.base_url,
access_token=access_token,
timeout=timeout,
user_agent=user_agent or DEFAULT_USER_AGENT,
client=http_client,
rate_limiter=True if rate_limit is None else rate_limit,
max_retries=max_retries,
retry_base=retry_base,
)
self._bind_async_resources()
# -- context manager ---------------------------------------------------
async def __aenter__(self) -> "AsyncDeezerClient":
return self
async def __aexit__(self, *exc: Any) -> None:
await self.close()
async def close(self) -> None:
await self._transport.close()
def set_access_token(self, token: str | None) -> None:
self.access_token = token
self._transport.access_token = token
# -- transport passthrough (coroutines) ----------------------------------
async def _get(self, path: str, *, params: dict[str, Any] | None = None) -> Any: # type: ignore[override]
return await self._transport.get(path, params=params)
async def _post( # type: ignore[override]
self,
path: str,
*,
params: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
) -> Any:
return await self._transport.post(path, params=params, data=data)
async def _delete( # type: ignore[override]
self, path: str, *, params: dict[str, Any] | None = None
) -> Any:
return await self._transport.delete(path, params=params)
async def _fetch_url(self, url: str) -> dict[str, Any]: # type: ignore[override]
from urllib.parse import parse_qsl, urlsplit
from .http import _decode
if url.startswith(self.base_url):
# Keep the query string (see sync _fetch_url).
split = urlsplit(url)
path = split.path
query = dict(parse_qsl(split.query, keep_blank_values=True))
params = query or None
if path.startswith(("http://", "https://")):
response = await self._transport.client.get(url)
return _decode(response, endpoint=url)
return await self._transport.get(path, params=params)
if url.startswith(("http://", "https://")):
response = await self._transport.client.get(url)
return _decode(response, endpoint=url)
return await self._transport.get(url)
# -- resource proxying ----------------------------------------------------
# Resource methods are written sync-style against ``_get``/``_post``.
# For the async client each resource method must be awaited *inside* the
# resource (so ``_paginated`` receives data, not a coroutine). The proxy
# below rewrites the resource's ``_client`` reference to an async shim
# whose _get/_post/_delete/_fetch_url are coroutine functions, then
# awaits the resource method itself.
def _bind_async_resources(self) -> None:
for name in (
"album", "artist", "track", "playlist", "podcast", "episode",
"user", "chart", "editorial", "genre", "radio", "search",
"oembed", "infos", "options",
):
setattr(self, name, _AsyncResourceProxy(getattr(self, name)))
async def raw(
self,
method: str,
path: str,
*,
params: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
) -> Any:
return await self._transport.request(method, path, params=params, data=data)
# -- high-level helpers (async mirrors of HighLevelMixin) --------------------
async def find_track(self, query: str, *, limit: int = 5) -> list: # type: ignore[override]
return list(await self.search.tracks(query, limit=limit))[:limit]
async def find_album(self, query: str, *, limit: int = 5) -> list: # type: ignore[override]
return list(await self.search.albums(query, limit=limit))[:limit]
async def find_artist(self, query: str, *, limit: int = 5) -> list: # type: ignore[override]
return list(await self.search.artists(query, limit=limit))[:limit]
async def find_playlist(self, query: str, *, limit: int = 5) -> list: # type: ignore[override]
return list(await self.search.playlists(query, limit=limit))[:limit]
async def find_podcast(self, query: str, *, limit: int = 5) -> list: # type: ignore[override]
return list(await self.search.podcasts(query, limit=limit))[:limit]
async def best_track_match(self, query: str) -> dict | None: # type: ignore[override]
items = await self.find_track(query, limit=1)
return items[0] if items else None
async def best_album_match(self, query: str) -> dict | None: # type: ignore[override]
items = await self.find_album(query, limit=1)
return items[0] if items else None
async def best_artist_match(self, query: str) -> dict | None: # type: ignore[override]
items = await self.find_artist(query, limit=1)
return items[0] if items else None
async def artist_top_tracks(self, artist_id: int, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.artist.top(artist_id, limit=limit))[:limit]
async def artist_albums(self, artist_id: int, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.artist.albums(artist_id, limit=limit))[:limit]
async def artist_related(self, artist_id: int, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.artist.related(artist_id, limit=limit))[:limit]
async def artist_radio_tracks(self, artist_id: int, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.artist.radio(artist_id, limit=limit))[:limit]
async def album_tracks(self, album_id: int) -> list: # type: ignore[override]
return list(await self.album.tracks(album_id))
async def playlist_tracks(self, playlist_id: int, *, limit: int | None = None) -> list: # type: ignore[override]
items = list(await self.playlist.tracks(playlist_id, limit=limit or 100))
return items if limit is None else items[:limit]
async def radio_tracks(self, radio_id: int, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.radio.tracks(radio_id, limit=limit))[:limit]
async def podcast_episodes(self, podcast_id: int, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.podcast.episodes(podcast_id, limit=limit))[:limit]
async def top_tracks(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.chart.tracks(limit=limit))[:limit]
async def top_albums(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.chart.albums(limit=limit))[:limit]
async def top_artists(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.chart.artists(limit=limit))[:limit]
async def top_playlists(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.chart.playlists(limit=limit))[:limit]
async def top_podcasts(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.chart.podcasts(limit=limit))[:limit]
async def genre_top_tracks(self, genre_id: int, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.chart.tracks(genre_id, limit=limit))[:limit]
async def genres(self, *, limit: int = 50) -> list: # type: ignore[override]
return list(await self.genre.list(limit=limit))[:limit]
async def new_releases(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.editorial.releases(limit=limit))[:limit]
async def editorial_selection(self, *, limit: int = 10) -> list: # type: ignore[override]
return list(await self.editorial.selection(limit=limit))[:limit]
async def track_preview_url(self, track_id: int) -> str | None: # type: ignore[override]
track = await self.track.get(track_id)
return track.get("preview") if isinstance(track, dict) else None
async def me(self) -> dict: # type: ignore[override]
return await self.user.me()
async def my_playlists(self, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.user.playlists("me", limit=limit))[:limit]
async def my_favorite_tracks(self, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.user.tracks("me", limit=limit))[:limit]
async def my_favorite_albums(self, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.user.albums("me", limit=limit))[:limit]
async def my_favorite_artists(self, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.user.artists("me", limit=limit))[:limit]
async def my_flow(self, *, limit: int = 25) -> list: # type: ignore[override]
return list(await self.user.flow("me", limit=limit))[:limit]
async def create_playlist(self, title: str, user_id: Any = "me") -> dict: # type: ignore[override]
return await self.user.create_playlist(user_id, title)
async def add_to_playlist(self, playlist_id: int, track_ids: Any) -> bool: # type: ignore[override]
return await self.playlist.add_tracks(playlist_id, track_ids)
async def full_track(self, track_id: int) -> dict: # type: ignore[override]
track = await self.track.get(track_id)
out: dict[str, Any] = {"track": track}
album = (track.get("album") or {}) if isinstance(track, dict) else {}
artist = (track.get("artist") or {}) if isinstance(track, dict) else {}
if album.get("id"):
try:
out["album"] = await self.album.get(album["id"])
except Exception:
out["album"] = album
if artist.get("id"):
try:
out["artist"] = await self.artist.get(artist["id"])
except Exception:
out["artist"] = artist
return out
async def full_album(self, album_id: int) -> dict: # type: ignore[override]
album = await self.album.get(album_id)
out: dict[str, Any] = {"album": album}
artist = (album.get("artist") or {}) if isinstance(album, dict) else {}
if artist.get("id"):
try:
out["artist"] = await self.artist.get(artist["id"])
except Exception:
out["artist"] = artist
return out
async def exchange_code_for_token(
self, app_id: str | int, secret: str, code: str, *, output: str = "json"
) -> dict:
from urllib.parse import urlencode
query = urlencode(
{"app_id": str(app_id), "secret": secret, "code": code, "output": output}
)
response = await self._transport.client.get(f"{TOKEN_URL}?{query}")
response.raise_for_status()
content_type = response.headers.get("content-type", "")
if output == "json" or "json" in content_type:
try:
data = response.json()
except ValueError:
data = parse_token_response(response.text)
else:
data = parse_token_response(response.text)
if isinstance(data, dict):
return data
return {"access_token": data}
|