Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Use bulk tracks from API client (#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson committed Sep 8, 2022
1 parent 643cbc5 commit a3e62e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
26 changes: 26 additions & 0 deletions packages/common/src/services/audius-api-client/AudiusAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const FULL_ENDPOINT_MAP = {
topGenreUsers: '/users/genre/top',
topArtists: '/users/top',
getTrack: (trackId: OpaqueID) => `/tracks/${trackId}`,
getTracks: () => `/tracks`,
getTrackByHandleAndSlug: `/tracks`,
getStems: (trackId: OpaqueID) => `/tracks/${trackId}/stems`,
getRemixes: (trackId: OpaqueID) => `/tracks/${trackId}/remixes`,
Expand Down Expand Up @@ -119,6 +120,11 @@ export type GetTrackArgs = {
}
}

type GetTracksArgs = {
ids: ID[]
currentUserId: Nullable<ID>
}

type GetTrackByHandleAndSlugArgs = {
handle: string
slug: string
Expand Down Expand Up @@ -796,6 +802,26 @@ export class AudiusAPIClient {
return adapted
}

async getTracks({ ids, currentUserId }: GetTracksArgs) {
this._assertInitialized()
const encodedTrackIds = ids.map((id) => this._encodeOrThrow(id))
const encodedCurrentUserId = encodeHashId(currentUserId)
const params = {
id: encodedTrackIds,
user_id: encodedCurrentUserId || undefined
}

const trackResponse: Nullable<APIResponse<APITrack[]>> =
await this._getResponse(FULL_ENDPOINT_MAP.getTracks(), params, true)
if (!trackResponse) {
return null
}
const adapted = trackResponse.data
.map((track) => adapter.makeTrack(track))
.filter(removeNullable)
return adapted
}

async getTrackByHandleAndSlug({
handle,
slug,
Expand Down
16 changes: 4 additions & 12 deletions packages/web/src/common/store/cache/tracks/utils/retrieveTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,11 @@ export function* retrieveTracks({
},
retrieveFromSource: function* (ids: ID[] | UnlistedTrackRequest[]) {
const apiClient = yield* getContext('apiClient')
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
let fetched: UserTrackMetadata | UserTrackMetadata[] | null | undefined
if (canBeUnlisted) {
const ids = trackIds as UnlistedTrackRequest[]
// TODO: remove the AudiusBackend
// branches here when we support
// bulk track fetches in the API.
if (ids.length > 1) {
fetched = yield* call(
audiusBackendInstance.getTracksIncludingUnlisted,
trackIds as UnlistedTrackRequest[]
)
throw new Error('Can only query for single unlisted track')
} else {
fetched = yield* call([apiClient, 'getTrack'], {
id: ids[0].id,
Expand All @@ -243,10 +236,9 @@ export function* retrieveTracks({
} else {
const ids = trackIds as number[]
if (ids.length > 1) {
fetched = yield* call(audiusBackendInstance.getAllTracks, {
offset: 0,
limit: ids.length,
idsArray: ids as ID[]
fetched = yield* call([apiClient, 'getTracks'], {
ids,
currentUserId
})
} else {
fetched = yield* call([apiClient, 'getTrack'], {
Expand Down

0 comments on commit a3e62e8

Please sign in to comment.