-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc08f30
commit 60a6842
Showing
37 changed files
with
13,362 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/album/AlbumApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.rubenquadros.kovibes.api.album | ||
|
||
import io.github.rubenquadros.kovibes.api.ApiResponse | ||
import io.github.rubenquadros.kovibes.api.album.models.GetAlbumTracksResponse | ||
import io.github.rubenquadros.kovibes.api.album.models.GetNewAlbumReleasesResponse | ||
import io.github.rubenquadros.kovibes.api.models.AlbumInfo | ||
import io.github.rubenquadros.kovibes.api.response.ErrorBody | ||
|
||
/** | ||
* Album API interface provides methods using which one can get all the information about an album. | ||
* | ||
* Each API returns [ApiResponse] | ||
*/ | ||
internal interface AlbumApi { | ||
|
||
/** | ||
* Get album API returns the information about an album. | ||
* | ||
* @param id | ||
* @param market | ||
* @return [AlbumInfo] when success and [ErrorBody] when error. | ||
*/ | ||
suspend fun getAlbum(id: String, market: String?): ApiResponse<AlbumInfo, ErrorBody> | ||
|
||
/** | ||
* Get album tracks API returns all the tracks in an album. | ||
* | ||
* @param id | ||
* @param market | ||
* @param limit | ||
* @param offset | ||
* @return [GetAlbumTracksResponse] when success and [ErrorBody] when error. | ||
*/ | ||
suspend fun getAlbumTracks( | ||
id: String, | ||
market: String?, | ||
limit: Int, | ||
offset: Int | ||
): ApiResponse<GetAlbumTracksResponse, ErrorBody> | ||
|
||
/** | ||
* Get new album releases API returns all the albums that have newly released. | ||
* | ||
* @param limit | ||
* @param offset | ||
* @return [GetNewAlbumReleasesResponse] when success and [ErrorBody] when error. | ||
*/ | ||
suspend fun getNewAlbumReleases( | ||
limit: Int, | ||
offset: Int | ||
): ApiResponse<GetNewAlbumReleasesResponse, ErrorBody> | ||
} |
85 changes: 85 additions & 0 deletions
85
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/album/AlbumApiImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.github.rubenquadros.kovibes.api.album | ||
|
||
import io.github.rubenquadros.kovibes.api.ApiResponse | ||
import io.github.rubenquadros.kovibes.api.KtorService | ||
import io.github.rubenquadros.kovibes.api.album.models.GetNewAlbumReleasesResponse | ||
import io.github.rubenquadros.kovibes.api.album.models.GetAlbumTracksResponse | ||
import io.github.rubenquadros.kovibes.api.getParsedHttpResponse | ||
import io.github.rubenquadros.kovibes.api.models.AlbumInfo | ||
import io.github.rubenquadros.kovibes.api.response.ErrorBody | ||
import io.ktor.client.request.get | ||
import io.ktor.http.path | ||
import io.ktor.util.StringValues | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
internal class AlbumApiImpl( | ||
private val ktorService: KtorService, | ||
private val dispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) : AlbumApi { | ||
override suspend fun getAlbum(id: String, market: String?): ApiResponse<AlbumInfo, ErrorBody> { | ||
val response = withContext(dispatcher) { | ||
ktorService.client.get { | ||
url { | ||
path("v1/albums/${id}") | ||
|
||
parameters.appendAll( | ||
StringValues.build { | ||
market?.let { this["market"] = market } | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
return response.getParsedHttpResponse() | ||
} | ||
|
||
override suspend fun getAlbumTracks( | ||
id: String, | ||
market: String?, | ||
limit: Int, | ||
offset: Int | ||
): ApiResponse<GetAlbumTracksResponse, ErrorBody> { | ||
val response = withContext(dispatcher) { | ||
ktorService.client.get { | ||
url { | ||
path("v1/albums/$id/tracks") | ||
|
||
parameters.appendAll( | ||
StringValues.build { | ||
market?.let { this["market"] = market } | ||
this["limit"] = limit.toString() | ||
this["offset"] = offset.toString() | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
return response.getParsedHttpResponse() | ||
} | ||
|
||
override suspend fun getNewAlbumReleases( | ||
limit: Int, | ||
offset: Int | ||
): ApiResponse<GetNewAlbumReleasesResponse, ErrorBody> { | ||
val response = withContext(dispatcher) { | ||
ktorService.client.get { | ||
url { | ||
path("v1/browse/new-releases") | ||
|
||
parameters.appendAll( | ||
StringValues.build { | ||
this["limit"] = limit.toString() | ||
this["offset"] = offset.toString() | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
return response.getParsedHttpResponse() | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
kovibes/src/commonMain/kotlin/io/github/rubenquadros/kovibes/api/album/AlbumApiMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.rubenquadros.kovibes.api.album | ||
|
||
import io.github.rubenquadros.kovibes.api.album.models.GetAlbumTracksResponse | ||
import io.github.rubenquadros.kovibes.api.album.models.GetNewAlbumReleasesResponse | ||
import io.github.rubenquadros.kovibes.api.album.models.SimplifiedTrackInfo | ||
import io.github.rubenquadros.kovibes.api.mapper.toAlbum | ||
import io.github.rubenquadros.kovibes.api.mapper.toArtist | ||
import io.github.rubenquadros.kovibes.api.response.AlbumTrack | ||
import io.github.rubenquadros.kovibes.api.response.AlbumTracks | ||
import io.github.rubenquadros.kovibes.api.response.NewAlbumReleases | ||
|
||
/** | ||
* @suppress | ||
* Map [GetAlbumTracksResponse] to [AlbumTracks]. | ||
*/ | ||
internal fun GetAlbumTracksResponse.toAlbumTracks(): AlbumTracks { | ||
return AlbumTracks( | ||
isNext = next != null, | ||
items = items.map { it.toAlbumTrack() } | ||
) | ||
} | ||
|
||
/** | ||
* @suppress | ||
* Map [GetNewAlbumReleasesResponse] to [NewAlbumReleases]. | ||
*/ | ||
internal fun GetNewAlbumReleasesResponse.toNewAlbumReleases(): NewAlbumReleases { | ||
return NewAlbumReleases( | ||
isNext = albums.next != null, | ||
items = albums.items.map { it.toAlbum() } | ||
) | ||
} | ||
|
||
private fun SimplifiedTrackInfo.toAlbumTrack(): AlbumTrack { | ||
return AlbumTrack( | ||
id = id, | ||
name = name, | ||
previewUrl = previewUrl, | ||
duration = duration, | ||
artists = artists.map { it.toArtist() } | ||
) | ||
} |
Oops, something went wrong.